fix youtube find

This commit is contained in:
j 2008-09-30 15:58:21 +02:00
parent c54bcc0738
commit 9e33245ee1

View file

@ -2,10 +2,11 @@
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
from urllib import quote from urllib import quote
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import re
import feedparser import feedparser
from oxlib.cache import getUrl from oxlib.cache import getUrl, getUrlUnicode
from oxlib import findString from oxlib import findString, findRe
def getVideoUrl(youtubeId, format='mp4'): def getVideoUrl(youtubeId, format='mp4'):
@ -26,6 +27,7 @@ def getMovieInfo(youtubeId):
fd = feedparser.parse(data) fd = feedparser.parse(data)
return getInfoFromAtom(fd.entries[0]) return getInfoFromAtom(fd.entries[0])
'''
def getInfoFromAtom(entry): def getInfoFromAtom(entry):
info = dict() info = dict()
info['title'] = entry['title'] info['title'] = entry['title']
@ -38,7 +40,7 @@ def getInfoFromAtom(entry):
info['thumbnail'] = "http://img.youtube.com/vi/%s/0.jpg" % info['id'] info['thumbnail'] = "http://img.youtube.com/vi/%s/0.jpg" % info['id']
info['flv'] = getVideoUrl(info['id'], 'flv') info['flv'] = getVideoUrl(info['id'], 'flv')
info['mp4'] = getVideoUrl(info['id'], 'mp4') info['mp4'] = getVideoUrl(info['id'], 'mp4')
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']) 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'])
return info return info
def find(query, max_results=10, offset=1, orderBy='relevance'): def find(query, max_results=10, offset=1, orderBy='relevance'):
@ -53,4 +55,32 @@ def find(query, max_results=10, offset=1, orderBy='relevance'):
if len(videos) >= max_results: if len(videos) >= max_results:
return videos return videos
return videos return videos
'''
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)
regx = re.compile(''' <a href="/watch.v=(.*?)" title="(.*?)" ''')
regx = re.compile('''<a href="/watch\?v=(\w*?)" ><img src="(.*?)" class="vimg120" title="(.*?)" alt="video">''')
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:
v['video_url'] = get_video_url(v['id'])
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()