Created
November 21, 2016 04:38
-
-
Save fhk/494c0567cbf6a059936e65b263709594 to your computer and use it in GitHub Desktop.
csv to Google API youtube playlist maker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httplib2 | |
import os | |
import sys | |
import csv | |
from apiclient.discovery import build | |
from apiclient.errors import HttpError | |
from oauth2client.client import flow_from_clientsecrets | |
from oauth2client.file import Storage | |
from oauth2client.tools import argparser, run_flow | |
DEVELOPER_KEY = "add_your_key" | |
YOUTUBE_API_SERVICE_NAME = "youtube" | |
YOUTUBE_API_VERSION = "v3" | |
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube" | |
CLIENT_SECRETS_FILE='client_secret.json' | |
def get_authenticated_service(): | |
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SCOPE, | |
message="MISSING_CLIENT_SECRETS_MESSAGE") | |
storage = Storage("%s-oauth2.json" % sys.argv[0]) | |
credentials = storage.get() | |
if credentials is None or credentials.invalid: | |
credentials = run_flow(flow, storage) | |
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, | |
http=credentials.authorize(httplib2.Http())) | |
def youtube_search(song, youtube): | |
# Call the search.list method to retrieve results matching the specified | |
# query term. | |
search_response = youtube.search().list( | |
q=song, | |
part="id,snippet", | |
maxResults=1 | |
).execute() | |
videos = [] | |
# Add each result to the appropriate list, and then display the lists of | |
# matching videos | |
for search_result in search_response.get("items", [song]): | |
if search_result["id"]["kind"] == "youtube#video": | |
videos.append([ | |
"%s" % (search_result["snippet"]["title"]), | |
"%s" % (search_result["id"]["videoId"]) | |
]) | |
return videos | |
def load_data(filename): | |
songs = [] | |
with open(filename, 'r') as f: | |
f_reader = csv.reader(f) | |
for row in f_reader: | |
songs.append("%s %s"% (row[0], row[1])) | |
return songs | |
def add_to_playlist(youtube, playlist_id, video_ids, added_videos_file=None): | |
count = len(video_ids) | |
for video_num, video_id in enumerate(video_ids, start=1): | |
sys.stdout.write('\rAdding video {} of {}'.format(video_num, count)) | |
sys.stdout.flush() | |
youtube.playlistItems()\ | |
.insert(part='snippet', | |
body={ | |
'snippet': { | |
'playlistId': playlist_id, | |
'resourceId': { | |
'videoId': video_id, | |
'kind': 'youtube#video', | |
}, | |
}, | |
})\ | |
.execute() | |
if added_videos_file: | |
added_videos_file.write(video_id + '\n') | |
if count: | |
sys.stdout.write('\n') | |
def main(): | |
argparser.add_argument("--songs", help="Song list") | |
args = argparser.parse_args() | |
youtube = get_authenticated_service() | |
song_search = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) | |
songs = load_data(args.songs) | |
songs_with_id = [] | |
playlists_insert_response = youtube.playlists().insert( | |
part="snippet,status", | |
body=dict( | |
snippet=dict( | |
title="Wedding Band Songs", | |
description="All the songs on the playlist" | |
), | |
status=dict( | |
privacyStatus="public" | |
) | |
) | |
).execute() | |
playlist_id = playlists_insert_response["id"] | |
try: | |
for song in songs: | |
result = youtube_search(song, song_search) | |
if result: | |
songs_with_id.append(result[0]) | |
else: | |
print("%s not found"%song) | |
print(songs_with_id[-1]) | |
add_to_playlist(youtube, playlist_id, [r[1] for r in songs_with_id]) | |
except HttpError as e: | |
print ("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)) | |
else: | |
print ("The bulletin was posted to your channel.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment