Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def query(vid, auth_key):
- '''
- returns the title, viewcount, time, and uploader of a Youtube video.
- vid is the Youtube video ID at the end of the Youtube URL.
- '''
- main = 'https://www.googleapis.com/youtube/v3/videos?id='
- key = '&key=' + auth_key
- ext = '&part=snippet,contentDetails,statistics'
- req = web.get(main + vid + key + ext)
- data = json.loads(req, encoding='utf-8')
- data = data['items'][0]
- title = data['snippet']['title']
- uploader = data['snippet']['channelTitle']
- try:
- viewcount = str(data['statistics']['viewCount'])
- except (KeyError, IndexError):
- viewcount = '0'
- duration = str(data['contentDetails']['duration'])
- # a video with no likes results in IndexErrors (assuming true for ratingCount, too)
- try:
- likes = str(data['statistics']['likeCount'])
- except (KeyError, IndexError):
- likes = '0'
- try:
- dislikes = str(data['statistics']['dislikeCount'])
- except (KeyError, IndexError):
- dislikes = '0'
- time = iso_8601(duration)
- return title, viewcount, time, uploader, likes, dislikes
Advertisement
Add Comment
Please, Sign In to add comment