sh20raj commited on
Commit
7cd5418
·
1 Parent(s): 2756a35

chore: Refactor Instagram Reels Uploader code

Browse files
Files changed (2) hide show
  1. app copy.py +0 -75
  2. app.py +73 -2
app copy.py DELETED
@@ -1,75 +0,0 @@
1
- import time
2
- import streamlit as st
3
- from instagrapi import Client
4
- import requests
5
- from pathlib import Path
6
- import os
7
- from moviepy.editor import VideoFileClip
8
- from PIL import Image
9
-
10
- # Your Instagram credentials
11
- username = "droidcv1"
12
- password = ";ya_#GkM,KND6?$"
13
-
14
- # Initialize the client
15
- cl = Client()
16
-
17
- # Add a delay before login
18
- time.sleep(5)
19
-
20
- # Login to Instagram
21
- try:
22
- cl.login(username, password)
23
- except Exception as e:
24
- st.error(f'Login failed: {e}')
25
- st.stop()
26
-
27
- # Function to download video
28
- def download_video(url, filename):
29
- response = requests.get(url, stream=True)
30
- if response.status_code == 200:
31
- with open(filename, 'wb') as f:
32
- for chunk in response.iter_content(1024):
33
- f.write(chunk)
34
- return filename
35
-
36
- # Function to generate thumbnail
37
- def generate_thumbnail(video_path):
38
- clip = VideoFileClip(str(video_path))
39
- frame = clip.get_frame(1) # Get a frame at 1 second
40
- thumbnail_path = str(video_path) + ".jpg"
41
- im = Image.fromarray(frame)
42
- im.save(thumbnail_path)
43
- return thumbnail_path
44
-
45
- # Function to upload video and return the reel URL
46
- def upload_video(video_path, caption):
47
- thumbnail_path = generate_thumbnail(video_path)
48
- media = cl.clip_upload(video_path, caption=caption, thumbnail=thumbnail_path)
49
- return f"https://www.instagram.com/reel/{media.pk}/"
50
-
51
- # Directory to save downloaded videos
52
- download_dir = Path("downloads")
53
- download_dir.mkdir(exist_ok=True)
54
-
55
- st.title('Instagram Reels Uploader')
56
-
57
- urls = st.text_area('Enter video URLs (one per line):')
58
-
59
- if st.button('Upload Videos'):
60
- if urls:
61
- video_urls = urls.splitlines()
62
- for idx, video_url in enumerate(video_urls, start=1):
63
- with st.spinner(f'Downloading video {idx}...'):
64
- video_path = download_video(video_url, download_dir / f"video_{idx}.mp4")
65
- caption = f"Sample video {idx}"
66
- with st.spinner(f'Uploading video {idx}...'):
67
- try:
68
- reel_url = upload_video(video_path, caption)
69
- st.success(f'Uploaded video {idx} URL: {reel_url}')
70
- except Exception as e:
71
- st.error(f'Failed to upload video {idx}: {e}')
72
- os.remove(video_path)
73
- os.remove(video_path.with_suffix('.mp4.jpg'))
74
- else:
75
- st.warning('Please enter at least one video URL.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py CHANGED
@@ -1,4 +1,75 @@
 
1
  import streamlit as st
 
 
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
  import streamlit as st
3
+ from instagrapi import Client
4
+ import requests
5
+ from pathlib import Path
6
+ import os
7
+ from moviepy.editor import VideoFileClip
8
+ from PIL import Image
9
 
10
+ # Your Instagram credentials
11
+ username = "droidcv1"
12
+ password = ";ya_#GkM,KND6?$"
13
+
14
+ # Initialize the client
15
+ cl = Client()
16
+
17
+ # Add a delay before login
18
+ time.sleep(5)
19
+
20
+ # Login to Instagram
21
+ try:
22
+ cl.login(username, password)
23
+ except Exception as e:
24
+ st.error(f'Login failed: {e}')
25
+ st.stop()
26
+
27
+ # Function to download video
28
+ def download_video(url, filename):
29
+ response = requests.get(url, stream=True)
30
+ if response.status_code == 200:
31
+ with open(filename, 'wb') as f:
32
+ for chunk in response.iter_content(1024):
33
+ f.write(chunk)
34
+ return filename
35
+
36
+ # Function to generate thumbnail
37
+ def generate_thumbnail(video_path):
38
+ clip = VideoFileClip(str(video_path))
39
+ frame = clip.get_frame(1) # Get a frame at 1 second
40
+ thumbnail_path = str(video_path) + ".jpg"
41
+ im = Image.fromarray(frame)
42
+ im.save(thumbnail_path)
43
+ return thumbnail_path
44
+
45
+ # Function to upload video and return the reel URL
46
+ def upload_video(video_path, caption):
47
+ thumbnail_path = generate_thumbnail(video_path)
48
+ media = cl.clip_upload(video_path, caption=caption, thumbnail=thumbnail_path)
49
+ return f"https://www.instagram.com/reel/{media.pk}/"
50
+
51
+ # Directory to save downloaded videos
52
+ download_dir = Path("downloads")
53
+ download_dir.mkdir(exist_ok=True)
54
+
55
+ st.title('Instagram Reels Uploader')
56
+
57
+ urls = st.text_area('Enter video URLs (one per line):')
58
+
59
+ if st.button('Upload Videos'):
60
+ if urls:
61
+ video_urls = urls.splitlines()
62
+ for idx, video_url in enumerate(video_urls, start=1):
63
+ with st.spinner(f'Downloading video {idx}...'):
64
+ video_path = download_video(video_url, download_dir / f"video_{idx}.mp4")
65
+ caption = f"Sample video {idx}"
66
+ with st.spinner(f'Uploading video {idx}...'):
67
+ try:
68
+ reel_url = upload_video(video_path, caption)
69
+ st.success(f'Uploaded video {idx} URL: {reel_url}')
70
+ except Exception as e:
71
+ st.error(f'Failed to upload video {idx}: {e}')
72
+ os.remove(video_path)
73
+ os.remove(video_path.with_suffix('.mp4.jpg'))
74
+ else:
75
+ st.warning('Please enter at least one video URL.')