Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,8 @@ import instaloader
|
|
3 |
import os
|
4 |
from dotenv import load_dotenv
|
5 |
import re
|
6 |
-
|
|
|
7 |
|
8 |
# Load environment variables
|
9 |
load_dotenv()
|
@@ -29,43 +30,46 @@ def clean_url(url):
|
|
29 |
match = re.search(pattern, cleaned_url)
|
30 |
if match:
|
31 |
return match.group(1)
|
32 |
-
|
33 |
# If patterns don't match, try getting the last segment
|
34 |
segments = cleaned_url.split('/')
|
35 |
if segments:
|
36 |
-
|
37 |
-
if len(last_segment) >= 11: # Instagram shortcodes are usually 11 chars or longer
|
38 |
-
return last_segment
|
39 |
|
40 |
return None
|
41 |
except Exception as e:
|
42 |
st.error(f"Error cleaning URL: {str(e)}")
|
43 |
return None
|
44 |
|
45 |
-
def
|
46 |
-
"""
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
def download_reel(url):
|
71 |
"""Download Instagram reel"""
|
@@ -79,7 +83,7 @@ def download_reel(url):
|
|
79 |
|
80 |
st.info(f"Extracted shortcode: {shortcode}")
|
81 |
|
82 |
-
# Initialize loader
|
83 |
loader = instaloader.Instaloader(
|
84 |
download_videos=True,
|
85 |
download_video_thumbnails=False,
|
@@ -98,29 +102,49 @@ def download_reel(url):
|
|
98 |
else:
|
99 |
return "Missing Instagram credentials"
|
100 |
|
101 |
-
# Create
|
102 |
download_path = os.path.join('reels', shortcode)
|
103 |
os.makedirs(download_path, exist_ok=True)
|
104 |
-
st.info(f"Download directory: {os.path.abspath(download_path)}")
|
105 |
|
106 |
-
#
|
107 |
st.info("Fetching post metadata...")
|
108 |
post = instaloader.Post.from_shortcode(loader.context, shortcode)
|
109 |
|
110 |
-
|
111 |
-
|
|
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
else:
|
118 |
-
return
|
119 |
|
120 |
except instaloader.exceptions.InstaloaderException as e:
|
121 |
return f"Instagram error: {str(e)}"
|
122 |
except Exception as e:
|
123 |
-
return f"Unexpected error: {str(e)}"
|
124 |
|
125 |
def main():
|
126 |
st.set_page_config(
|
@@ -130,17 +154,15 @@ def main():
|
|
130 |
|
131 |
st.title('📱 Instagram Reel Downloader')
|
132 |
|
|
|
|
|
|
|
|
|
|
|
133 |
# Input for Reel URL
|
134 |
url = st.text_input('Enter the Instagram Reel URL',
|
135 |
placeholder='https://www.instagram.com/reel/XXXXXXXXXXXX')
|
136 |
|
137 |
-
if url:
|
138 |
-
shortcode = clean_url(url)
|
139 |
-
if shortcode:
|
140 |
-
st.info(f"Detected shortcode: {shortcode}")
|
141 |
-
else:
|
142 |
-
st.warning("Could not detect valid shortcode from URL")
|
143 |
-
|
144 |
# Download button
|
145 |
if st.button('Download Reel', type='primary'):
|
146 |
if url:
|
@@ -150,9 +172,14 @@ def main():
|
|
150 |
|
151 |
# Show directory contents
|
152 |
if os.path.exists("reels"):
|
153 |
-
st.write("
|
154 |
-
for
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
156 |
else:
|
157 |
st.warning('Please enter a URL.')
|
158 |
|
|
|
3 |
import os
|
4 |
from dotenv import load_dotenv
|
5 |
import re
|
6 |
+
import requests
|
7 |
+
import shutil
|
8 |
|
9 |
# Load environment variables
|
10 |
load_dotenv()
|
|
|
30 |
match = re.search(pattern, cleaned_url)
|
31 |
if match:
|
32 |
return match.group(1)
|
33 |
+
|
34 |
# If patterns don't match, try getting the last segment
|
35 |
segments = cleaned_url.split('/')
|
36 |
if segments:
|
37 |
+
return segments[-1]
|
|
|
|
|
38 |
|
39 |
return None
|
40 |
except Exception as e:
|
41 |
st.error(f"Error cleaning URL: {str(e)}")
|
42 |
return None
|
43 |
|
44 |
+
def download_file(url, path):
|
45 |
+
"""Download file from URL with progress bar"""
|
46 |
+
try:
|
47 |
+
response = requests.get(url, stream=True)
|
48 |
+
response.raise_for_status()
|
49 |
+
|
50 |
+
# Get file size for progress bar
|
51 |
+
file_size = int(response.headers.get('content-length', 0))
|
52 |
+
|
53 |
+
# Create progress bar
|
54 |
+
progress_bar = st.progress(0)
|
55 |
+
|
56 |
+
# Download with progress updates
|
57 |
+
with open(path, 'wb') as f:
|
58 |
+
if file_size == 0:
|
59 |
+
f.write(response.content)
|
60 |
+
else:
|
61 |
+
downloaded = 0
|
62 |
+
for chunk in response.iter_content(chunk_size=8192):
|
63 |
+
if chunk:
|
64 |
+
f.write(chunk)
|
65 |
+
downloaded += len(chunk)
|
66 |
+
progress = int((downloaded / file_size) * 100)
|
67 |
+
progress_bar.progress(progress)
|
68 |
+
|
69 |
+
return True
|
70 |
+
except Exception as e:
|
71 |
+
st.error(f"Download error: {str(e)}")
|
72 |
+
return False
|
73 |
|
74 |
def download_reel(url):
|
75 |
"""Download Instagram reel"""
|
|
|
83 |
|
84 |
st.info(f"Extracted shortcode: {shortcode}")
|
85 |
|
86 |
+
# Initialize loader with debugging
|
87 |
loader = instaloader.Instaloader(
|
88 |
download_videos=True,
|
89 |
download_video_thumbnails=False,
|
|
|
102 |
else:
|
103 |
return "Missing Instagram credentials"
|
104 |
|
105 |
+
# Create download directory
|
106 |
download_path = os.path.join('reels', shortcode)
|
107 |
os.makedirs(download_path, exist_ok=True)
|
108 |
+
st.info(f"Download directory created: {os.path.abspath(download_path)}")
|
109 |
|
110 |
+
# Get post metadata
|
111 |
st.info("Fetching post metadata...")
|
112 |
post = instaloader.Post.from_shortcode(loader.context, shortcode)
|
113 |
|
114 |
+
# Debug post information
|
115 |
+
st.info(f"Post type: {post.typename}")
|
116 |
+
st.info(f"Is video: {post.is_video}")
|
117 |
|
118 |
+
if post.is_video:
|
119 |
+
# Get video URL
|
120 |
+
video_url = post.video_url
|
121 |
+
st.info("Video URL obtained")
|
122 |
+
|
123 |
+
# Download video file
|
124 |
+
video_path = os.path.join(download_path, f"{shortcode}.mp4")
|
125 |
+
st.info("Starting direct video download...")
|
126 |
+
|
127 |
+
if download_file(video_url, video_path):
|
128 |
+
# Verify file exists and has size
|
129 |
+
if os.path.exists(video_path):
|
130 |
+
size = os.path.getsize(video_path)
|
131 |
+
if size > 0:
|
132 |
+
return f"""Download successful!
|
133 |
+
Location: {video_path}
|
134 |
+
Size: {size/1024/1024:.2f} MB"""
|
135 |
+
else:
|
136 |
+
return "Download failed: File is empty"
|
137 |
+
else:
|
138 |
+
return "Download failed: File not created"
|
139 |
+
else:
|
140 |
+
return "Failed to download video file"
|
141 |
else:
|
142 |
+
return "Post is not a video"
|
143 |
|
144 |
except instaloader.exceptions.InstaloaderException as e:
|
145 |
return f"Instagram error: {str(e)}"
|
146 |
except Exception as e:
|
147 |
+
return f"Unexpected error: {str(e)}\nType: {type(e)}"
|
148 |
|
149 |
def main():
|
150 |
st.set_page_config(
|
|
|
154 |
|
155 |
st.title('📱 Instagram Reel Downloader')
|
156 |
|
157 |
+
# Show current working directory and permissions
|
158 |
+
st.info(f"Working directory: {os.getcwd()}")
|
159 |
+
if os.path.exists("reels"):
|
160 |
+
st.info(f"Reels directory permissions: {oct(os.stat('reels').st_mode)[-3:]}")
|
161 |
+
|
162 |
# Input for Reel URL
|
163 |
url = st.text_input('Enter the Instagram Reel URL',
|
164 |
placeholder='https://www.instagram.com/reel/XXXXXXXXXXXX')
|
165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
# Download button
|
167 |
if st.button('Download Reel', type='primary'):
|
168 |
if url:
|
|
|
172 |
|
173 |
# Show directory contents
|
174 |
if os.path.exists("reels"):
|
175 |
+
st.write("\nContents of reels directory:")
|
176 |
+
for root, dirs, files in os.walk("reels"):
|
177 |
+
level = root.replace("reels", "").count(os.sep)
|
178 |
+
indent = " " * 4 * level
|
179 |
+
st.write(f"{indent}{os.path.basename(root)}/")
|
180 |
+
subindent = " " * 4 * (level + 1)
|
181 |
+
for f in files:
|
182 |
+
st.write(f"{subindent}{f}")
|
183 |
else:
|
184 |
st.warning('Please enter a URL.')
|
185 |
|