-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtt-backup.py
334 lines (285 loc) · 13.3 KB
/
tt-backup.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import os
import sys
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests
import json
from datetime import datetime
import subprocess
import re
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
def install_dependencies():
"""Install required dependencies"""
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", "yt-dlp"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "selenium"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])
except Exception as e:
print(f"Error installing dependencies: {str(e)}")
def setup_chrome_profile():
chrome_options = webdriver.ChromeOptions()
# Get the correct path for Windows
user_data_dir = os.path.join(os.environ['LOCALAPPDATA'], 'Google', 'Chrome', 'User Data')
# Add necessary options to prevent crashes and detection
chrome_options.add_argument(f'--user-data-dir={user_data_dir}')
chrome_options.add_argument('--profile-directory=Default')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--remote-debugging-port=9222')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging', 'enable-automation'])
chrome_options.add_experimental_option('useAutomationExtension', False)
# Close any existing Chrome instances
os.system("taskkill /f /im chrome.exe")
time.sleep(2)
try:
driver = webdriver.Chrome(options=chrome_options)
# Mask selenium's presence
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
return driver
except Exception as e:
print(f"Error initializing Chrome with profile: {str(e)}")
print("\nTrying alternative method without user profile...")
try:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(options=chrome_options)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
return driver
except Exception as e:
print(f"Error with alternative method: {str(e)}")
sys.exit("Could not initialize Chrome. Please make sure Chrome is installed.")
def handle_empty_directory(directory, message="No content was found to scrape for this section."):
"""Create an explanation file in empty directories"""
if os.path.exists(directory) and not os.listdir(directory):
with open(os.path.join(directory, "Nothing to Scrape.txt"), 'w', encoding='utf-8') as f:
f.write(message)
def create_backup_structure(username):
"""Create the backup directory structure and handle empty folders"""
# Format the date
date_str = datetime.now().strftime("%B %d")
day = int(datetime.now().strftime("%d"))
if 10 <= day % 100 <= 20:
suffix = 'th'
else:
suffix = {1: 'st', 2: 'nd', 3: 'rd'}.get(day % 10, 'th')
date_str = f"{date_str}{suffix}-{datetime.now().strftime('%Y')}"
# Clean up username
clean_username = username.replace('https://www.tiktok.com/', '').replace('/', '')
# Create backups directory in script directory
script_dir = os.path.dirname(os.path.abspath(__file__))
backups_dir = os.path.join(script_dir, "backups")
os.makedirs(backups_dir, exist_ok=True)
# Create user backup directory inside backups folder
base_dir = os.path.join(backups_dir, f"@{clean_username}_{date_str}")
# Create directory structure
directories = {
"01_profile": {
"path": os.path.join(base_dir, "01_profile"),
"subdirs": ["01_avatar", "02_bio", "03_stats"],
"message": "No profile information was found to scrape."
},
"02_pinned_videos": {
"path": os.path.join(base_dir, "02_pinned_videos"),
"message": "No pinned videos were found on this profile."
},
"03_playlists": {
"path": os.path.join(base_dir, "03_playlists"),
"message": "No playlists were found on this profile."
},
"04_videos": {
"path": os.path.join(base_dir, "04_videos"),
"message": "No videos were found on this profile."
},
"05_reposts": {
"path": os.path.join(base_dir, "05_reposts"),
"message": "No reposts were found on this profile."
},
"06_favorites": {
"path": os.path.join(base_dir, "06_favorites"),
"message": "No favorites were found on this profile."
},
"07_liked": {
"path": os.path.join(base_dir, "07_liked"),
"message": "No liked videos were found on this profile."
},
"08_html_snapshot": {
"path": os.path.join(base_dir, "08_html_snapshot"),
"message": "No HTML snapshot was created for this profile."
}
}
# Create directories
for dir_info in directories.values():
os.makedirs(dir_info["path"], exist_ok=True)
if "subdirs" in dir_info:
for subdir in dir_info["subdirs"]:
os.makedirs(os.path.join(dir_info["path"], subdir), exist_ok=True)
handle_empty_directory(dir_info["path"], dir_info["message"])
return base_dir
def handle_tiktok_page_load(driver, url):
try:
# Initial page load
driver.get(url)
time.sleep(3) # Wait for initial load
# Refresh the page to bypass automation detection
driver.refresh()
time.sleep(3) # Wait after refresh
# Wait for body element to be present
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "body"))
)
# Additional wait for content to load
time.sleep(2)
return True
except Exception as e:
print(f"Error loading page: {str(e)}")
return False
def scrape_profile_info(driver, base_dir):
print("\nScraping profile information...")
try:
# Wait longer for the page to fully load
time.sleep(5)
# Get profile information using updated selectors
try:
# Get bio using JavaScript to get the full text content
bio = driver.execute_script("""
return document.querySelector('h2[data-e2e="user-bio"]').textContent
|| document.querySelector('h2[data-e2e="user-subtitle"]').textContent
""")
if not bio:
bio = "No bio found"
except:
bio = "No bio found"
try:
following = driver.find_element(By.CSS_SELECTOR, "strong[data-e2e='following-count']").text
except:
following = "0"
try:
followers = driver.find_element(By.CSS_SELECTOR, "strong[data-e2e='followers-count']").text
except:
followers = "0"
try:
likes = driver.find_element(By.CSS_SELECTOR, "strong[data-e2e='likes-count']").text
except:
likes = "0"
try:
website = driver.find_element(By.CSS_SELECTOR, "a[data-e2e='user-link']").get_attribute('href')
except:
website = "No website found"
# Save bio and stats
bio_path = os.path.join(base_dir, "01_profile", "02_bio", "bio.txt")
with open(bio_path, 'w', encoding='utf-8') as f:
f.write(f"{following}\nFollowing\n{followers}\nFollowers\n{likes}\nLikes\n{bio}\n{website}")
stats_path = os.path.join(base_dir, "01_profile", "03_stats", "stats.txt")
with open(stats_path, 'w', encoding='utf-8') as f:
f.write(f"Following: {following}\nFollowers: {followers}\nLikes: {likes}")
# Download avatar
try:
avatar_url = driver.execute_script("""
const header = document.querySelector('[class*="ShareLayoutHeader"]');
if (header) {
const avatar = header.querySelector('img[class*="ImgAvatar"]');
return avatar ? avatar.src : null;
}
return null;
""")
if avatar_url:
avatar_path = os.path.join(base_dir, "01_profile", "01_avatar", "avatar.jpeg")
response = requests.get(avatar_url, headers={
'User-Agent': 'Mozilla/5.0',
'Referer': 'https://www.tiktok.com/'
})
if response.status_code == 200:
with open(avatar_path, 'wb') as f:
f.write(response.content)
except Exception as e:
print(f"Warning: Could not download avatar: {str(e)}")
return True
except Exception as e:
print(f"Error scraping profile information: {str(e)}")
return False
def scrape_pinned_videos(driver, base_dir):
print("\nScraping pinned videos...")
try:
# Wait for video grid to load
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "[data-e2e='user-post-item']"))
)
# Get first 3 videos (pinned)
video_elements = driver.find_elements(By.CSS_SELECTOR, "[data-e2e='user-post-item']")[:3]
for idx, video in enumerate(video_elements, 1):
try:
video_link = video.find_element(By.CSS_SELECTOR, "a").get_attribute("href")
video_path = os.path.join(base_dir, "02_pinned_videos", f"pinned_{idx}")
os.makedirs(video_path, exist_ok=True)
try:
subprocess.run([
'yt-dlp',
'--no-warnings',
'--quiet',
'-o', os.path.join(video_path, "video.mp4"),
video_link
], check=True)
except Exception as e:
print(f"Error downloading pinned video {idx}: {str(e)}")
continue
except Exception as e:
print(f"Error processing pinned video {idx}: {str(e)}")
continue
return True
except Exception as e:
print(f"Error scraping pinned videos: {str(e)}")
return False
def scrape_videos(driver, base_dir):
print("\nScraping videos...")
try:
# Wait for video grid to load
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "[data-e2e='user-post-item']"))
)
# Scroll to load all videos
print("Loading all videos...")
last_height = driver.execute_script("return document.documentElement.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
time.sleep(2)
new_height = driver.execute_script("return document.documentElement.scrollHeight")
if new_height == last_height:
break
last_height = new_height
# Get all video elements
video_elements = driver.find_elements(By.CSS_SELECTOR, "[data-e2e='user-post-item']")
for idx, video in enumerate(video_elements, 1):
try:
video_link = video.find_element(By.CSS_SELECTOR, "a").get_attribute("href")
video_path = os.path.join(base_dir, "04_videos", f"video_{idx}")
os.makedirs(video_path, exist_ok=True)
try:
subprocess.run([
'yt-dlp',
'--no-warnings',
'--quiet',
'-o', os.path.join(video_path, "video.mp4"),
video_link
], check=True)
except Exception as e:
print(f"Error downloading video {idx}: {str(e)}")
continue
except Exception as e:
print(f"Error processing video {idx}: {str(e)}")
continue
return True
except Exception as e:
print(f"Error scraping videos: {str(e)}")
return False
if __name__ == "__main__":
print("This module is meant to be imported, not run directly.")