-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathyt_public.py
115 lines (85 loc) · 3.18 KB
/
yt_public.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
import os
from dotenv import load_dotenv
from googleapiclient.discovery import build
from iteration_utilities import unique_everseen
from utils.comments import process_comments, make_csv
load_dotenv()
API_KEY = os.getenv("API_KEY")
youtube = build("youtube", "v3", developerKey=API_KEY)
def search_result(query):
"""
Refer to the documentation: https://googleapis.github.io/google-api-python-client/docs/dyn/youtube_v3.search.html
"""
request = youtube.search().list(
part="snippet",
q=query,
maxResults=10,
)
return request.execute()
def channel_stats(channelID):
"""
Refer to the documentation: https://googleapis.github.io/google-api-python-client/docs/dyn/youtube_v3.channels.html
"""
request = youtube.channels().list(
part="statistics",
id=channelID
)
return request.execute()
def comment_threads(videoID, to_csv=False):
comments_list = []
request = youtube.commentThreads().list(
part='id,replies,snippet',
videoId=videoID,
)
response = request.execute()
comments_list.extend(process_comments(response['items']))
# if there is nextPageToken, then keep calling the API
while response.get('nextPageToken', None):
request = youtube.commentThreads().list(
part='id,replies,snippet',
videoId=videoID,
pageToken=response['nextPageToken']
)
response = request.execute()
comments_list.extend(process_comments(response['items']))
comments_list = list(unique_everseen(comments_list))
print(f"Finished fetching comments for {videoID}. {len(comments_list)} comments found.")
if to_csv:
make_csv(comments_list, videoID)
return comments_list
def get_video_ids(channelId):
"""
Refer to the documentation: https://googleapis.github.io/google-api-python-client/docs/dyn/youtube_v3.search.html
"""
videoIds = []
request = youtube.search().list(
part="snippet",
channelId=channelId,
type="video",
maxResults=50,
order="date"
)
response = request.execute()
responseItems = response['items']
videoIds.extend([item['id']['videoId'] for item in responseItems if item['id'].get('videoId', None) != None])
# if there is nextPageToken, then keep calling the API
while response.get('nextPageToken', None):
request = youtube.search().list(
part="snippet",
channelId=channelId,
)
response = request.execute()
responseItems = response['items']
videoIds.extend([item['id']['videoId'] for item in responseItems if item['id'].get('videoId', None) != None])
print(f"Finished fetching videoIds for {channelId}. {len(videoIds)} videos found.")
return videoIds
if __name__ == '__main__':
# to get a list of IDs from a channel
# ids = get_video_ids("UCws1b7urfxMnvjhUJxHjtuQ")
# to get results from search
# response = search_result("pyscript")
# get channel stats
# response = channel_stats(channelID='UCzIxc8Vg53_ewaRIk3shBug')
# get comments
response = comment_threads(videoID='Qo8dXyKXyME', to_csv=True)
print(response)