2008-04-30 14:15:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-06-19 09:47:02 +00:00
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2009-07-24 10:46:59 +00:00
|
|
|
from urllib import quote, unquote
|
2008-10-07 20:22:17 +00:00
|
|
|
import httplib
|
2008-04-30 14:15:22 +00:00
|
|
|
import xml.etree.ElementTree as ET
|
2008-09-30 13:58:21 +00:00
|
|
|
import re
|
2008-04-30 14:15:22 +00:00
|
|
|
|
2008-04-30 14:22:01 +00:00
|
|
|
import feedparser
|
2008-09-30 13:58:21 +00:00
|
|
|
from oxlib.cache import getUrl, getUrlUnicode
|
|
|
|
from oxlib import findString, findRe
|
2008-04-30 14:15:22 +00:00
|
|
|
|
2008-06-19 09:47:02 +00:00
|
|
|
|
2008-10-07 20:22:17 +00:00
|
|
|
def getVideoKey(youtubeId):
|
2009-08-19 16:02:05 +00:00
|
|
|
data = getUrl("http://www.youtube.com/get_video_info?&video_id=%s" % youtubeId)
|
|
|
|
match = re.compile("token=(.+)&thumbnail").findall(data)
|
2008-12-09 18:25:35 +00:00
|
|
|
if match:
|
2009-07-24 10:46:59 +00:00
|
|
|
return unquote(match[0])
|
2008-12-07 14:39:39 +00:00
|
|
|
return False
|
2008-10-07 20:22:17 +00:00
|
|
|
|
|
|
|
def getVideoUrl(youtubeId, format='mp4'):
|
|
|
|
youtubeKey = getVideoKey(youtubeId)
|
2008-12-09 18:09:12 +00:00
|
|
|
if format == '720p':
|
|
|
|
fmt=22
|
|
|
|
url = "http://youtube.com/get_video.php?video_id=%s&t=%s&fmt=%s" % (youtubeId, youtubeKey, fmt)
|
|
|
|
elif format == 'mp4':
|
2008-06-19 09:47:02 +00:00
|
|
|
fmt=18
|
2008-10-07 20:22:17 +00:00
|
|
|
url = "http://youtube.com/get_video.php?video_id=%s&t=%s&fmt=%s" % (youtubeId, youtubeKey, fmt)
|
2008-06-19 09:47:02 +00:00
|
|
|
else:
|
2008-10-07 20:22:17 +00:00
|
|
|
url = "http://youtube.com/get_video.php?video_id=%s&t=%s" % (youtubeId, youtubeKey)
|
2008-06-19 09:47:02 +00:00
|
|
|
return url
|
2008-04-30 14:15:22 +00:00
|
|
|
|
2008-10-07 21:07:49 +00:00
|
|
|
def getMovieInfo(youtubeId, video_url_base=None):
|
|
|
|
url = "http://gdata.youtube.com/feeds/api/videos/%s" % youtubeId
|
2008-06-19 09:47:02 +00:00
|
|
|
data = getUrl(url)
|
|
|
|
fd = feedparser.parse(data)
|
2008-10-07 21:07:49 +00:00
|
|
|
return getInfoFromAtom(fd.entries[0], video_url_base)
|
2008-04-30 14:15:22 +00:00
|
|
|
|
2008-10-07 21:07:49 +00:00
|
|
|
def getInfoFromAtom(entry, video_url_base=None):
|
2008-06-19 09:47:02 +00:00
|
|
|
info = dict()
|
|
|
|
info['title'] = entry['title']
|
|
|
|
info['description'] = entry['description']
|
|
|
|
info['author'] = entry['author']
|
2008-10-07 21:07:49 +00:00
|
|
|
#info['published'] = entry['published_parsed']
|
2008-12-09 18:04:36 +00:00
|
|
|
if 'media_keywords' in entry:
|
|
|
|
info['keywords'] = entry['media_keywords'].split(', ')
|
2008-06-19 09:47:02 +00:00
|
|
|
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']
|
2008-10-07 21:07:49 +00:00
|
|
|
if video_url_base:
|
|
|
|
info['flv'] = "%s/%s.%s" % (video_url_base, info['id'], 'flv')
|
|
|
|
info['mp4'] = "%s/%s.%s" % (video_url_base, info['id'], 'mp4')
|
|
|
|
else:
|
|
|
|
info['flv'] = getVideoUrl(info['id'], 'flv')
|
|
|
|
info['mp4'] = getVideoUrl(info['id'], 'mp4')
|
2008-12-09 18:09:12 +00:00
|
|
|
info['720p'] = getVideoUrl(info['id'], '720p')
|
2008-09-30 13:58:21 +00:00
|
|
|
info['embed'] = '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/%s&hl=en"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/%s&hl=en" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>' % (info['id'], info['id'])
|
2008-06-19 09:47:02 +00:00
|
|
|
return info
|
2008-04-30 14:15:22 +00:00
|
|
|
|
2008-10-07 21:07:49 +00:00
|
|
|
def find(query, max_results=10, offset=1, orderBy='relevance', video_url_base=None):
|
2008-06-19 09:47:02 +00:00
|
|
|
query = quote(query)
|
2008-10-07 21:07:49 +00:00
|
|
|
url = "http://gdata.youtube.com/feeds/api/videos?vq=%s&orderby=%s&start-index=%s&max-results=%s" % (query, orderBy, offset, max_results)
|
|
|
|
data = getUrlUnicode(url)
|
2008-06-19 09:47:02 +00:00
|
|
|
fd = feedparser.parse(data)
|
|
|
|
videos = []
|
|
|
|
for entry in fd.entries:
|
2008-10-07 21:07:49 +00:00
|
|
|
v = getInfoFromAtom(entry, video_url_base)
|
2008-06-19 09:47:02 +00:00
|
|
|
videos.append(v)
|
|
|
|
if len(videos) >= max_results:
|
|
|
|
return videos
|
|
|
|
return videos
|
2008-09-30 13:58:21 +00:00
|
|
|
|
2008-10-07 21:07:49 +00:00
|
|
|
'''
|
2008-09-30 13:58:21 +00:00
|
|
|
def find(query, max_results=10, offset=1, orderBy='relevance', video_url_base=None):
|
|
|
|
url = "http://youtube.com/results?search_query=%s&search=Search" % quote(query)
|
|
|
|
data = getUrlUnicode(url)
|
2008-10-07 21:07:49 +00:00
|
|
|
regx = re.compile(' <a href="/watch.v=(.*?)" title="(.*?)" ')
|
|
|
|
regx = re.compile('<a href="/watch\?v=(\w*?)" ><img src="(.*?)" class="vimg120" title="(.*?)" alt="video">')
|
2008-09-30 13:58:21 +00:00
|
|
|
id_title = regx.findall(data)
|
|
|
|
data_flat = data.replace('\n', ' ')
|
|
|
|
videos = {}
|
|
|
|
for video in id_title:
|
|
|
|
vid = video[0]
|
|
|
|
if vid not in videos:
|
|
|
|
v = dict()
|
|
|
|
v['id'] = vid
|
|
|
|
v['link'] = "http//youtube.com/watch.v=%s" % v['id']
|
|
|
|
v['title'] = video[2].strip()
|
|
|
|
if video_url_base:
|
|
|
|
v['video_link'] = "%s/%s" % (video_url_base, v['id'])
|
|
|
|
else:
|
2008-10-07 20:22:17 +00:00
|
|
|
v['video_url'] = getVideoUrl(v['id'])
|
2008-09-30 13:58:21 +00:00
|
|
|
v['description'] = findRe(data, 'BeginvidDesc%s">(.*?)</span>' % v['id']).strip().replace('<b>', ' ').replace('</b>', '')
|
|
|
|
v['thumbnail'] = video[1]
|
|
|
|
videos[vid] = v
|
|
|
|
if len(videos) >= max_results:
|
|
|
|
return videos.values()
|
|
|
|
return videos.values()
|
2008-10-07 21:07:49 +00:00
|
|
|
'''
|
2008-09-30 13:58:21 +00:00
|
|
|
|