Skip to content

Instantly share code, notes, and snippets.

@grapefrukt
Last active December 7, 2023 11:06
Show Gist options
  • Save grapefrukt/eded6d6edb2049e67fd9ab571fffc680 to your computer and use it in GitHub Desktop.
Save grapefrukt/eded6d6edb2049e67fd9ab571fffc680 to your computer and use it in GitHub Desktop.
Fills a pre-created playlist with videos from the IGF json feed.
#!/usr/bin/python
import os
import json
import google.oauth2.credentials
import google_auth_oauthlib.flow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google_auth_oauthlib.flow import InstalledAppFlow
CLIENT_SECRETS_FILE = 'client_secret.json'
# This OAuth 2.0 access scope allows for full read/write access to the
# authenticated user's account.
SCOPES = ['https://www.googleapis.com/auth/youtube']
API_SERVICE_NAME = 'youtube'
API_VERSION = 'v3'
# Authorize the request and store authorization credentials.
def get_authenticated_service():
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
credentials = flow.run_local_server(port=0)
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
def add_to_playlist(youtube, id):
body = dict(
snippet=dict(
playlistId='PLwJUHMIkmaO4muB5uM3hRwHHZxOoutzmq',
resourceId=dict(
kind='youtube#video',
videoId=id
)
)
)
playlists_insert_response = youtube.playlistItems().insert(
part='snippet',
body=body
).execute()
print('New playlist ID: %s' % playlists_insert_response['id'])
if __name__ == '__main__':
# https://submit.igf.com/json
jfile = open('igf.json')
data = json.load(jfile)
count_total = 0
count_video = 0
count_youtube = 0
print(len(data['entries']))
youtube = get_authenticated_service()
for entry in data['entries']:
count_total += 1
if not 'video' in entry : continue
count_video += 1
if not entry['video']['type'] == 'YouTube' : continue
count_youtube += 1
# this will inevitably hit youtube api limits, skip ahead here:
#if count_youtube <= 404 : continue
print("{} ({}/{})".format(entry['video']['videoid'].split('?', 1)[0], count_youtube, 487))
add_to_playlist(youtube, entry['video']['videoid'][:11])
print("")
print("count_total: {0}".format(count_total))
print("count_video: {0}".format(count_video))
print("count_youtube: {0}".format(count_youtube))
#!/usr/bin/python
import os
import json
from pytube import YouTube
if __name__ == '__main__':
# https://submit.igf.com/json
jfile = open('igf.json')
data = json.load(jfile)
count_total = 0
count_video = 0
count_youtube = 0
duration_sum = 0
print(len(data['entries']))
for entry in data['entries']:
count_total += 1
if not 'video' in entry : continue
count_video += 1
if not entry['video']['type'] == 'YouTube' : continue
count_youtube += 1
yt = YouTube(entry['video']['url']) ## this creates a YOUTUBE OBJECT
try:
yt.check_availability()
except:
continue
video_length = yt.length ## this will return the length
print("{}\t{}\t{}\t{}".format(entry['video']['videoid'].split('?', 1)[0], video_length, yt.views, count_youtube))
duration_sum += video_length
print("")
print("count_total: {0}".format(count_total))
print("count_video: {0}".format(count_video))
print("count_youtube: {0}".format(count_youtube))
print("duration_sum: {0}".format(duration_sum))
google-api-python-client
google-auth-oauthlib
google-auth-httplib2
pytube
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment