-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape_channel.py
61 lines (43 loc) · 1.97 KB
/
scrape_channel.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
import argparse
import os
from time import sleep
import googleapiclient.discovery
DEV_KEY = "YOUR_KEY_HERE"
def scrape_channel(channel_id):
"""Print a list of uploads from _channel_id_ to stdout.
"""
youtube = googleapiclient.discovery.build("youtube", "v3", developerKey=DEV_KEY)
channel_request = youtube.channels().list(id=channel_id, part="contentDetails")
channel_response = channel_request.execute()
channels = channel_response['items']
# This should really be unique but let's loop anyway.
for channel in channels:
uploads_id = channel['contentDetails']['relatedPlaylists']['uploads']
next_page = True
page_token = None
while next_page:
uploads_request = youtube.playlistItems().list(playlistId=uploads_id,
pageToken=page_token,
part="contentDetails",
maxResults=5)
uploads_response = uploads_request.execute()
videos = uploads_response['items']
for video in videos:
video_id = video['contentDetails']['videoId']
video_request = youtube.videos().list(id=video_id,
part="snippet")
video_response = video_request.execute()
print(video_id, video_response['items'][0]['snippet']['title'],
sep="\t", flush=True)
next_page = ('nextPageToken' in uploads_response.keys())
if next_page:
page_token = uploads_response['nextPageToken']
sleep(1)
def __main__():
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
parser = argparse.ArgumentParser()
parser.add_argument("id", type=str, help="id of channel to scrape")
args = parser.parse_args()
scrape_channel(args.id)
if __name__ == "__main__":
__main__()