Blabber Blabber - 4 months ago 5
Python Question

Fetch youtube video title and category from url using youtube api v3

I'm a total beginner at this! I want to fetch the title of a youtube video and also it's category from the Url in python.

import lxml
from lxml import etree
youtube = etree.HTML(urllib.urlopen("http://www.youtube.com/watch?v=KQEOBZLx-Z8").read())
video_title = youtube.xpath("//span[@id='eow-title']/@title")
print ''.join(video_title)


Output : '12 Days of Christmas - Christmas Carol'

I've tried the
lxml
module to obtain the title but it doesn't work for all videos and I wish to use the youtube api v3. But how do I start, I went through a lot of sites and tried to take help but I always get an error on my console. I have created the server key for my project at https://console.developers.google.com, what should I do now? I went through the official page but couldn't undestand much. Where should I start? I also looked at a couple of other SO questions and the codes didn't work for me.

import urllib
import simplejson

id = 'KQEOBZLx-Z8'
url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % id

json = simplejson.load(urllib.urlopen(url))

title = json['entry']['title']['$t']
author = json['entry']['author'][0]['name']

print "id:%s\nauthor:%s\ntitle:%s" % (id, author, title)


What should I do?

Answer

You can access the data using the google-api-client lib, simple example:

from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser

# 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 = key
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"


def youtube_search(vid):
    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
                    developerKey=DEVELOPER_KEY)

    # Call the search.list method to retrieve results matching the specified
    # query term.
    search_response = youtube.search().list(
    part="snippet",
    relatedToVideoId=vid,
    type="video",
).execute()
print(search_response)
    print(search_response)


try:
    youtube_search("KQEOBZLx-Z8")
except HttpError as  e:
    print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)

The valid parameters are listed here and below that is another full example. You will need to have the youtube data api enabled and to have a developer key.

Using requests:

import requests

url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id={id}&key={api_key}"
_id = "KQEOBZLx-Z8"
r = requests.get(url.format(id=_id, api_key=DEVELOPER_KEY))

print(r.json())

Which gives you:

