YouTube API - unauthorized access

As discussed during lectures, some APIs require authorization to provide access, and some not. YouTube API provides some data without authorization that can easily be accessed using a third-package called pafy. The latter provides easiy access to movie data from YouTube once the user provides the url. Code below is not explained line-by-line as it is quite user-friendly and self-explaining. The official documentation on pafy package can be found on their GitHub repository.

The package can be installed using pip as usually:

pip install pafy

The data can be accessed even without using this package, as the API response is a publicly available JSON file. One can find that file as follows: https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=BGBM5vWiBLo&format=json Just change the id with your movie ID (after watch?v= and before %format=json). Similarly, one can access the XML type of response by just changing the last part (json) to XML.


In [1]:
import pafy
url = "https://www.youtube.com/watch?v=BGBM5vWiBLo"
video = pafy.new(url)


WARNING:root:pafy: youtube-dl not found; falling back to internal backend. This is not as well maintained as the youtube-dl backend. To hide this message, set the environmental variable PAFY_BACKEND to "internal".

In [2]:
video.title


Out[2]:
u'\u266b Pink Floyd - High Hopes [Lyrics]'

In [3]:
video.description


Out[3]:
u"--- INFO ---\r\n\r\nAlbum: The Division Bell\r\nYear: 1994\r\nMusic: Gilmour\r\n\r\n--- LYRICS ---\r\n\r\nBeyond the horizon of the place we lived when we were young\r\nIn a world of magnets and miracles\r\nOur thoughts strayed constantly and without boundary\r\nThe ringing of the division bell had begun\r\n\r\nAlong the Long Road and on down the Causeway\r\nDo they still meet there by the Cut\r\n\r\nThere was a ragged band that followed in our footsteps\r\nRunning before time took our dreams away\r\nLeaving the myriad small creatures trying to tie us to the ground\r\nTo a life consumed by slow decay\r\n\r\nThe grass was greener\r\nThe light was brighter\r\nWith friends surrounded\r\nThe nights of wonder\r\n\r\nLooking beyond the embers of bridges glowing behind us\r\nTo a glimpse of how green it was on the other side\r\nSteps taken forwards but sleepwalking back again\r\nDragged by the force of some inner tide\r\n\r\nAt a higher altitude with flag unfurled\r\nWe reached the dizzy heights of that dreamed of world\r\n\r\nEncumbered forever by desire and ambition\r\nThere's a hunger still unsatisfied\r\nOur weary eyes still stray to the horizon\r\nThough down this road we've been so many times\r\n\r\nThe grass was greener\r\nThe light was brighter\r\nThe taste was sweeter\r\nThe nights of wonder\r\nWith friends surrounded\r\nThe dawn mist glowing\r\nThe water flowing\r\nThe endless river\r\nForever and ever"

In [4]:
details = [video.title, video.rating, video.viewcount, video.author, video.length]
print(details)


[u'\u266b Pink Floyd - High Hopes [Lyrics]', 4.9097776413, 22113761, u'MrOLMOBE', 419]

In [5]:
# downloading the video with best quality
best_video = video.getbest()
best_video.download(quiet=False)



Out[5]:
u'\u266b Pink Floyd - High Hopes [Lyrics].webm'

In [6]:
# downloading the audio of the video with best quality
bestaudio = video.getbestaudio()
bestaudio.download()



Out[6]:
u'\u266b Pink Floyd - High Hopes [Lyrics].opus'

In [7]:
# getting all streams: all possible audio/video extensions
allstreams = video.allstreams
from pprint import pprint
pprint(allstreams)


[normal:webm@640x360,
 normal:mp4@640x360,
 normal:3gp@320x240,
 normal:3gp@176x144,
 video:m4v@854x480,
 video:webm@640x480,
 video:m4v@640x360,
 video:webm@480x360,
 video:m4v@426x240,
 video:webm@360x240,
 video:m4v@256x144,
 video:webm@256x144,
 audio:opus@128k,
 audio:ogg@128k,
 audio:m4a@128k,
 audio:opus@56k,
 audio:opus@48k]

In [8]:
for i in allstreams:
    print(i.mediatype, i.extension, i.quality)


('normal', 'webm', '640x360')
('normal', 'mp4', '640x360')
('normal', '3gp', '320x240')
('normal', '3gp', '176x144')
('video', 'm4v', '854x480')
('video', 'webm', '640x480')
('video', 'm4v', '640x360')
('video', 'webm', '480x360')
('video', 'm4v', '426x240')
('video', 'webm', '360x240')
('video', 'm4v', '256x144')
('video', 'webm', '256x144')
('audio', 'opus', '128k')
('audio', 'ogg', '128k')
('audio', 'm4a', '128k')
('audio', 'opus', '56k')
('audio', 'opus', '48k')

In [9]:
# download a chosen filetype, e.g. m4a
allstreams[-3].download()



Out[9]:
u'\u266b Pink Floyd - High Hopes [Lyrics].m4a'