slimshadow commited on
Commit
151202b
·
verified ·
1 Parent(s): b68bcf0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -53
app.py CHANGED
@@ -1,65 +1,45 @@
1
- # streamlit_instaloader_cookie.py
2
  import streamlit as st
3
  import instaloader
4
  import os
5
- from pathlib import Path
6
- import base64
7
 
8
- def download_reel_from_link(link, cookiefile):
9
- L = instaloader.Instaloader()
10
- download_dir = "downloaded_reels"
11
- Path(download_dir).mkdir(exist_ok=True)
12
 
13
- try:
14
- # Load session from cookies
15
- L.context.load_cookiejar(cookiefile)
16
-
17
- # Extract shortcode from the link
18
- shortcode = link.split('/')[-2]
19
-
20
- # Download the reel
21
- post = instaloader.Post.from_shortcode(L.context, shortcode)
22
- L.download_post(post, target=download_dir)
23
- reel_path = os.path.join(download_dir, f"{post.date_utc.strftime('%Y-%m-%d_%H-%M-%S')}_{shortcode}.mp4")
24
-
25
- # Return the path to the downloaded reel
26
- return reel_path
27
 
28
- except instaloader.exceptions.InstaloaderException as e:
29
- st.error(f"Error: {e}")
30
- return None
31
-
32
- def get_download_link(file_path):
33
- with open(file_path, "rb") as f:
34
- file_bytes = f.read()
35
- b64 = base64.b64encode(file_bytes).decode()
36
- href = f'<a href="data:file/mp4;base64,{b64}" download="{Path(file_path).name}">Download Reel</a>'
37
- return href
38
 
39
  def main():
40
- st.title("Instagram Reel Downloader")
41
- st.write("Enter the Instagram reel link and provide your cookie file to download:")
42
-
43
- link = st.text_input("Instagram Reel Link", "")
44
- cookiefile = st.file_uploader("Upload Cookie File")
45
-
46
- if st.button("Download Reel"):
47
- if link and cookiefile:
48
- # Save the uploaded cookie file locally
49
- cookie_path = f"cookies_{cookiefile.name}"
50
- with open(cookie_path, "wb") as f:
51
- f.write(cookiefile.getvalue())
52
-
53
- st.info(f"Downloading reel from {link}...")
54
- reel_path = download_reel_from_link(link, cookie_path)
55
- if reel_path:
56
- st.success(f"Downloaded reel: {Path(reel_path).name}")
57
- st.markdown(get_download_link(reel_path), unsafe_allow_html=True)
58
-
59
- # Clean up the saved cookie file
60
- os.remove(cookie_path)
61
  else:
62
- st.warning("Please enter a valid Instagram reel link and upload your cookie file.")
63
 
64
  if __name__ == "__main__":
65
  main()
 
 
1
  import streamlit as st
2
  import instaloader
3
  import os
 
 
4
 
5
+ def download_reel(url, username, password):
6
+ # Set up Instaloader with login credentials
7
+ loader = instaloader.Instaloader()
8
+ loader.login(username, password)
9
 
10
+ # Extract shortcode from URL
11
+ shortcode = url.split('/')[-2]
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # Download the reel
14
+ try:
15
+ post = instaloader.Post.from_shortcode(loader.context, shortcode)
16
+ loader.download_post(post, target=f"reels/{shortcode}")
17
+ return f"Reel downloaded successfully to reels/{shortcode}"
18
+ except Exception as e:
19
+ return f"An error occurred: {str(e)}"
 
 
 
20
 
21
  def main():
22
+ st.title('Instagram Reel Downloader')
23
+
24
+ # Input for Reel URL
25
+ url = st.text_input('Enter the Instagram Reel URL')
26
+
27
+ # Handle form submission
28
+ if st.button('Download Reel'):
29
+ if url:
30
+ # Instagram login credentials (remember to handle this securely)
31
+ username = 'balance.and.blisss'
32
+ password = '1e9!5a=e1^BvRBYjz%aB'
33
+
34
+ # Create the directory if it doesn't exist
35
+ if not os.path.exists("reels"):
36
+ os.makedirs("reels")
37
+
38
+ # Download the reel
39
+ result = download_reel(url, username, password)
40
+ st.write(result)
 
 
41
  else:
42
+ st.warning('Please enter a URL.')
43
 
44
  if __name__ == "__main__":
45
  main()