Chrunos commited on
Commit
7251edc
·
verified ·
1 Parent(s): c542a72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -26
app.py CHANGED
@@ -1,45 +1,102 @@
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 = 'chrunosc'
32
- password = 'Chrunosc58'
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()
 
1
  import streamlit as st
2
  import instaloader
3
  import os
4
+ from dotenv import load_dotenv
5
 
6
+ # Load environment variables
7
+ load_dotenv()
 
 
8
 
9
+ # Get credentials from environment variables
10
+ INSTAGRAM_USERNAME = os.getenv('INSTAGRAM_USERNAME')
11
+ INSTAGRAM_PASSWORD = os.getenv('INSTAGRAM_PASSWORD')
12
 
13
+ def initialize_instaloader():
14
+ """Initialize and login to Instaloader"""
15
  try:
16
+ loader = instaloader.Instaloader()
17
+ if INSTAGRAM_USERNAME and INSTAGRAM_PASSWORD:
18
+ loader.login(INSTAGRAM_USERNAME, INSTAGRAM_PASSWORD)
19
+ return loader
20
+ else:
21
+ st.error("Instagram credentials not found in environment variables")
22
+ return None
23
+ except instaloader.exceptions.BadCredentialsException:
24
+ st.error("Invalid Instagram credentials")
25
+ return None
26
+ except Exception as e:
27
+ st.error(f"Error initializing Instaloader: {str(e)}")
28
+ return None
29
+
30
+ def extract_shortcode(url):
31
+ """Extract shortcode from Instagram URL"""
32
+ try:
33
+ # Handle both full URLs and shortened ones
34
+ parts = url.strip('/').split('/')
35
+ return parts[-1] if parts[-1] else parts[-2]
36
+ except Exception:
37
+ return None
38
+
39
+ def download_reel(url):
40
+ """Download Instagram reel"""
41
+ if not url:
42
+ return "Please enter a URL"
43
+
44
+ loader = initialize_instaloader()
45
+ if not loader:
46
+ return "Failed to initialize downloader"
47
+
48
+ shortcode = extract_shortcode(url)
49
+ if not shortcode:
50
+ return "Invalid URL format"
51
+
52
+ try:
53
+ # Create download directory
54
+ os.makedirs("reels", exist_ok=True)
55
+
56
+ # Download the post
57
  post = instaloader.Post.from_shortcode(loader.context, shortcode)
58
  loader.download_post(post, target=f"reels/{shortcode}")
59
+
60
  return f"Reel downloaded successfully to reels/{shortcode}"
61
+
62
+ except instaloader.exceptions.InstaloaderException as e:
63
+ return f"Instagram error: {str(e)}"
64
  except Exception as e:
65
+ return f"An unexpected error occurred: {str(e)}"
66
 
67
  def main():
68
+ st.set_page_config(
69
+ page_title="Instagram Reel Downloader",
70
+ page_icon="📱"
71
+ )
72
+
73
+ st.title('📱 Instagram Reel Downloader')
74
+
75
+ # Add instructions
76
+ st.markdown("""
77
+ ### Instructions:
78
+ 1. Paste an Instagram reel URL
79
+ 2. Click 'Download Reel'
80
+ 3. Wait for the download to complete
81
+
82
+ Note: This tool requires valid Instagram credentials to work.
83
+ """)
84
+
85
  # Input for Reel URL
86
+ url = st.text_input('Enter the Instagram Reel URL',
87
+ placeholder='https://www.instagram.com/reel/...')
88
+
89
+ # Download button
90
+ if st.button('Download Reel', type='primary'):
91
  if url:
92
+ with st.spinner('Downloading reel...'):
93
+ result = download_reel(url)
94
+ if "successfully" in result:
95
+ st.success(result)
96
+ else:
97
+ st.error(result)
 
 
 
 
 
98
  else:
99
  st.warning('Please enter a URL.')
100
 
101
  if __name__ == "__main__":
102
+ main()