-
Notifications
You must be signed in to change notification settings - Fork 1
/
scraper.py
67 lines (56 loc) · 2.64 KB
/
scraper.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
import os
import sys
import json
import requests
import logging
import urllib.parse as urlparse
# logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.INFO, format='%(message)s')
def scraper(filepath):
with open(filepath) as file:
urls = file.read().split("\n")
print("Urls provided = ", urls)
links_api = "http://localhost:3000/api/"
comments_url = "http://ytcomments.klostermann.ca/scrape"
comments_api = "http://ytcomments.klostermann.ca/api"
def write_comments(comments, index, video_id):
for obj in comments:
with open("./data/{}/{}.csv".format(index, video_id), 'a') as file:
file.write("\"{}\"\n".format(obj['commentText']))
try:
for index, main_url in enumerate(urls):
path = "./data/{}".format(index)
logging.info("Creating directories")
if not os.path.exists(path):
os.makedirs(path)
r1 = requests.get(links_api, params = {'url' : main_url})
for obj in r1.json():
link = obj['link']
# print(link)
parsed = urlparse.urlparse(link)
video_id = urlparse.parse_qs(parsed.query)['v'][0]
# print(video_id)
with open("./data/{}/{}.csv".format(index, video_id), 'w') as file:
file.write("Comments, Flag\n")
logging.info("Getting comments of videoId = {}".format(video_id))
with requests.Session() as s:
req1 = s.post(comments_url, data = {'yt-url' : link, 'videoID' : video_id})
req2 = s.post(comments_api, data = {'videoID' : video_id})
initial_res = json.loads(req2.text)
initial_comments = initial_res['comments']
write_comments(initial_comments, index, video_id)
next_page_token = initial_res['nextPageToken']
logging.info("Writing comments of videoId = {}".format(video_id))
while(next_page_token != None):
req_pag = s.post(comments_api, data = {'videoID' : video_id, 'pageToken': next_page_token})
pag_res = json.loads(req_pag.text)
comments_pag = pag_res['comments']
write_comments(comments_pag, index, video_id)
try:
next_page_token = pag_res['nextPageToken']
except Exception as e:
break
except Exception as e:
logging.info(e)
if __name__ == "__main__":
scraper(sys.argv[1])