slimshadow commited on
Commit
eee9760
·
verified ·
1 Parent(s): 469ad23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -22
app.py CHANGED
@@ -1,38 +1,52 @@
1
- # streamlit_instaloader.py
2
  import streamlit as st
3
  import instaloader
 
 
 
4
 
5
- def download_reels(username):
6
  L = instaloader.Instaloader()
 
 
7
 
8
  try:
9
- # Login (if needed)
10
- # L.login(username, password)
11
 
12
- # Get the profile
13
- profile = instaloader.Profile.from_username(L.context, username)
14
-
15
- # Iterate over the reels
16
- for post in profile.get_posts():
17
- if post.typename == 'GraphVideo': # Reels are typically videos
18
- L.download_post(post, target=f"{username}_reels")
19
- st.success(f"Downloaded reel: {post.shortcode}")
20
 
21
  except instaloader.exceptions.InstaloaderException as e:
22
  st.error(f"Error: {e}")
 
23
 
24
- def main():
25
- st.title("Instagram Reels Downloader")
26
- st.write("Enter the Instagram username to download their reels:")
27
-
28
- username = st.text_input("Instagram Username", "")
 
29
 
30
- if st.button("Download Reels"):
31
- if username:
32
- st.info(f"Downloading reels for {username}...")
33
- download_reels(username)
 
 
 
 
 
 
 
 
34
  else:
35
- st.warning("Please enter a username.")
36
 
37
  if __name__ == "__main__":
38
  main()
 
1
+ # streamlit_instaloader_link.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):
9
  L = instaloader.Instaloader()
10
+ download_dir = "downloaded_reels"
11
+ Path(download_dir).mkdir(exist_ok=True)
12
 
13
  try:
14
+ # Extract shortcode from the link
15
+ shortcode = link.split('/')[-2]
16
 
17
+ # Download the reel
18
+ post = instaloader.Post.from_shortcode(L.context, shortcode)
19
+ L.download_post(post, target=download_dir)
20
+ reel_path = os.path.join(download_dir, f"{post.date_utc.strftime('%Y-%m-%d_%H-%M-%S')}_{shortcode}.mp4")
21
+
22
+ # Return the path to the downloaded reel
23
+ return reel_path
 
24
 
25
  except instaloader.exceptions.InstaloaderException as e:
26
  st.error(f"Error: {e}")
27
+ return None
28
 
29
+ def get_download_link(file_path):
30
+ with open(file_path, "rb") as f:
31
+ file_bytes = f.read()
32
+ b64 = base64.b64encode(file_bytes).decode()
33
+ href = f'<a href="data:file/mp4;base64,{b64}" download="{Path(file_path).name}">Download Reel</a>'
34
+ return href
35
 
36
+ def main():
37
+ st.title("Instagram Reel Downloader")
38
+ st.write("Enter the Instagram reel link to download:")
39
+
40
+ link = st.text_input("Instagram Reel Link", "")
41
+ if st.button("Download Reel"):
42
+ if link:
43
+ st.info(f"Downloading reel from {link}...")
44
+ reel_path = download_reel_from_link(link)
45
+ if reel_path:
46
+ st.success(f"Downloaded reel: {Path(reel_path).name}")
47
+ st.markdown(get_download_link(reel_path), unsafe_allow_html=True)
48
  else:
49
+ st.warning("Please enter a valid Instagram reel link.")
50
 
51
  if __name__ == "__main__":
52
  main()