import time import streamlit as st from instagrapi import Client import requests from pathlib import Path import os from moviepy.editor import VideoFileClip from PIL import Image # Your Instagram credentials username = "droidcv1" password = "9TanFzU5ZLdUX#5" # Initialize the client cl = Client() # Add a delay before login time.sleep(5) # Login to Instagram try: cl.login(username, password) except Exception as e: st.error(f'Login failed: {e}') st.stop() # Function to download video def download_video(url, filename): response = requests.get(url, stream=True) if response.status_code == 200: with open(filename, 'wb') as f: for chunk in response.iter_content(1024): f.write(chunk) return filename # Function to generate thumbnail def generate_thumbnail(video_path): clip = VideoFileClip(str(video_path)) frame = clip.get_frame(1) # Get a frame at 1 second thumbnail_path = str(video_path) + ".jpg" im = Image.fromarray(frame) im.save(thumbnail_path) return thumbnail_path # Function to upload video and return the reel URL def upload_video(video_path, caption): thumbnail_path = generate_thumbnail(video_path) media = cl.clip_upload(video_path, caption=caption, thumbnail=thumbnail_path) return f"https://www.instagram.com/reel/{media.pk}/" # Directory to save downloaded videos download_dir = Path("downloads") download_dir.mkdir(exist_ok=True) st.title('Instagram Reels Uploader') urls = st.text_area('Enter video URLs (one per line):') if st.button('Upload Videos'): if urls: video_urls = urls.splitlines() for idx, video_url in enumerate(video_urls, start=1): with st.spinner(f'Downloading video {idx}...'): video_path = download_video(video_url, download_dir / f"video_{idx}.mp4") caption = f"Sample video {idx}" with st.spinner(f'Uploading video {idx}...'): try: reel_url = upload_video(video_path, caption) st.success(f'Uploaded video {idx} URL: {reel_url}') except Exception as e: st.error(f'Failed to upload video {idx}: {e}') os.remove(video_path) os.remove(video_path.with_suffix('.mp4.jpg')) else: st.warning('Please enter at least one video URL.')