All pastes #942091 Raw Edit

Stuff

public python v1 · immutable
·published 2008-03-14 06:09 UTC
rendered paste body
import urllib2import reimport socketimport xml.parsers.expatimport timeimport libxml2ignore=Falsesocket.setdefaulttimeout(15)d={}def youtube_search(Message):  print "youtube_search"  m=re.search("^!youtube\s+(.*)", Message)  if m!=None:    searchString=m.groups()[0]    searchString=searchString.replace("%", "%25")    searchString=searchString.replace(" ", "%20")    searchString=searchString.replace("&", "%26")    searchString=searchString.replace('"', "%22")    searchString=searchString.replace("?", "%3F")    req = "http://www.youtube.com/api2_rest?method="    req += "youtube.videos.list_by_tag&dev_id=" + YoutubeDevID    req += "&tag=" + searchString + "&per_page=3"    f = urllib2.urlopen(urllib2.Request(req)).read().replace("\r\n", " ").replace("&","__amp__")    d = libxml2.parseDoc(f)    vids = []    for vid in d.xpathEval("//video"):       title = vid.xpathEval("title")[0].content.replace("__amp__","&")       length = length_from_seconds(vid.xpathEval("length_seconds")[0].content)       url = vid.xpathEval("url")[0].content       vids.append({"url":url,"length":length,"title":title})    if len(vids) == 0:       vids.append({"url":"No results for " + m.groups()[0],"length":"","title":""})    return vids  else:    Nonedef length_from_seconds(s):  secs = int(s)  mins = str(secs/60)  secs = str(secs - (secs/60)*60)  if (len(secs)==1): secs = "0"+secs  return "(" + mins + ":" + secs + ")"