/main.py
Python | 188 lines | 180 code | 7 blank | 1 comment | 2 complexity | 77057cccf0a5320faaa6ca2f5677fc3b MD5 | raw file
- import os
- import webapp2
- import cgi
- import json
- import jinja2
- import urllib2
- from google.appengine.ext import db
- opener = urllib2.build_opener()
- opener.addheaders = [('User-agent', 'Mozilla/5.0')]
- template_dir = os.path.join(os.path.dirname(__file__), 'templates')
- jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
- autoescape = True)
- def render_str(template, **params):
- t = jinja_env.get_template(template)
- return t.render(params)
- class BaseHandler(webapp2.RequestHandler):
- def render(self, template, **kw):
- self.response.out.write(render_str(template, **kw))
- def write(self, *a, **kw):
- self.response.out.write(*a, **kw)
- #Wikipedia
- def topic_summary(w):
- k=str(w);
- l=k.lower()
- t=l.replace (" ", "+")
- link="http://api.duckduckgo.com/?q="+t+"&format=json&pretty=1"
- try:
- req = urllib2.Request(link, None)
- f = opener.open(req)
- d=json.load(f)
- except:
- return None
- if d["Definition"]!="":
- return d["Definition"]
- if d["AbstractText"]!="":
- return d["AbstractText"]
- ret="<h2>Related topics</h2>"
- for i in d["RelatedTopics"]:
- if i["Result"]!="":
- ret+= i["Text"]+'<br>'
- return ret
-
- #CrunchBase
- def cb(term):
- term=term.replace(" ", "-")
- link="http://api.crunchbase.com/v/1/company/"+term+".js"
- try:
- req = urllib2.Request(link, None)
- f = opener.open(req)
- d=json.load(f)
- except:
- return None
- if 'error' in d:
- return None
- else:
- return d['name']+"<br>"+d['homepage_url']+"<br>"+d['category_code']
- #Twitter
- def twit(term):
- term=term.replace(" ", "")
- link="http://search.twitter.com/search.json?q=%23"+term
- try:
- req = urllib2.Request(link, None)
- f = opener.open(req)
- d=json.load(f)
- except:
- return None
- length=len(d['results'])
- i=1
- answer=""
- if length<10:
- for i in range(1,length):
- answer+=d['results'][i]['text']+"<br>"
- else:
- for i in range(1,10):
- answer+=d['results'][i]['text']+"<br>"
- return answer
- #IMDB
- def imdb(term):
- term=term.replace(" ","+")
- link="http://www.deanclatworthy.com/imdb/?q="+term
- try:
- req = urllib2.Request(link, None)
- f = opener.open(req)
- d=json.load(f)
- except:
- return None
- if 'error' in d:
- return None
- else:
- return d['title']+"<br>"+d['rating']+"<br>"+d['runtime']+"<br>"+d['genres']+"<br>"+d['year']+"<br>"+d['imdburl']
- #gmaps
- def gmaps(term):
- striped=term.strip()
- loc=striped.replace(" ","+")
- link='http://maps.googleapis.com/maps/api/staticmap?center='+loc+'&zoom=12&size=600x300&maptype=roadmap&sensor=false&key=AIzaSyBAspYdcOeGNE_gKhbLtOTrIUwqKByrJ9M'
- try:
- infile = opener.open(link)
- page = infile.read()
- except:
- return None
- return link
- #Facebook
- def faceb(term):
- term=term.replace(" ", "")
- link="http://graph.facebook.com/"+term
- try:
- req = urllib2.Request(link, None)
- f = opener.open(req)
- d=json.load(f)
- except:
- return None
- if 'error' in d:
- return None
- elif 'picture' in d:
- return d['picture']+"<br>"+d['link']+"<br>"+d['about']+"<br>"+str(d['likes'])
- else:
- return d['link']+"<br>"+d['about']+"<br>"+str(d['likes'])
- #TechIRIS
- def iris(term):
- term=term.replace(" ", "")
- link="http://thetechiris.com/api/get_search_results/?dev=1&search="+term
- try:
- req = urllib2.Request(link, None)
- f = opener.open(req)
- d=json.load(f)
- except:
- return None
- if d['count']==0:
- return None
- elif d['count']>=2:
- return d['posts'][0]['title']+"<br>"+d['posts'][0]['url']+"<br>"+d['posts'][1]['title']+"<br>"+d['posts'][1]['url']
- else:
- return d['posts'][0]['title']+"<br>"+d['posts'][0]['url']
- class MainPage(BaseHandler):
- def get(self):
- self.render("search-form.html")
- def post(self):
- q=self.request.get("q")
-
- params = dict(q=q)
-
- p=topic_summary(q)
- p2=cb(q)
- p3=twit(q)
- p4=imdb(q)
- img_url=gmaps(q)
- p5=faceb(q)
- p6=iris(q)
-
- if p:
- self.write("<h1> Results wiki</h1>"+p)
- if p2:
- self.write("<br><br><h1> Results CB</h1>"+p2)
- if p3:
- self.write("<br><br><h1> Results Twitter</h1>"+p3)
- if p4:
- self.write("<br><br><h1> Results IMDB</h1>"+p4)
- if img_url:
- self.write('<br><br><h1> Results maps</h1>'+ '<img src="'+img_url+'" >')
- if p5:
- self.write("<br><br><h1> Results Fb</h1>"+p5)
- if p6:
- self.write("<br><br><h1> Results TechIRIS</h1>"+p6)
- if not p and not p2 and not p3 and not p4 and not img_url and not p5 and not p6:
- params['error_q']="That's not a valid term!"
- self.render('search-form.html', **params)
-
- app = webapp2.WSGIApplication([('/', MainPage)], debug=True)