-
Notifications
You must be signed in to change notification settings - Fork 0
/
YTscrap.py
126 lines (97 loc) · 3.41 KB
/
YTscrap.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
import os
import json
from dotenv import load_dotenv
import googleapiclient.discovery
load_dotenv()
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
DEVELOPER_KEY = os.getenv('YOUTUBE_API_KEY')
YOUTUBE = googleapiclient.discovery.build(
api_service_name, api_version, developerKey = DEVELOPER_KEY)
MAX_COMMENT_RESULTS = 100
MAX_VIDEOS_RESULTS = 50
def reqPlaylistVideos(playlistId):
request = YOUTUBE.playlistItems().list(
part="contentDetails",
maxResults=MAX_VIDEOS_RESULTS,
playlistId=playlistId
)
response = request.execute()
videos = []
for item in response['items']:
videoId = item['contentDetails']['videoId']
videos.append(videoId)
return videos
def reqCommentThreads(videoId, pageToken=None):
request = YOUTUBE.commentThreads().list(
part="snippet,replies",
maxResults=MAX_COMMENT_RESULTS,
textFormat="plainText",
videoId=videoId,
pageToken = pageToken,
)
response = request.execute()
return response
def filterJSON(response):
items = response['items']
comments_data = []
for item in items:
try:
comment = item['snippet']['topLevelComment']['snippet']['textDisplay']
comment = comment.replace('\n', ' ')
comments_data.append(comment)
if 'replies' in item:
comment_replies= item['replies']['comments']
for reply in comment_replies:
comment_reply = reply['snippet']['textDisplay']
comment_reply = comment_reply.replace('\n', ' ')
comments_data.append(comment_reply)
except KeyError:
print('textDisplay Key not Found!!')
return comments_data
def main():
choice = int(input('''Wanna give videoID(0) or playlistID(1) ?\nChoose either (0 or 1): '''))
contentType = input('Enter type of content: ')
Id = None
rawResponse = []
finalData = []
if choice:
Id = input('Enter playListID: ')
videoIds = reqPlaylistVideos(Id)
for vID in videoIds:
try:
response = reqCommentThreads(vID)
rawResponse.append(response)
data = filterJSON(response)
finalData.extend(data)
except:
pass
else:
Id = input('Enter videoID: ')
response = reqCommentThreads(Id)
rawResponse.append(response)
data = filterJSON(response)
finalData.extend(data)
try:
totalPages = response['pageInfo']['totalResults']
for _ in range(totalPages):
if 'nextPageToken' not in response:
break
nextPageToken = response['nextPageToken']
try:
response = reqCommentThreads(Id, nextPageToken)
rawResponse.append(response)
data = filterJSON(response)
finalData.extend(data)
except:
pass
except KeyError:
print('totalResults Key not Found!!')
""" with open(contentType + '_' + Id + '.json', 'w', encoding='utf-8') as outfile:
json.dump(rawResponse, outfile) """
with open(contentType + '_' + Id + '.txt', 'w', encoding='utf-8') as outfile:
for line in finalData:
outfile.write("%s\n" % line)
if __name__ == "__main__":
main()