From ab67ab9500c8c157ed4ae6454955ecb06fee3aa0 Mon Sep 17 00:00:00 2001 From: j Date: Wed, 30 Apr 2008 16:15:22 +0200 Subject: [PATCH] youtube, find, getMovieInfo, getVideoUrl --- ox/youtube.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 ox/youtube.py diff --git a/ox/youtube.py b/ox/youtube.py new file mode 100644 index 0000000..b29f070 --- /dev/null +++ b/ox/youtube.py @@ -0,0 +1,54 @@ +# -*- Mode: Python; -*- +# -*- coding: utf-8 -*- +# vi:si:et:sw=2:sts=2:ts=2 + +import re +import urllib2 +from urllib import quote +import xml.etree.ElementTree as ET +import feedparser + +from oxutils.cache import getUrl +from oxutils import findString + +def getVideoUrl(youtubeId): + url = 'http://www.youtube.com/api2_rest?method=youtube.videos.get_video_token&video_id=' + youtubeId + data = getUrl(url) + xml = ET.fromstring(data) + youtubeKey = xml.find('t').text + url = "http://youtube.com/get_video.php?video_id=%s&t=%s"%(youtubeId, youtubeKey) + return url + +def getMovieInfo(youtubeId): + url = "http://gdata.youtube.com/feeds/api/videos/%s " % youtubeId + data = getUrl(url) + fd = feedparser.parse(data) + return getInfoFromAtom(fd.entries[0]) + +def getInfoFromAtom(entry): + info = dict() + info['title'] = entry['title'] + info['description'] = entry['description'] + info['author'] = entry['author'] + info['published'] = entry['published_parsed'] + info['keywords'] = entry['media_keywords'].split(', ') + info['url'] = entry['links'][0]['href'] + info['id'] = findString(info['url'], "/watch?v=") + info['thumbnail'] = "http://img.youtube.com/vi/%s/0.jpg" % info['id'] + info['flv'] = getVideoUrl(info['id']) + info['embed'] = '''''' % (info['id'], info['id']) + return info + +def find(query, max_results=10, offset=1, orderBy='relevance'): + query = quote(query) + url = "http://gdata.youtube.com/feeds/api/videos?vq=%s&orderby=%s&start-index=%s&max-results=%s"%(query, orderBy, offset, max_results) + data = getUrl(url) + fd = feedparser.parse(data) + videos = [] + for entry in fd.entries: + v = getInfoFromAtom(entry) + videos.append(v) + if len(videos) >= max_results: + return videos + return videos +