-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyt_spider.py
78 lines (62 loc) · 2.29 KB
/
yt_spider.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
# -*- coding: utf-8 -*-
# Sample Python code for youtube.channels.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python
import os
from secrets import yt_api_key
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
import json
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
def get_playlist_id(youtube_url) :
playlist_id = youtube_url.split('list=')[1]
return playlist_id
def fetch_titles(youtube_url):
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
playlist_id = get_playlist_id(youtube_url)
# os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
api_key = yt_api_key
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey = api_key)
get_total_results = youtube.playlistItems().list(
part="snippet",
fields = ('pageInfo/totalResults'),
playlistId = playlist_id,
)
response = get_total_results.execute()
total_results = response['pageInfo']['totalResults']
if total_results > 100 :
total_results = 100
print(total_results)
nextPageToken = None
video_title_list = []
while(True) :
request = youtube.playlistItems().list(
part="snippet",
fields = ('items/snippet/title, nextPageToken, pageInfo/resultsPerPage'),
playlistId = playlist_id,
maxResults = 50,
pageToken = nextPageToken
)
response = request.execute()
if total_results > 50 :
results_number = 50
total_results -= 50
else :
results_number = total_results
total_results = 0
for index, i in enumerate(range(results_number)) :
title = response['items'][i]['snippet']['title']
print(f'{index}) {title}')
video_title_list.append(title)
try :
nextPageToken = response['nextPageToken']
except KeyError :
nextPageToken = None
if total_results == 0:
break
return video_title_list