def weather(query): import engine, config if query == "": loc = engine.location() if not loc: return None location = "where you are now" query = "%s,%s" % (loc[0], loc[1]) else: location = query j = engine.getJson("http://api.wunderground.com/api/%s/forecast/q/%s.json" % (config.weatherunderground_key, query)) if 'forecast' in j: s = [] for day in j['forecast']['txt_forecast']['forecastday'][:5]: s.append( "<img src='%s' /><br/><strong>%s</strong><br/>%s" % ( day['icon_url'], day['title'], day['fcttext'] ) ) s = engine.grid(s) return { "snippet" : s, "style" : "goodies", "url" : "http://www.wunderground.com/", "display_url" : "Powered by WeatherUnderground", "title" : "Weather for %s" % location }
def flickr(q_query, raw_query, page): results = [] config.pre_output = header % "Flickr" config.result_class = ' thumbnails' j = getJson( "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%s&text=%s&format=json&nojsoncallback=1&per_page=20" % (config.flickr_key, q_query), default='{ "photos" : { "photo" : [] } }') if len(j['photos']['photo']) != 0: for r in j['photos']['photo']: results.append({ "url": "http://flickr.com/photos/%s/%s" % (r['owner'], r['id']), "display_url": "http://flic.kr/%s" % base58(int(r['id'])), "title": shorten(r['title'], 140), "user": r['owner'], "style": "image", "source": "flickr", "image": "http://farm%s.static.flickr.com/%s/%s_%s_t.jpg" % (r['farm'], r['server'], r['id'], r['secret']), }) return results
def dictionary(query): import config, engine, urllib2 from BeautifulSoup import BeautifulSoup j = urllib2.urlopen("http://api-pub.dictionary.com/v001?vid=%s&q=%s&type=define" % ( config.dictionary_com_key, query )) j = BeautifulSoup(j) d = None try: d = j.find("def").contents[0] + "" except Exception: pass if d is not None: return { "style" : "goodies", "title" : "Dictionary.com defention of %s" % query, "snippet" : d, "url" : "http://dictionary.com", "display_url" : "Powered by Dictionary.com" } else: j = engine.getJson("http://www.urbandictionary.com/iphone/search/define?term=%s" % query) if j['result_type'] != "no_results": d = j['list'][0] s = '<strong>%s</strong>: %s<br/>Examples: %s' % ( d['word'], d['definition'].replace("\n", "<br/>"), d['example'].replace("\n", "<br/>") ) return { "style" : "goodies", "title" : "UrbanDictionary defention of %s" % query, "snippet" : s, "url" : "http://urbandictionary.com", "display_url" : "Powered by UrbanDictionary.com" }
def youtube(q_query, raw_query, page): config.result_class = ' thumbnails' results = [] s = ((page-1)*12) + 1 j = getJson('https://gdata.youtube.com/feeds/api/videos?alt=json&q=%s&start-index=%i&max-results=12' % (q_query, s)) for v in j['feed']['entry']: tl = v['media$group']['media$thumbnail'] for th in tl: if th['width'] == 120: t = th['url'] for l in v['link']: if l['type'] == 'text/html' and l['rel'] == "alternate": u = l['href'] import urlparse url = urlparse.urlparse(u) vid = urlparse.parse_qs(url.query)['v'][0] results.append({ "style" : "video", "thumbnail" : t, "url" : u, "source" : "YouTube", "user" : v['author'][0]['name']["$t"], "title" : v['title']['$t'], "player" : '<iframe width="700" height="425" src="http://www.youtube.com/embed/%s?autoplay=1"></iframe>' % vid }) return results
def localwiki(q_query, raw_query, page): results = [] try: j = getJson("http://localhost/wiki/search.php?q=%s" % q_query, True) except Exception: return {} for r in j: results.append({ "url" : u"http://localhost/wiki/doku.php?id=%s" % urllib.quote(r['id']), "display_url" : "LocalWiki://%s" % r['id'], "snippet" : r['snip'], "title" : unicode(r['title']), "style" : u"special" }) return results
def lastfm(q_query, raw_query, page): results = [] j = getJson("http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=%s&api_key=%s&format=json&limit=3" % (q_query, config.lastfm_key)) if "artist" in j['results']['artistmatches']: for artist in j['results']['artistmatches']['artist']: for th in artist['image']: if th['size'] == "large": t = th['#text'] results.append({ "style" : "lastfm music", "title" : artist['name'], "url" : artist['url'], "display_url" : artist['url'], "snippet" : '<img src="%s" />' % t }) return results
def flickr(q_query, raw_query, page): results = [] config.pre_output = header % "Flickr" config.result_class = ' thumbnails' j = getJson("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%s&text=%s&format=json&nojsoncallback=1&per_page=20" % (config.flickr_key, q_query), default='{ "photos" : { "photo" : [] } }' ) if len(j['photos']['photo']) != 0: for r in j['photos']['photo']: results.append({ "url" : "http://flickr.com/photos/%s/%s" % (r['owner'], r['id']), "display_url" : "http://flic.kr/%s" % base58(int(r['id'])), "title" : shorten(r['title'], 140), "user" : r['owner'], "style" : "image", "source" : "flickr", "image" : "http://farm%s.static.flickr.com/%s/%s_%s_t.jpg" % (r['farm'], r['server'], r['id'], r['secret']), }) return results
def instagram(q_query, raw_query, page): results = [] config.pre_output = header % "Instagram" config.result_class = ' thumbnails' j = getJson("https://api.instagram.com/v1/tags/%s/media/recent?client_id=%s" % ( q_query, config.instagram_key ), default='{ "data" : [] }' ) if len(j['data']) != 0: for r in j['data']: results.append({ "url" : r['link'], "style" : "image", "source" : "Instagram", "display_url" : r['link'], "title" : shorten(r['caption']['text'], 140), "user" : r['user']['username'], "image" : r['images']['low_resolution']['url'], }) return results
def bing(q_query, raw_query, page): results = [] import magic j = getJson("http://api.bing.net/json.aspx?sources=Web+RelatedSearch&AppId=%s&query=%s&Web.Count=10&Web.Offset=%s&Market=%s" % (config.bing_api_key, q_query, (page-1) * 10, config.default_location), default='{ "SearchResponse" : { "Web" : { } } }' ) if 'Results' in j['SearchResponse']['Web']: for r in j['SearchResponse']['Web']['Results']: if 'Description' not in r: r['Description'] = '' go_magic = False for m in magic.magic.keys(): if m in r['Url']: go_magic = magic.magic[m] if go_magic != False: try: set_debug_info("") x = go_magic(r['Url'], r) if x is not None: x["style"] += " bing" results.append(x) else: add_normal(r, results) except Exception as ex: import traceback results.append({ "style" : "error", "title" : "An error occured: %s" % repr(ex) + "<br/>" + traceback.format_exc().replace("\n", "<br/>"), "url" : r['Url'], "snippet" : "<pre>%s</pre>" % debug_info }) add_normal(r, results) else: add_normal(r, results) if 'RelatedSearch' in j['SearchResponse']: s = "<ul>" for r in j['SearchResponse']['RelatedSearch']['Results']: s += "<li><a href='?q=%s'>%s</a></li>" % (urllib.quote(r['Title'].encode("utf8")), r['Title']) s += "</ul>" results.append({ "style" : "related_search bing", "title" : "Releated Searches", "snippet" : s }) return results
def ddg(q_query, raw_query, page): results = [] j = getJson("http://api.duckduckgo.com/?q=%s&format=json" % q_query) if j['Abstract'] != '': results.append({ "url" : j["AbstractURL"], "title" : j["Heading"], "style" : "duckduckgo magic", "display_url" : "Source: %s (via DDG)" % j['AbstractSource'], "snippet" : j["Abstract"] }) if j['Definition'] != '': results.append({ "url" : j['DefinitionURL'], "snippet" : j['Definition'], "style" : "duckduckgo magic", "display_url" : "Source: %s (via DDG)" % j['DefinitionSource'] }) if j['Answer'] != '': results.append({ "style" : "duckduckgo magic", "display_url" : "%s on DuckDuckGo" % j['AnswerType'], "url" : "http://ddg.gg?q=%s" % q_query, "snippet" : j['Answer'] }) for r in j['Results']: results.append({ "style" : "duckduckgo", "url" : r['FirstURL'], "snippet" : r['Text'], "title" : r['Result'] }) s = '<ul>' for r in j['RelatedTopics']: if 'Result' in r: s += '<li>%s</li>' % r['Result'] if s != '<ul>': results.append({ "style" : "duckduckgo related", "snippet" : s, "title" : "DuckDuckGo Related Topics" }) return results
def instagram(q_query, raw_query, page): results = [] config.pre_output = header % "Instagram" config.result_class = ' thumbnails' j = getJson( "https://api.instagram.com/v1/tags/%s/media/recent?client_id=%s" % (q_query, config.instagram_key), default='{ "data" : [] }') if len(j['data']) != 0: for r in j['data']: results.append({ "url": r['link'], "style": "image", "source": "Instagram", "display_url": r['link'], "title": shorten(r['caption']['text'], 140), "user": r['user']['username'], "image": r['images']['low_resolution']['url'], }) return results
def youtube(q_query, raw_query, page): config.result_class = ' thumbnails' results = [] s = ((page - 1) * 12) + 1 j = getJson( 'https://gdata.youtube.com/feeds/api/videos?alt=json&q=%s&start-index=%i&max-results=12' % (q_query, s)) for v in j['feed']['entry']: tl = v['media$group']['media$thumbnail'] for th in tl: if th['width'] == 120: t = th['url'] for l in v['link']: if l['type'] == 'text/html' and l['rel'] == "alternate": u = l['href'] import urlparse url = urlparse.urlparse(u) vid = urlparse.parse_qs(url.query)['v'][0] results.append({ "style": "video", "thumbnail": t, "url": u, "source": "YouTube", "user": v['author'][0]['name']["$t"], "title": v['title']['$t'], "player": '<iframe width="700" height="425" src="http://www.youtube.com/embed/%s?autoplay=1"></iframe>' % vid }) return results