{u'etag': u'"5g01s4-wS2b4VpScndqCYc5Y-8k/8KOFImC75qsfDbVWk53UnuxsQmo"',
 u'items': [{u'etag': u'"5g01s4-wS2b4VpScndqCYc5Y-8k/60ikaVqe2cge-D1XrdM2l0Bf4Jg"',
             u'id': u'KQEOBZLx-Z8',
             u'kind': u'youtube#video',
             u'snippet': {u'categoryId': u'1',
                          u'channelId': u'UC3djj8jS0370cu_ghKs_Ong',
                          u'channelTitle': u'HooplaKidz',
                          u'description': u'Check out http://vid.io/xovq for Non-Stop Children Songs!\nEveryone likes to enjoy festivals, so come and sing along to this beautiful 12 Days of Christmas which will be loved by one and all !!! \n\nLyrics to Sing Along :-\n-------------------------------------\nOn the first day of Christmas, \nmy true love sent to me \nA partridge in a pear tree.\n \nOn the second day of Christmas, \nmy true love sent to me \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the third day of Christmas, \nmy true love sent to me \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the fourth day of Christmas, \nmy true love sent to me \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the fifth day of Christmas, \nmy true love sent to me \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the sixth day of Christmas, \nmy true love sent to me \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the seventh day of Christmas, \nmy true love sent to me \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the eighth day of Christmas, \nmy true love sent to me \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the ninth day of Christmas, \nmy true love sent to me \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the tenth day of Christmas, \nmy true love sent to me \nTen lords a-leaping, \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the eleventh day of Christmas, \nmy true love sent to me \nEleven pipers piping, \nTen lords a-leaping, \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the twelfth day of Christmas, \nmy true love sent to me \nTwelve drummers drumming, \nEleven pipers piping, \nTen lords a-leaping, \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree!  \n\nDownload Hooplakidz Songs on iTunes:\nhttp://itunes.apple.com/us/album/hoopla-kidz/id439987868\n\nBecome a Hooplakidz Fan on Facebook:\nhttp://www.facebook.com/hooplakidz',
                          u'liveBroadcastContent': u'none',
                          u'localized': {u'description': u'Check out http://vid.io/xovq for Non-Stop Children Songs!\nEveryone likes to enjoy festivals, so come and sing along to this beautiful 12 Days of Christmas which will be loved by one and all !!! \n\nLyrics to Sing Along :-\n-------------------------------------\nOn the first day of Christmas, \nmy true love sent to me \nA partridge in a pear tree.\n \nOn the second day of Christmas, \nmy true love sent to me \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the third day of Christmas, \nmy true love sent to me \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the fourth day of Christmas, \nmy true love sent to me \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the fifth day of Christmas, \nmy true love sent to me \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the sixth day of Christmas, \nmy true love sent to me \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the seventh day of Christmas, \nmy true love sent to me \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the eighth day of Christmas, \nmy true love sent to me \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the ninth day of Christmas, \nmy true love sent to me \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the tenth day of Christmas, \nmy true love sent to me \nTen lords a-leaping, \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the eleventh day of Christmas, \nmy true love sent to me \nEleven pipers piping, \nTen lords a-leaping, \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree. \n\nOn the twelfth day of Christmas, \nmy true love sent to me \nTwelve drummers drumming, \nEleven pipers piping, \nTen lords a-leaping, \nNine ladies dancing, \nEight maids a-milking, \nSeven swans a-swimming, \nSix geese a-laying, \nFive golden rings, \nFour calling birds, \nThree French hens, \nTwo turtle doves, \nAnd a partridge in a pear tree!  \n\nDownload Hooplakidz Songs on iTunes:\nhttp://itunes.apple.com/us/album/hoopla-kidz/id439987868\n\nBecome a Hooplakidz Fan on Facebook:\nhttp://www.facebook.com/hooplakidz',
                                         u'title': u'12 Days of Christmas | Christmas Carols by Hooplakidz'},
                          u'publishedAt': u'2011-12-15T08:59:29.000Z',
                          u'tags': [u'12 days of christmas',
                                    u'Twelve Days Of Christmas (Holiday Period)',
                                    u'christmas carols',
                                    u'Christmas Carol (Composition Type)',
                                    u'christmas carols songs',
                                    u'christmas carols for children',
                                    u'christmas carols for kids',
                                    u'christmas songs',
                                    u'christmas songs for children',
                                    u'Christmas Music (Musical Genre)',
                                    u'Christmas (Holiday)',
                                    u'hooplakidz'],
                          u'thumbnails': {u'default': {u'height': 90,
                                                       u'url': u'https://i.ytimg.com/vi/KQEOBZLx-Z8/default.jpg',
                                                       u'width': 120},
                                          u'high': {u'height': 360,
                                                    u'url': u'https://i.ytimg.com/vi/KQEOBZLx-Z8/hqdefault.jpg',
                                                    u'width': 480},
                                          u'maxres': {u'height': 720,
                                                      u'url': u'https://i.ytimg.com/vi/KQEOBZLx-Z8/maxresdefault.jpg',
                                                      u'width': 1280},
                                          u'medium': {u'height': 180,
                                                      u'url': u'https://i.ytimg.com/vi/KQEOBZLx-Z8/mqdefault.jpg',
                                                      u'width': 320},
                                          u'standard': {u'height': 480,
                                                        u'url': u'https://i.ytimg.com/vi/KQEOBZLx-Z8/sddefault.jpg',
                                                        u'width': 640}},
                          u'title': u'12 Days of Christmas | Christmas Carols by Hooplakidz'}}],
 u'kind': u'youtube#videoListResponse',
 u'pageInfo': {u'resultsPerPage': 1, u'totalResults': 1}}

To get the title and categoryId:

r = requests.get(url.format(id=_id, api_key=DEVELOPER_KEY))
js = r.json()
items = js["items"][0]
print (items["snippet"]["categoryId"])
print (items["snippet"]["title"])

Output:

1
12 Days of Christmas | Christmas Carols by Hooplakidz

This will allow you to do it all programmatically, creating a mapping of id to category name:

import requests

def get_data(key, region, *ids):
    url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id={ids}&key={api_key}"
    r = requests.get(url.format(ids=",".join(ids), api_key=key))
    js = r.json()
    items = js["items"]
    cat_js = requests.get("https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&regionCode={}&key={}".format(region,
        key)).json()
    categories = {d['id']: d["snippet"]['title'] for d in cat_js["items"]}
    for item in items:
        yield item["snippet"]["title"], categories[item["snippet"]["categoryId"]]

If we run it by passing in he required args we get:

In [86]: for title, cat in get_data(DEVELOPER_KEY, "IE",  "KQEOBZLx-Z8", "ALTc-Cqz650"):
   ....:         print(title, cat)
   ....:     
(u'12 Days of Christmas | Christmas Carols by Hooplakidz', u'Film & Animation')
(u'Conor Mcgregor Favorite to win Rematch against Nate Diaz,Lesnar on Drug addiction,Ali Funeral', u'Sports')
Comments