Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
import streamlit as st
|
2 |
import instaloader
|
3 |
import os
|
4 |
-
import re
|
5 |
from dotenv import load_dotenv
|
6 |
import time
|
|
|
7 |
|
8 |
# Load environment variables
|
9 |
load_dotenv()
|
@@ -12,54 +12,27 @@ load_dotenv()
|
|
12 |
INSTAGRAM_USERNAME = os.getenv('INSTAGRAM_USERNAME')
|
13 |
INSTAGRAM_PASSWORD = os.getenv('INSTAGRAM_PASSWORD')
|
14 |
|
15 |
-
def
|
16 |
-
"""
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
except instaloader.exceptions.BadCredentialsException:
|
37 |
-
st.error("Invalid Instagram credentials")
|
38 |
-
return None
|
39 |
-
except instaloader.exceptions.TwoFactorAuthRequiredException:
|
40 |
-
st.error("2FA is enabled on this account. Please use an account without 2FA.")
|
41 |
-
return None
|
42 |
-
except Exception as e:
|
43 |
-
st.error(f"Error initializing Instaloader: {str(e)}")
|
44 |
-
return None
|
45 |
-
|
46 |
-
def extract_shortcode(url):
|
47 |
-
"""Extract shortcode from Instagram URL"""
|
48 |
-
try:
|
49 |
-
# Handle different URL formats
|
50 |
-
patterns = [
|
51 |
-
r'instagram.com/reel/([A-Za-z0-9_-]+)',
|
52 |
-
r'instagram.com/p/([A-Za-z0-9_-]+)',
|
53 |
-
r'/([A-Za-z0-9_-]{11})[/]?$'
|
54 |
-
]
|
55 |
-
|
56 |
-
for pattern in patterns:
|
57 |
-
match = re.search(pattern, url)
|
58 |
-
if match:
|
59 |
-
return match.group(1)
|
60 |
-
return None
|
61 |
-
except Exception:
|
62 |
-
return None
|
63 |
|
64 |
def download_reel(url):
|
65 |
"""Download Instagram reel"""
|
@@ -68,87 +41,87 @@ def download_reel(url):
|
|
68 |
|
69 |
# Initialize loader with debug message
|
70 |
st.info("Initializing downloader...")
|
71 |
-
loader =
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
|
82 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
# Create download directory
|
84 |
-
os.makedirs("reels", exist_ok=True)
|
85 |
|
86 |
-
#
|
87 |
-
|
|
|
88 |
|
89 |
-
# Download the post
|
90 |
st.info("Fetching post metadata...")
|
91 |
post = instaloader.Post.from_shortcode(loader.context, shortcode)
|
92 |
|
93 |
-
st.info("Starting download
|
94 |
-
loader.download_post(post, target=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
-
return f"Reel downloaded successfully to reels/{shortcode}"
|
97 |
-
|
98 |
except instaloader.exceptions.InstaloaderException as e:
|
99 |
-
|
100 |
-
if "404" in error_msg:
|
101 |
-
return "Post not found. The reel might be private or deleted."
|
102 |
-
elif "429" in error_msg:
|
103 |
-
return "Too many requests. Please try again later."
|
104 |
-
elif "login" in error_msg.lower():
|
105 |
-
return "Session expired. Please try logging in again."
|
106 |
-
return f"Instagram error: {error_msg}"
|
107 |
except Exception as e:
|
108 |
return f"An unexpected error occurred: {str(e)}"
|
109 |
|
110 |
def main():
|
111 |
-
st.set_page_config(
|
112 |
-
page_title="Instagram Reel Downloader",
|
113 |
-
page_icon="📱"
|
114 |
-
)
|
115 |
|
116 |
st.title('📱 Instagram Reel Downloader')
|
117 |
|
118 |
-
#
|
119 |
-
st.
|
120 |
-
### Instructions:
|
121 |
-
1. Paste an Instagram reel URL
|
122 |
-
2. Click 'Download Reel'
|
123 |
-
3. Wait for the download to complete
|
124 |
-
|
125 |
-
Note: This tool requires valid Instagram credentials to work.
|
126 |
-
""")
|
127 |
-
|
128 |
-
# Debug mode toggle
|
129 |
-
debug_mode = st.checkbox('Enable debug mode')
|
130 |
|
131 |
# Input for Reel URL
|
132 |
url = st.text_input('Enter the Instagram Reel URL',
|
133 |
placeholder='https://www.instagram.com/reel/...')
|
134 |
|
135 |
-
if debug_mode and url:
|
136 |
-
st.info(f"URL provided: {url}")
|
137 |
-
shortcode = extract_shortcode(url)
|
138 |
-
if shortcode:
|
139 |
-
st.info(f"Extracted shortcode: {shortcode}")
|
140 |
-
else:
|
141 |
-
st.warning("Could not extract shortcode from URL")
|
142 |
-
|
143 |
# Download button
|
144 |
if st.button('Download Reel', type='primary'):
|
145 |
if url:
|
146 |
with st.spinner('Downloading reel...'):
|
147 |
result = download_reel(url)
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
|
|
|
|
|
|
152 |
else:
|
153 |
st.warning('Please enter a URL.')
|
154 |
|
|
|
1 |
import streamlit as st
|
2 |
import instaloader
|
3 |
import os
|
|
|
4 |
from dotenv import load_dotenv
|
5 |
import time
|
6 |
+
import glob
|
7 |
|
8 |
# Load environment variables
|
9 |
load_dotenv()
|
|
|
12 |
INSTAGRAM_USERNAME = os.getenv('INSTAGRAM_USERNAME')
|
13 |
INSTAGRAM_PASSWORD = os.getenv('INSTAGRAM_PASSWORD')
|
14 |
|
15 |
+
def verify_download(shortcode):
|
16 |
+
"""Verify that files were actually downloaded"""
|
17 |
+
download_path = f"reels/{shortcode}"
|
18 |
+
|
19 |
+
# Check if directory exists
|
20 |
+
if not os.path.exists(download_path):
|
21 |
+
return False, "Download directory not found"
|
22 |
+
|
23 |
+
# Look for video files (mp4)
|
24 |
+
video_files = glob.glob(f"{download_path}/*.mp4")
|
25 |
+
|
26 |
+
if video_files:
|
27 |
+
# Get the full path of the first video file
|
28 |
+
video_path = video_files[0]
|
29 |
+
file_size = os.path.getsize(video_path)
|
30 |
+
return True, {
|
31 |
+
'path': video_path,
|
32 |
+
'size': f"{file_size / (1024*1024):.2f} MB",
|
33 |
+
'exists': os.path.exists(video_path)
|
34 |
+
}
|
35 |
+
return False, "No video files found in download directory"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
def download_reel(url):
|
38 |
"""Download Instagram reel"""
|
|
|
41 |
|
42 |
# Initialize loader with debug message
|
43 |
st.info("Initializing downloader...")
|
44 |
+
loader = instaloader.Instaloader(
|
45 |
+
download_videos=True,
|
46 |
+
download_video_thumbnails=False,
|
47 |
+
download_geotags=False,
|
48 |
+
download_comments=False,
|
49 |
+
save_metadata=False,
|
50 |
+
compress_json=False,
|
51 |
+
max_connection_attempts=3,
|
52 |
+
dirname_pattern="reels/{target}" # Explicitly set download directory
|
53 |
+
)
|
54 |
|
55 |
try:
|
56 |
+
if INSTAGRAM_USERNAME and INSTAGRAM_PASSWORD:
|
57 |
+
st.info(f"Attempting to login as {INSTAGRAM_USERNAME}...")
|
58 |
+
loader.login(INSTAGRAM_USERNAME, INSTAGRAM_PASSWORD)
|
59 |
+
st.success("Login successful!")
|
60 |
+
else:
|
61 |
+
st.error("Instagram credentials not found")
|
62 |
+
return "Missing credentials"
|
63 |
+
|
64 |
+
# Extract shortcode
|
65 |
+
shortcode = url.strip('/').split('/')[-1]
|
66 |
+
if not shortcode:
|
67 |
+
shortcode = url.strip('/').split('/')[-2]
|
68 |
+
|
69 |
+
st.info(f"Attempting to download reel with shortcode: {shortcode}")
|
70 |
+
|
71 |
# Create download directory
|
72 |
+
os.makedirs(f"reels/{shortcode}", exist_ok=True)
|
73 |
|
74 |
+
# Get absolute path
|
75 |
+
abs_download_path = os.path.abspath(f"reels/{shortcode}")
|
76 |
+
st.info(f"Download directory: {abs_download_path}")
|
77 |
|
78 |
+
# Download the post
|
79 |
st.info("Fetching post metadata...")
|
80 |
post = instaloader.Post.from_shortcode(loader.context, shortcode)
|
81 |
|
82 |
+
st.info(f"Starting download of post type: {post.typename}")
|
83 |
+
loader.download_post(post, target=shortcode)
|
84 |
+
|
85 |
+
# Verify the download
|
86 |
+
success, result = verify_download(shortcode)
|
87 |
+
|
88 |
+
if success:
|
89 |
+
return f"""Download completed:
|
90 |
+
- File: {result['path']}
|
91 |
+
- Size: {result['size']}
|
92 |
+
- File exists: {result['exists']}"""
|
93 |
+
else:
|
94 |
+
return f"Download failed: {result}"
|
95 |
|
|
|
|
|
96 |
except instaloader.exceptions.InstaloaderException as e:
|
97 |
+
return f"Instagram error: {str(e)}\nCurrent working directory: {os.getcwd()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
except Exception as e:
|
99 |
return f"An unexpected error occurred: {str(e)}"
|
100 |
|
101 |
def main():
|
102 |
+
st.set_page_config(page_title="Instagram Reel Downloader", page_icon="📱")
|
|
|
|
|
|
|
103 |
|
104 |
st.title('📱 Instagram Reel Downloader')
|
105 |
|
106 |
+
# Show current working directory
|
107 |
+
st.text(f"Working directory: {os.getcwd()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
# Input for Reel URL
|
110 |
url = st.text_input('Enter the Instagram Reel URL',
|
111 |
placeholder='https://www.instagram.com/reel/...')
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
# Download button
|
114 |
if st.button('Download Reel', type='primary'):
|
115 |
if url:
|
116 |
with st.spinner('Downloading reel...'):
|
117 |
result = download_reel(url)
|
118 |
+
st.text(result) # Show detailed result
|
119 |
+
|
120 |
+
# List contents of download directory
|
121 |
+
if "reels" in os.listdir():
|
122 |
+
st.text("Contents of reels directory:")
|
123 |
+
for item in os.listdir("reels"):
|
124 |
+
st.text(f"- {item}")
|
125 |
else:
|
126 |
st.warning('Please enter a URL.')
|
127 |
|