Spaces:
Sleeping
Sleeping
gradio
Browse files
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import zipfile
|
4 |
+
import instaloader
|
5 |
+
from selenium import webdriver
|
6 |
+
from selenium.webdriver.common.by import By
|
7 |
+
from selenium.webdriver.common.keys import Keys
|
8 |
+
import time
|
9 |
+
|
10 |
+
# Function to log in to Instagram using Selenium
|
11 |
+
def instagram_login(username, password):
|
12 |
+
chrome_options = webdriver.ChromeOptions()
|
13 |
+
chrome_options.add_argument("--headless") # Uncomment this to run in headless mode
|
14 |
+
driver = webdriver.Chrome(options=chrome_options)
|
15 |
+
driver.get("https://www.instagram.com/accounts/login/")
|
16 |
+
time.sleep(3)
|
17 |
+
|
18 |
+
# Locate and input username and password
|
19 |
+
username_input = driver.find_element(By.NAME, "username")
|
20 |
+
password_input = driver.find_element(By.NAME, "password")
|
21 |
+
|
22 |
+
username_input.send_keys(username)
|
23 |
+
password_input.send_keys(password)
|
24 |
+
|
25 |
+
# Submit login form
|
26 |
+
password_input.send_keys(Keys.RETURN)
|
27 |
+
time.sleep(5) # Adjust sleep time as needed
|
28 |
+
|
29 |
+
return driver
|
30 |
+
|
31 |
+
# Function to get posts, followers, and following using Instaloader
|
32 |
+
def get_instagram_data(username, password, folder_name):
|
33 |
+
# Log in to Instagram with Selenium
|
34 |
+
driver = instagram_login(username, password)
|
35 |
+
|
36 |
+
# Set up Instaloader
|
37 |
+
L = instaloader.Instaloader(dirname_pattern=folder_name + "/{target}")
|
38 |
+
L.login(username, password)
|
39 |
+
|
40 |
+
# Fetch profile data
|
41 |
+
profile = instaloader.Profile.from_username(L.context, username)
|
42 |
+
|
43 |
+
# Create directory if not exists
|
44 |
+
if not os.path.exists(folder_name):
|
45 |
+
os.makedirs(folder_name)
|
46 |
+
|
47 |
+
# Get posts data
|
48 |
+
posts_data = []
|
49 |
+
for post in profile.get_posts():
|
50 |
+
post_data = {
|
51 |
+
'caption': post.caption,
|
52 |
+
'likes': post.likes,
|
53 |
+
'comments': post.comments,
|
54 |
+
'date': post.date,
|
55 |
+
'url': post.url,
|
56 |
+
'video': post.is_video
|
57 |
+
}
|
58 |
+
posts_data.append(post_data)
|
59 |
+
# Download post image/video
|
60 |
+
L.download_post(post, target=username)
|
61 |
+
|
62 |
+
# Get followers
|
63 |
+
followers = [follower.username for follower in profile.get_followers()]
|
64 |
+
|
65 |
+
# Get following
|
66 |
+
following = [followee.username for followee in profile.get_followees()]
|
67 |
+
|
68 |
+
# Save posts data, followers, and following into text files
|
69 |
+
with open(os.path.join(folder_name, 'posts_data.txt'), 'w') as file:
|
70 |
+
for i, post in enumerate(posts_data, 1):
|
71 |
+
file.write(f"Post {i}\n")
|
72 |
+
for key, value in post.items():
|
73 |
+
file.write(f"{key.capitalize()}: {value}\n")
|
74 |
+
file.write("\n")
|
75 |
+
|
76 |
+
with open(os.path.join(folder_name, 'followers.txt'), 'w') as file:
|
77 |
+
for follower in followers:
|
78 |
+
file.write(f"{follower}\n")
|
79 |
+
|
80 |
+
with open(os.path.join(folder_name, 'following.txt'), 'w') as file:
|
81 |
+
for followee in following:
|
82 |
+
file.write(f"{followee}\n")
|
83 |
+
|
84 |
+
# Create a ZIP file
|
85 |
+
zip_filename = f"{folder_name}.zip"
|
86 |
+
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
87 |
+
for root, dirs, files in os.walk(folder_name):
|
88 |
+
for file in files:
|
89 |
+
zipf.write(os.path.join(root, file),
|
90 |
+
arcname=os.path.relpath(os.path.join(root, file),
|
91 |
+
os.path.join(folder_name, '..')))
|
92 |
+
|
93 |
+
driver.quit()
|
94 |
+
|
95 |
+
return zip_filename
|
96 |
+
|
97 |
+
# Gradio Interface
|
98 |
+
def main(username, password, folder_name):
|
99 |
+
if username and password:
|
100 |
+
zip_file = get_instagram_data(username, password, folder_name)
|
101 |
+
success_message = f"Data saved and compressed into ZIP file: `{zip_file}`"
|
102 |
+
return zip_file, success_message
|
103 |
+
else:
|
104 |
+
return None, "Please enter both username and password."
|
105 |
+
|
106 |
+
# Create Gradio interface
|
107 |
+
iface = gr.Interface(
|
108 |
+
fn=main,
|
109 |
+
inputs=[
|
110 |
+
gr.Textbox(label="Instagram Username"),
|
111 |
+
gr.Textbox(label="Instagram Password", type="password"),
|
112 |
+
gr.Textbox(label="Folder Name to Save Data", value="instagram_data")
|
113 |
+
],
|
114 |
+
outputs=["file", "text"],
|
115 |
+
title="Snap Crawler",
|
116 |
+
description="Extracts posts, followers, and following from an Instagram account and saves them in a ZIP file."
|
117 |
+
)
|
118 |
+
|
119 |
+
if __name__ == "__main__":
|
120 |
+
iface.launch()
|