-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.py
156 lines (127 loc) · 5.4 KB
/
engine.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# coding: utf-8
import httplib2
import os
import sys
import pprint
import arrow
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
# the OAuth 2.0 information for this application, including its client_id and
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
# the {{ Google Cloud Console }} at
# {{ https://cloud.google.com/console }}.
# Please ensure that you have enabled the YouTube Data API for your project.
# For more information about using OAuth2 to access the YouTube Data API, see:
# https://developers.google.com/youtube/v3/guides/authentication
# For more information about the client_secrets.json file format, see:
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
CLIENT_SECRETS_FILE = 'client_secrets.json'
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
# missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file
found at:
%s
with information from the {{ Cloud Console }}
{{ https://cloud.google.com/console }}
For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
CLIENT_SECRETS_FILE))
# This OAuth 2.0 access scope allows for read-only access to the authenticated
# user's account, but not other types of account access.
YOUTUBE_READ_WRITE_SCOPE = 'https://www.googleapis.com/auth/youtube'
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
message=MISSING_CLIENT_SECRETS_MESSAGE,
scope=YOUTUBE_READ_WRITE_SCOPE)
storage = Storage('%s-oauth2.json' % sys.argv[0])
credentials = storage.get()
if credentials is None or credentials.invalid:
flags = argparser.parse_args()
credentials = run_flow(flow, storage, flags)
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
subscriptions_request = youtube.subscriptions().list(
mine=True,
part='snippet'
)
subscriptions = []
print 'Requesting channels from current user...'
while subscriptions_request:
subscriptions_response = subscriptions_request.execute()
for subscription in subscriptions_response['items']:
channel_id = subscription['snippet']['resourceId']['channelId']
subscriptions.append(channel_id)
subscriptions_request = youtube.subscriptions() \
.list_next(subscriptions_request, subscriptions_response)
channels_response = youtube.channels().list(
part='contentDetails',
id=','.join(subscriptions)
).execute()
print 'Requesting upload playlist from channels...'
today = arrow.utcnow()
cutoff_date = today.replace(days=-7)
videos = []
for channel in channels_response['items']:
uploads_list_id = channel['contentDetails']['relatedPlaylists']['uploads']
print 'Videos in list of channel %s' % (uploads_list_id)
playlistitems_list_request = youtube.playlistItems().list(
playlistId=uploads_list_id,
part='snippet',
maxResults=50
)
stale = False
while playlistitems_list_request and not stale:
playlistitems_list_response = playlistitems_list_request.execute()
for playlist_item in playlistitems_list_response['items']:
snippet = playlist_item['snippet']
published_date = arrow.get(snippet['publishedAt'])
if published_date < cutoff_date:
# Since the videos are ordered by newest first, if a video is
# older than the cutoff date, we can ignore the rest of the
# videos and move on to the next channel.
stale = True
break
videos.append(snippet['resourceId']['videoId'])
playlistitems_list_request = youtube.playlistItems().list_next(
playlistitems_list_request, playlistitems_list_response)
playlists_insert_response = youtube.playlists().insert(
part='snippet,status',
body=dict(
snippet=dict(
title='{0}-{1}'.format(cutoff_date, today),
description='Videos from subscripted channels from {0} to {1}'
.format(cutoff_date, today)
),
status=dict(
privacyStatus='private'
)
)
).execute()
print 'New playlist id: %s' % playlists_insert_response['id']
base_playlist_insert = {
'snippet': {
'playlistId': playlists_insert_response['id'],
'resourceId': {
'kind': 'youtube#video',
},
},
}
for video in videos:
# There is no point in sorting the videos here. The publishedAt in the
# response from the channels refers to the date that the video was uploaded
# to YouTube, not when it was made available.
# The Youtube interface for the playlist correctly sorts the playlist via
# the videos' appearance date.
body = dict(**base_playlist_insert)
body['snippet']['resourceId']['videoId'] = video
youtube.playlistItems().insert(
part='snippet',
body=body
).execute()