Showing posts with label micawber. Show all posts
Showing posts with label micawber. Show all posts

Wednesday, December 18, 2013

Download YouTube thumbnails with micawber and wget


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.

Wednesday, August 28, 2013

Micawber gets rich content from URLs


By Vasudev Ram



micawber is a Python library that enables you to "fetch rich content from URLs" of videos, music, etc. I guess micawber is named after the character Micawber in the novel "David Copperfield" by Charles Dickens.

coleifer/micawber on GitHub

Read the micawber docs

I wrote a small test program to try out micawber, by modifying one of the basic examples of its use. It fetches some of the data about 3 YouTube videos: Andres Segovia playing 'Asturias', Guido van Rossum's talk - 'In Python We Trust', and Luisa Fernandez singing 'Granada'.

Here is the program:
# test_micawber.py

import micawber

# load up rules for some default providers, such as youtube and flickr
providers = micawber.bootstrap_basic()

# Fetch the data for 3 video URLs
for url in [ 'http://www.youtube.com/watch?v=9efHwnFAkuA',
    'http://www.youtube.com/watch?v=mWB3oh1GPdo',
    'http://www.youtube.com/watch?v=UYGc9joeAD0' ]:
    
    retval = providers.request(url)
    for key in ['type', 'title', 'url', 'author_name', 
    'author_url', 'provider_name', 'provider_url', 'height',
    'width', 'thumbnail_url']:
        print key, ":", retval[key]

    print

# EOF
You can run it like this:
python test_micawber.py
And here is its output:
type : video
title : Andres Segovia - Asturias
url : http://www.youtube.com/watch?v=9efHwnFAkuA
author_name : ByTheSPiRiTs
author_url : http://www.youtube.com/user/ByTheSPiRiTs
provider_name : YouTube
provider_url : http://www.youtube.com/
height : 344
width : 459
thumbnail_url : http://i1.ytimg.com/vi/9efHwnFAkuA/hqdefault.jpg

type : video
title : In Python We Trust
url : http://www.youtube.com/watch?v=mWB3oh1GPdo
author_name : OreillyMedia
author_url : http://www.youtube.com/user/OreillyMedia
provider_name : YouTube
provider_url : http://www.youtube.com/
height : 270
width : 480
thumbnail_url : http://i1.ytimg.com/vi/mWB3oh1GPdo/hqdefault.jpg

type : video
title : Luisa Fernandez -  Granada
url : http://www.youtube.com/watch?v=UYGc9joeAD0
author_name : Dany Massart
author_url : http://www.youtube.com/user/DJCOOLDANY
provider_name : YouTube
provider_url : http://www.youtube.com/
height : 344
width : 459
thumbnail_url : http://i1.ytimg.com/vi/UYGc9joeAD0/hqdefault.jpg

- Vasudev Ram - Dancing Bison Enterprises

Contact me