By Vasudev Ram
I had blogged about micawber earlier, here:
Micawber gets rich content from URLs
Among the data that micawber returns for a URL, is the thumbnail, if it is a video, for example. So I thought of writing a program to download the thumbnail for a given YouTube video using micawber and the wget Python library.
Here's the code for download_youtube_thumbnails.py:
# download_youtube_thumbnails.py
# A program to download YouTube video thumbnail images.
# Author: Vasudev Ram - http://www.dancingbison.com
# Uses the micawber and wget Python libraries.
# micawber: https://github.com/coleifer/micawber
# wget: https://pypi.python.org/pypi/wget
import os
import os.path
import micawber
import wget
def download_youtube_thumbnail(video_url, video_name):
# load up rules for some default providers, such as youtube and flickr
providers = micawber.bootstrap_basic()
ret_val = providers.request(video_url)
if 'thumbnail_url' in ret_val:
thumbnail = wget.download(ret_val['thumbnail_url'], bar=None)
root, ext = os.path.splitext(thumbnail)
new_name = 'thumb_' + video_name + ext
os.rename(thumbnail, new_name)
print "Thumbnail downloaded: ", new_name
else:
print "'thumbnail_url' key not found in ret_val dict"
def main():
try:
# Download the thumbnail for the video of the album
# 'Rarely Heard Ragas' by Vilayat Khan.
download_youtube_thumbnail(
"http://www.youtube.com/watch?feature=player_detailpage&v=Qim2av-SRwU",
'Vilayat_Khan_Rarely_Heard_Ragas')
except Exception, e:
print "ERROR: Caught exception: " + repr(e)
sys.exit(1)
if __name__ == "__main__":
main()
# EOF
And here is its output - the thumbnail - when given the URL for Vilayat Khan playing Rarely Heard Ragas on the sitar:
- Vasudev Ram - Dancing Bison Enterprises
Contact Page
More Python posts.

No comments:
Post a Comment