Skip to content

Instantly share code, notes, and snippets.

@ashoksahoo
Created October 4, 2019 11:15
Show Gist options
  • Save ashoksahoo/89f3120a90769b67735300fd3e0a8d53 to your computer and use it in GitHub Desktop.
Save ashoksahoo/89f3120a90769b67735300fd3e0a8d53 to your computer and use it in GitHub Desktop.
from googleapiclient.discovery import build
# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of
# https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
DEVELOPER_KEY = ''
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'
def remove_empty_kwargs(**kwargs):
# TODO:Change to Py3 code.
good_kwargs = {}
if kwargs is not None:
for key, value in kwargs.iteritems():
if value:
good_kwargs[key] = value
return good_kwargs
def playlist_items_list_by_playlist_id(**kwargs):
# kwargs = remove_empty_kwargs(**keargs)
kwargs['maxResults'] = 50
# print 'connecting YouTube...'
client = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
# print 'youtube connected...'
response = client.playlistItems().list(
**kwargs
).execute()
videos = []
# Add each result to the appropriate list, and then display the lists of
# matching videos, channels, and playlists.
for search_result in response.get('items', []):
if search_result['kind'] == 'youtube#playlistItem':
videos.append({
"id": search_result['snippet']['resourceId']['videoId'],
"title": search_result['snippet']['title'],
"publishedAt": search_result['snippet']['publishedAt']
})
if response.get('nextPageToken'):
kwargs['pageToken'] = response.get('nextPageToken')
videos = videos + playlist_items_list_by_playlist_id(**kwargs)
return videos
def videos_list_by_id(**kwargs):
# kwargs = remove_empty_kwargs(**kwargs)
# print "connecting for videoIDs.."
client = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
# print "connected for videoIDs.."
response = client.videos().list(
**kwargs
).execute()
if 'items' in response and len(response['items'])>0:
return response.get('items', [])[0]
else:
return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment