-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathins.py
More file actions
128 lines (106 loc) · 3.98 KB
/
ins.py
File metadata and controls
128 lines (106 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import streamlit as st
import instaloader
import os
from pathlib import Path
import uuid
import shutil
# Set page configuration
st.set_page_config(page_title="Instagram Reel Downloader(Lavgram)", page_icon="📸", layout="centered")
# Initialize Instaloader
@st.cache_resource
def get_instaloader():
return instaloader.Instaloader(
download_videos=True,
save_metadata=False,
compress_json=False,
request_timeout=60
)
L = get_instaloader()
# Main content
st.title("📸 Instagram Reel Downloader(Lavgram)")
st.markdown("Download Instagram reels directly from public links")
# Input form
post_url = st.text_input("Reel URL",
placeholder="e.g., https://www.instagram.com/reel/C1234567890/",
help="Enter the full URL of the reel")
def download_reel(post_url):
try:
# Create temp directory
temp_dir = f"temp_reel_{uuid.uuid4().hex}"
os.makedirs(temp_dir, exist_ok=True)
with st.spinner("Downloading reel..."):
# Extract shortcode from URL
shortcode = post_url.split("/")[-2]
# Get the post
post = instaloader.Post.from_shortcode(L.context, shortcode)
# Download the reel
L.download_post(post, target=temp_dir)
# Find downloaded files
downloaded_files = []
for file in Path(temp_dir).glob("*.*"):
if file.suffix.lower() in [".mp4", ".jpg", ".jpeg", ".png"]:
downloaded_files.append(str(file))
return downloaded_files, temp_dir
except Exception as e:
if 'temp_dir' in locals() and os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
raise e
# Session state management
if "downloaded_files" not in st.session_state:
st.session_state.downloaded_files = []
if "temp_dirs" not in st.session_state:
st.session_state.temp_dirs = []
# Clean up previous temp directories
for temp_dir in st.session_state.temp_dirs:
try:
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
except:
pass
st.session_state.temp_dirs = []
# Download button
if st.button("Download Reel", type="primary"):
st.session_state.downloaded_files = []
try:
if post_url:
downloaded_files, temp_dir = download_reel(post_url)
if downloaded_files:
st.session_state.downloaded_files = downloaded_files
st.session_state.temp_dirs.append(temp_dir)
st.success("✅ Reel downloaded successfully!")
st.balloons()
else:
st.warning("Could not download the reel")
else:
st.warning("Please enter a valid reel URL")
except Exception as e:
st.error(f"Download failed: {str(e)}")
# Display downloaded media
if st.session_state.downloaded_files:
st.subheader("Download Options")
for file_path in st.session_state.downloaded_files:
try:
file_name = os.path.basename(file_path)
file_ext = os.path.splitext(file_name)[1].lower()
# Only show preview for video files
if file_ext == ".mp4":
st.video(file_path)
# Download button for all file types
with open(file_path, "rb") as f:
st.download_button(
label=f"Download {file_name}",
data=f,
file_name=file_name,
mime="video/mp4" if file_ext == ".mp4" else "image/jpeg",
key=f"dl_{uuid.uuid4().hex}"
)
except Exception as e:
st.error(f"Error processing file: {str(e)}")
# Footer
st.markdown("---")
st.markdown("""
<div style="text-align: center;">
Developed by Lav Kush |
<a href="https://lav-developer.netlify.app" target="_blank">Portfolio</a>
</div>
""", unsafe_allow_html=True)