-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
143 lines (121 loc) · 5.64 KB
/
main.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import requests
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from datetime import datetime
import time
def get_instagram_stories(username, download_folder):
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.get("https://fastdl.app/")
try:
print("Waiting for cookies button..")
cookies_button = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, "//button[@aria-label='Consent']"))
)
cookies_button.click()
print("Cookies button clicked.")
print("Waiting for username input..")
url_input = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, "//input[@id='search-form-input']"))
)
url_input.send_keys(f"{username}")
print("Username entered.")
print("Waiting for download button..")
download_button = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, "//button[@class='search-form__button']"))
)
download_button.click()
print("Download button clicked.")
try:
print("Removing popup ad..")
popup_element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.CLASS_NAME, "ads-modal"))
)
driver.execute_script("""
var element = arguments[0];
element.parentNode.removeChild(element);
""", popup_element)
print("Popup ad removed.")
except Exception as e:
print("No popup ad detected or an error occurred:", e)
while True:
try:
see_more_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//button[@class='button button--see-more profile-media-list__button--see-more']"))
)
see_more_button.click()
print("See more button clicked.")
time.sleep(2)
except:
print("No more 'See more' buttons.")
break
print("Waiting for download buttons to become clickable..")
download_buttons = WebDriverWait(driver, 20).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, "a.button--filled"))
)
print(f"Found {len(download_buttons)} download buttons.")
for index, download_button in enumerate(download_buttons):
download_url = download_button.get_attribute("href")
response = requests.head(download_url)
content_type = response.headers.get('content-type')
if 'image' in content_type:
extension = "jpg"
elif 'video' in content_type:
extension = "mp4"
else:
print(f"Unknown content type: {content_type}. Skipping download.")
continue
download_date = datetime.now().strftime("%d%m%Y")
filename = os.path.join(download_folder, "Stories", f"LupusDownloader_{username}_{download_date}_{index + 1}.{extension}")
if not is_already_downloaded(download_url, download_folder):
print(f"Downloading {download_url} to {filename}..")
response = requests.get(download_url)
with open(filename, 'wb') as file:
file.write(response.content)
record_downloaded_link(download_url, download_folder)
print(f"Downloaded {download_url}.")
else:
print(f"Link already downloaded: {download_url}")
except Exception as e:
print("An error occurred:", e)
return False
finally:
driver.quit()
def is_already_downloaded(download_url, download_folder):
downloaded_links_file = os.path.join(download_folder, "downloaded_links.txt")
if os.path.exists(downloaded_links_file):
with open(downloaded_links_file, "r") as f:
existing_links = [line.strip().split('&')[0] for line in f.readlines()]
return download_url.split('&')[0] in existing_links
return False
def record_downloaded_link(download_url, download_folder):
downloaded_links_file = os.path.join(download_folder, "downloaded_links.txt")
with open(downloaded_links_file, "a") as f:
f.write(download_url + "\n")
def run_script():
######################################################################
# Folder
download_folder = r"(YOUR FOLDER PATH)"
# Example = r"C:\Users\PC\Downloads\InstaDownloader\"
# Example = r"/home/kali/Downloads/InstaDownloader/"
# User id
username = "jiri_mdf"
######################################################################
stories_folder = os.path.join(download_folder, "Stories")
if not os.path.exists(stories_folder):
os.makedirs(stories_folder)
downloaded_links_file = os.path.join(download_folder, "downloaded_links.txt")
if not os.path.exists(downloaded_links_file):
with open(downloaded_links_file, "w"):
pass
task_file_path = os.path.join(download_folder, 'task.txt')
with open(task_file_path, 'a') as task_file:
task_file.write(f'{datetime.now()} - The script ran\n')
if not get_instagram_stories(username, download_folder):
return False
run_script()