Skip to content

Instantly share code, notes, and snippets.

@bkuri
Last active August 29, 2015 14:01
Show Gist options
  • Save bkuri/5ab3ad572a8062250c6b to your computer and use it in GitHub Desktop.
Save bkuri/5ab3ad572a8062250c6b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from config import *
from apiclient.discovery import build
from apiclient.errors import HttpError
from argparse import ArgumentParser
from httplib2 import Http
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run_flow
from os import listdir, path, remove
from sys import exit
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-a", "--add", action="append", help="Add this video ID to playlist")
args = parser.parse_args()
if args.add:
for video in args.add:
print "Adding video %s to queue" % video
open(path.join(WATCH_FOLDER, video), "a").close()
exit(0)
else:
videos = listdir(WATCH_FOLDER)
if len(videos) < 1:
print "No pending videos found in queue."
exit(0)
message = MISSING_CLIENT_SECRETS_MESSAGE % path.abspath(path.join(path.dirname(__file__), CLIENT_SECRETS_FILE))
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, message=message, scope=OAUTH_SCOPE)
credentials = Storage("%s-oauth2.json" % __file__).get()
youtube = build('youtube', 'v3', http=credentials.authorize(Http()))
for video in videos:
try:
print "Adding video %s to playlist" % video
request=youtube.playlistItems().insert(
part="snippet",
body={
'snippet': {
'playlistId': YOUTUBE_PLAYLIST,
'resourceId': {
'kind': 'youtube#video',
'videoId': video
}
}
}).execute()
except HttpError as e: print e
finally: remove(path.join(WATCH_FOLDER, video))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment