-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreparator.py
More file actions
352 lines (316 loc) · 11.5 KB
/
preparator.py
File metadata and controls
352 lines (316 loc) · 11.5 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from selenium import webdriver;
from selenium.webdriver.common.keys import Keys;
from selenium.webdriver.common.by import By;
from selenium.webdriver.support.ui import WebDriverWait;
from selenium.webdriver.support import expected_conditions as EC;
from selenium.common.exceptions import TimeoutException;
from selenium.common.exceptions import WebDriverException;
from selenium.webdriver.chrome.options import Options as ChromeOptions;
from selenium.webdriver.firefox.options import Options as FirefoxOptions;
from bs4 import BeautifulSoup;
import common;
import time;
import math;
import datetime;
import platform;
import requests;
class StaticScroller:
def __init__(self, expected, xpath):
self.expected = expected;
self.xpath = xpath;
def __call__(self, driver):
driver.execute_script("window.scrollTo(0," + str(self.expected + 1) + ")");
cc = int(math.floor(driver.execute_script("return window.pageYOffset")));
ce = int(math.floor(self.expected));
return cc < ce and len(driver.find_elements_by_xpath(self.xpath)) > 0;
def determine_exec(drivertype):
if drivertype=="firefox":
driver_uri = "geckodriver/geckodriver";
elif drivertype=="chrome":
driver_uri = "chromedriver/chromedriver";
else:
raise RuntimeError("Unknown driver type " + drivertype);
os = platform.platform().lower();
arc = platform.machine().lower();
if os.find("windows") != -1:
if arc == "i386" or arc == "i686":
if drivertype=="chrome":
return driver_uri + "-win";
else:
return driver_uri + "-win-32";
else:
if drivertype=="chrome":
return driver_uri + "-win";
else:
return driver_uri + "-win-64";
elif os.find("linux") != -1:
if arc == "i386" or arc == "i686":
if drivertype=="chrome":
return driver_uri + "-linux";
else:
return driver_uri + "-linux-32";
else:
if drivertype=="chrome":
return driver_uri + "-linux";
else:
return driver_uri + "-linux-64";
elif os.find("mac") != -1:
return driver_uri + "-mac";
else:
return None;
def force_visit(driver, url):
try:
driver.set_page_load_timeout(10);
driver.get(url);
except (WebDriverException, TimeoutException) as e:
print("Reloading...");
def get_element(driver, bytype, key):
wait = WebDriverWait(driver, 10).until(EC.presence_of_element_located((bytype, key)));
return driver.find_element(bytype, key);
def get_elements(driver, bytype, key):
wait = WebDriverWait(driver, 10).until(EC.presence_of_element_located((bytype, key)));
return driver.find_elements(bytype, key);
def scroll_to_bottom(driver):
continuation_xpath = "//div[@id='continuations']/yt-next-continuation";
try:
while len(driver.find_elements_by_xpath(continuation_xpath)) > 0:
current_offset = driver.execute_script("return window.pageYOffset") + 1000;
driver.execute_script("window.scrollTo(0," + str(current_offset) + ")");
try:
wait = WebDriverWait(driver, 7).until_not(StaticScroller(current_offset, continuation_xpath));
except TimeoutException:
return False;
return True;
except Exception:
return False;
def visit_channel(driver, channel_name):
driver.get(common.URL);
searchBox = driver.find_element_by_name('search_query');
# searchBox.set_attribute("value", channel_name);
searchBox.send_keys(channel_name);
searchBox.send_keys(Keys.RETURN);
videosPage = False;
while videosPage == False:
try:
wait = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "channel-title")));
videosPage = True;
except TimeoutException:
print("Bad Internet or Username does not exist");
driver.close();
channelTitle = driver.find_element_by_id("channel-title");
channelTitle.click();
def open_channel_tab(driver, tabname):
try:
wait = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "tabsContent")));
except TimeoutException:
print("Reloading...");
driver.get(driver.current_url);
tabs = driver.find_elements_by_tag_name("paper-tab");
videoIndex = -1;
i = 0;
for tab in tabs:
if tab.text == tabname:
videoIndex = i;
break;
i = i + 1;
tabs[videoIndex].click();
def videos_scan_reloader(driver, url):
eLengths = driver.find_elements_by_xpath("//ytd-thumbnail-overlay-time-status-renderer/span");
videos = driver.find_elements_by_xpath("//h3/a[@id='video-title']");
sec_counter = 0;
finish = False;
while sec_counter < 10 and finish == False:
if len(eLengths) == len(videos):
finish = True;
else:
time.sleep(1);
eLengths = driver.find_elements_by_xpath("//ytd-thumbnail-overlay-time-status-renderer/span");
videos = driver.find_elements_by_xpath("//h3/a[@id='video-title']");
sec_counter += 1;
if sec_counter == 10:
return None;
return {"videos": videos, "lengths": eLengths};
def scan_videos_link(driver, url):
while True:
force_visit(driver, url);
if scroll_to_bottom(driver) == True:
break;
channel_name = driver.find_element_by_xpath("//h1[@id='channel-title-container']/span").text;
eAuthor = driver.find_elements_by_xpath("//div[@id='metadata']/div[@id='byline-container']/yt-formatted-string/a");
eLengths = driver.find_elements_by_xpath("//ytd-thumbnail-overlay-time-status-renderer/span");
data = videos_scan_reloader(driver, url);
if data == None:
print("Reloading...");
return scan_videos_link(driver, url);
videos = data["videos"];
eLengths = data["lengths"];
codes = [];
links = [];
lengths = [];
titles = [];
i = 0;
while i < len(videos):
length = eLengths[i];
video = videos[i];
title = video.text;
link = video.get_attribute("href");
if len(eAuthor) > 0:
true_author = eAuthor[0].text == channel_name;
else:
true_author = True;
if len(title) > 0 and len(link) > 0 and true_author:
code = str(link[link.find("=")+1:]);
if not code in codes:
codes.append(code);
links.append(link);
lengths.append(length.get_attribute("innerText").strip());
titles.append(title);
i += 1;
return {"links": links, "lengths": lengths, "codes": codes, "title": titles};
def collect_videos_link(driver, url, silent=False):
if silent:
headers = {"accept-language": "en-us"};
page = requests.get(url + "/videos", headers=headers);
parsed = BeautifulSoup(page.content, "html.parser");
menuitem = parsed.select('ul#browse-items-primary li.branded-page-v2-subnav-container ul[role="menu"] li[role="menuitem"] span');
categoryLinks = [];
for item in menuitem:
print("Appending category " + item.get_text());
categoryLinks.append("https://www.youtube.com" + item["href"]);
else:
open_channel_tab(driver, "VIDEOS");
category_xpath = "//div[@id='primary-items']/yt-dropdown-menu/paper-menu-button/iron-dropdown[@id='dropdown']/div/div/paper-listbox";
eLinks = get_elements(driver, By.XPATH, category_xpath + "/a");
divs = get_elements(driver, By.XPATH, category_xpath + "/a/paper-item/paper-item-body/div[contains(@class, 'item')]");
if len(eLinks) > 1:
categoryLinks = [];
for index in range(0, len(eLinks)):
name = divs[index].get_attribute("innerText");
link = eLinks[index].get_attribute("href");
if name != "All videos":
print("Appending category " + name);
categoryLinks.append(link);
categoryLinks = list(set(categoryLinks));
else:
categoryLinks = [eLinks[0].get_attribute("href")];
print("Found " + str(len(categoryLinks)) + " categories... ");
videos_codes = [];
videos_links = [];
videos_lengths = [];
for link in categoryLinks:
print("Collecting links from " + link);
res = scan_videos_link(driver, link);
for i in range(0, len(res["codes"])):
if res["codes"][i] not in videos_codes:
print("Appending " + res["title"][i] + " " + res["links"][i]);
videos_codes.append(res["codes"][i]);
videos_links.append(res["links"][i]);
if len(res["lengths"][i]) == 0:
raise RuntimeError("Blank length for " + res["title"][i] + " " + res["links"][i]);
videos_lengths.append(res["lengths"][i]);
return {"links": videos_links, "lengths": videos_lengths};
def get_channel_start_date(driver, url, silent=False):
datestr = None;
if silent:
headers = {"accept-language": "en-us"};
page = requests.get(url + "/about", headers=headers);
parsed = BeautifulSoup(page.content, "html.parser");
stats = parsed.select('ul#browse-items-primary li div.about-metadata-container div.about-stats span.about-stat');
for stat in stats:
if stat.get_text().find("Joined", 0, 6) != -1:
datestr = stat.get_text().split(" ");
else:
open_channel_tab(driver, "ABOUT");
date_xpath = "//div[@id='right-column']/yt-formatted-string[contains(@class, 'ytd-channel-about-metadata-renderer')]";
eDate = get_elements(driver, By.XPATH, date_xpath);
datestr = eDate[1].text.split(" ");
day = common.toInt(datestr[2]);
month = common.get_month(datestr[1]);
year = datestr[3];
return datetime.date(int(year), month, day);
def get_chrome_driver(silent):
options = ChromeOptions();
if silent:
options.add_argument("--mute-audio");
options.add_argument("--headless");
options.add_argument("--window-size=1366x768");
options.add_argument("--lang=en-us");
executable_path = determine_exec("chrome");
if executable_path == None:
print("Cannot determine operating system");
try:
return webdriver.chrome(chrome_options=options);
except Exception:
print("Cannot find chromedriver executable");
else:
return webdriver.Chrome(executable_path=executable_path, chrome_options=options);
def get_firefox_driver(silent):
profile = webdriver.FirefoxProfile();
profile.set_preference("intl.accept_languages", "en-us");
executable_path = determine_exec("firefox");
if silent:
options = FirefoxOptions();
options.add_argument("-headless");
if executable_path == None:
print("Cannot determine operating system");
try:
if silent:
return webdriver.Firefox(firefox_options=options, firefox_profile=profile);
else:
return webdriver.Firefox(firefox_profile=profile);
except Exception:
print("Cannot find geckodriver executable");
else:
if silent:
return webdriver.Firefox(firefox_options=options, executable_path=executable_path, firefox_profile=profile);
else:
return webdriver.Firefox(executable_path=executable_path, firefox_profile=profile);
def get_channel_url(channel_name):
page = requests.get("https://www.youtube.com/results?search_query=" + channel_name.replace(" ", "+"));
parsed = BeautifulSoup(page.content, "html.parser");
urls = parsed.select('div.yt-lockup-content h3.yt-lockup-title a');
for url in urls:
ls = 1;
le = url["href"].find("/", 1);
href = url["href"][ls:le];
if href=="user" or href=="channel":
return "http://www.youtube.com" + url["href"];
return None;
def open_browser(browser, silent):
print("Open browser...");
if browser != None:
if browser == "chrome":
return get_chrome_driver(silent);
elif browser == "firefox":
return get_firefox_driver(silent);
else:
raise RuntimeError("Unknown browser");
else:
try:
return get_chrome_driver(silent);
except Exception:
try:
return get_firefox_driver(silent);
except Exception as err:
print("Cannot use any browser.");
raise err;
def gather_channel_data(channel_name, browser=None, silent=False):
driver = open_browser(browser, silent);
print("Visit channel...");
if silent:
url = get_channel_url(channel_name);
if url == None:
print("Channel not found");
return None;
else:
visit_channel(driver, channel_name);
url = driver.current_url;
print("Get channel join date...");
date = get_channel_start_date(driver, url, silent);
print("Visit channel...");
if silent==False:
force_visit(driver, url);
print("Get videos data...");
videos_data = collect_videos_link(driver, url, silent);
driver.close();
return {"videos_data": videos_data, "date": date};