def search(ctx, cmd, arg, *args): """so <tag1> <tag2> <etc>\nSearch through stackoverflow for the given tags""" thingy = ";".join(args) bunnies = 'search?tagged=%s&pagesize=3' % thingy searchURL = apiURL % bunnies urlObject = urllib2.urlopen(searchURL) urlObject = StringIO(urlObject.read()) # Ugly hack because we can't .read() from the gzip otherwise urlObject = gzip.GzipFile(fileobj=urlObject) # Stack Overflow compresseses decoded = jsonLoad(urlObject) results = decoded["total"] if results > 0: res = min(results, 3) ctx.reply("Results 1-%d of %s" % (res, prettyNumber(results)), "StackOverflow") else: ctx.reply("No results for your query", "StackOverflow") for q in decoded['questions']: title = q['title'] questionURL = qURL % ('questions', q['question_id']) ctx.reply(u'%s • %s' % (title, questionURL), 'StackOverflow')
def wikipedia_cmd(ctx, cmd, arg): """wikipedia <search> Searches through wikipedia for the given search string""" o = openUrl(u"http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=%s&srwhat=text&srlimit=3&format=xml" % urllib.quote(arg)) xml = dom.parse(o) results = int(xml.getElementsByTagName("searchinfo")[0].getAttribute("totalhits")) if results > 0: res = min(results, 3) ctx.reply("Results 1-%d out of %s" % (res, prettyNumber(results)), "Wikipedia") else: ctx.reply(u"No results found for %s" % arg, "Wikipedia") for i in xml.getElementsByTagName("p"): title = i.getAttribute("title") snippet = i.getAttribute("snippet") # Hilight the matched parts snippet = re.sub('<span class=\'searchmatch\'>(.+?)</span>', u'`B\\1`B', snippet) # Clean it up nice and pretty now snippet = re.sub('<[^>]+>', u'', snippet) snippet = re.sub(' ([.,!?\'])', u'\\1', snippet) snippet = snippet.replace(' ', u' ') # And then display it. :D ctx.reply(u"`B%s`B ( %s ) • %s" % (title, "http://wikipedia.org/wiki/%s" % urllib.quote(title), snippet), "Wikipedia")
def displayMeta(ctx, data, vid): """Displays a single youtube video result, given the xml node""" s = u"" s += u"Title: %s " % data.getElementsByTagName("title")[0].firstChild.data s += u" • By: %s" % data.getElementsByTagName("author")[0].getElementsByTagName("name")[0].firstChild.data s += u" • Length: %s" % prettyTime(data.getElementsByTagName("yt:duration")[0].getAttribute("seconds")) s += u" • View Count: %s" % prettyNumber(data.getElementsByTagName("yt:statistics")[0].getAttribute("viewCount")) r = data.getElementsByTagName("gd:rating") if len(r): r = r[0] s += u" • Average Rating: %1.2f/5 over %s people" % ( float(r.getAttribute("average")), prettyNumber(r.getAttribute("numRaters")), ) else: s += u" • No ratings" s += u" • http://youtu.be/%s" % vid ctx.reply(s, "YouTube")
def displayMeta(ctx, data, vid): """Displays a single youtube video result, given the xml node""" s = u"" s += u"Title: %s " % data.getElementsByTagName("title")[0].firstChild.data s += u" • By: %s" % data.getElementsByTagName("author")[0].getElementsByTagName("name")[0].firstChild.data showRest = True r = data.getElementsByTagName("yt:state") if len(r): r = r[0] if r.getAttribute("name") == "restricted": showRest = r.getAttribute("reasonCode") == "limitedSyndication" if showRest: s += u" • Syndication Limited." else: s += u" • Video is unavailable: %s" % r.firstChild.data if showRest: s += u" • Length: %s" % prettyTime(data.getElementsByTagName("yt:duration")[0].getAttribute("seconds")) s += u" • View Count: %s" % prettyNumber(data.getElementsByTagName("yt:statistics")[0].getAttribute("viewCount")) r = data.getElementsByTagName("gd:rating") if len(r): r = r[0] s += u" • Average Rating: %1.2f/5 over %s people" % ( float(r.getAttribute("average")), prettyNumber(r.getAttribute("numRaters")) ) else: s += u" • No ratings" s += u" • http://youtu.be/%s" % vid addStatusToArchive(ctx, s, "YouTube") ctx.reply(s, "YouTube")
def youtube_cmd(ctx, cmd, arg): """youtube <search string> Searches youtube for the given search string""" url = "http://gdata.youtube.com/feeds/api/videos?q=%s&max-results=3&v=2" % urllib.quote(arg) r = urllib2.urlopen(url) r = dom.parse(r) results = int(r.getElementsByTagName("openSearch:totalResults")[0].firstChild.data) if results > 0: res = min(results, 3) ctx.reply("Results 1-%d out of %s" % (res, prettyNumber(results)), "YouTube") else: ctx.reply("No results found for %s" % arg, "YouTube") for i in r.getElementsByTagName("entry"): vid = i.getElementsByTagName("id")[0].firstChild.data vid = vid.split(":")[-1] displayMeta(ctx, i, vid)