import tempfile import os from pathlib import Path from instaloader import Instaloader, Profile from dataclasses import dataclass, field from typing import List, Dict, Tuple import json import zipfile import io import fal_client @dataclass class InstagramData: profile_info: Dict posts: List[Dict] = field(default_factory=list) image_urls: List[str] = field(default_factory=list) def public_instagram_loader(username: str) -> InstagramData: with tempfile.TemporaryDirectory() as temp_dir: profile_dir = Path(temp_dir) / username profile_dir.mkdir(parents=True, exist_ok=True) L = Instaloader( download_pictures=True, download_videos=False, download_video_thumbnails=False, download_geotags=False, download_comments=False, save_metadata=False, compress_json=False, dirname_pattern=str(profile_dir) ) try: profile = Profile.from_username(L.context, username) except Exception: return None profile_info = { 'username': profile.username, 'full_name': profile.full_name, 'biography': profile.biography, 'followers': profile.followers, 'followees': profile.followees, 'mediacount': profile.mediacount, } instagram_data = InstagramData(profile_info=profile_info) print(f"Downloading and uploading posts from {username}") for post in profile.get_posts(): if post.typename == 'GraphImage': post_data = { 'shortcode': post.shortcode, 'caption': post.caption, 'date': post.date_local, 'likes': post.likes, 'filename': f"{post.date_utc:%Y-%m-%d_%H-%M-%S}_UTC.jpg", } # Download the image image_path = profile_dir / post_data['filename'] L.download_pic(filename=str(image_path), url=post.url, mtime=post.date_utc) # Upload the image to FAL with open(image_path, 'rb') as img_file: url = fal_client.upload(img_file.read(), "image/jpeg") instagram_data.posts.append(post_data) instagram_data.image_urls.append(url) print(f"Data and images uploaded for {username}") return instagram_data if __name__ == "__main__": username = input("Enter Instagram username: ") data = public_instagram_loader(username) if data: print(f"Collected data for {data.profile_info['username']}:") print(f"Total posts: {len(data.posts)}") print(f"Zip file size: {len(data.zip_file)} bytes") else: print("Profile not found or an error occurred.")