PageRenderTime 49ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/main.py

https://github.com/akshaytyagi/quantum
Python | 188 lines | 180 code | 7 blank | 1 comment | 1 complexity | 77057cccf0a5320faaa6ca2f5677fc3b MD5 | raw file
  1import os
  2import webapp2
  3import cgi
  4import json
  5import jinja2
  6import urllib2
  7from google.appengine.ext import db
  8
  9opener = urllib2.build_opener()
 10opener.addheaders = [('User-agent', 'Mozilla/5.0')]
 11
 12template_dir = os.path.join(os.path.dirname(__file__), 'templates')
 13jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
 14                               autoescape = True)
 15
 16def render_str(template, **params):
 17    t = jinja_env.get_template(template)
 18    return t.render(params)
 19
 20class BaseHandler(webapp2.RequestHandler):
 21    def render(self, template, **kw):
 22        self.response.out.write(render_str(template, **kw))
 23
 24    def write(self, *a, **kw):
 25        self.response.out.write(*a, **kw)
 26
 27
 28#Wikipedia
 29def topic_summary(w):
 30    k=str(w);
 31    l=k.lower()
 32    t=l.replace (" ", "+")        
 33    link="http://api.duckduckgo.com/?q="+t+"&format=json&pretty=1"
 34    try:
 35        req = urllib2.Request(link, None)
 36        f = opener.open(req)
 37        d=json.load(f)
 38    except:
 39        return None
 40    if d["Definition"]!="":
 41        return d["Definition"]
 42    if d["AbstractText"]!="":
 43        return d["AbstractText"]
 44    ret="<h2>Related topics</h2>"
 45    for i in d["RelatedTopics"]:
 46        if i["Result"]!="":
 47            ret+= i["Text"]+'<br>'
 48    return ret
 49        
 50
 51
 52
 53
 54#CrunchBase
 55def cb(term):
 56    term=term.replace(" ", "-")
 57    link="http://api.crunchbase.com/v/1/company/"+term+".js"
 58    try:
 59        req = urllib2.Request(link, None)
 60        f = opener.open(req)
 61        d=json.load(f)
 62    except:
 63        return None
 64    if 'error' in d:
 65        return None
 66    else:
 67        return d['name']+"<br>"+d['homepage_url']+"<br>"+d['category_code']
 68
 69#Twitter
 70def twit(term):
 71    term=term.replace(" ", "")
 72    link="http://search.twitter.com/search.json?q=%23"+term
 73    try:
 74        req = urllib2.Request(link, None)
 75        f = opener.open(req)
 76        d=json.load(f)
 77    except:
 78        return None
 79    length=len(d['results'])
 80    i=1
 81    answer=""
 82    if length<10:
 83        for i in range(1,length):
 84             answer+=d['results'][i]['text']+"<br>"
 85    else:
 86        for i in range(1,10):
 87             answer+=d['results'][i]['text']+"<br>"
 88    return answer         
 89
 90#IMDB
 91def imdb(term):
 92    term=term.replace(" ","+")
 93    link="http://www.deanclatworthy.com/imdb/?q="+term
 94    try:
 95        req = urllib2.Request(link, None)
 96        f = opener.open(req)
 97        d=json.load(f)
 98    except:
 99        return None
100    if 'error' in d:
101        return None
102    else:
103        return d['title']+"<br>"+d['rating']+"<br>"+d['runtime']+"<br>"+d['genres']+"<br>"+d['year']+"<br>"+d['imdburl']
104
105#gmaps
106def gmaps(term):
107    striped=term.strip()
108    loc=striped.replace(" ","+")
109    link='http://maps.googleapis.com/maps/api/staticmap?center='+loc+'&zoom=12&size=600x300&maptype=roadmap&sensor=false&key=AIzaSyBAspYdcOeGNE_gKhbLtOTrIUwqKByrJ9M'
110    try:
111        infile = opener.open(link)
112        page = infile.read()
113    except:
114        return None    
115    return link
116
117#Facebook
118def faceb(term):
119    term=term.replace(" ", "")
120    link="http://graph.facebook.com/"+term
121    try:
122        req = urllib2.Request(link, None)
123        f = opener.open(req)
124        d=json.load(f)
125    except:
126        return None
127    if 'error' in d:
128        return None
129    elif 'picture' in d:
130        return d['picture']+"<br>"+d['link']+"<br>"+d['about']+"<br>"+str(d['likes'])
131    else:
132        return d['link']+"<br>"+d['about']+"<br>"+str(d['likes'])
133
134#TechIRIS
135def iris(term):
136    term=term.replace(" ", "")
137    link="http://thetechiris.com/api/get_search_results/?dev=1&search="+term
138    try:
139        req = urllib2.Request(link, None)
140        f = opener.open(req)
141        d=json.load(f)
142    except:
143        return None
144    if d['count']==0:
145        return None
146    elif d['count']>=2:
147        return d['posts'][0]['title']+"<br>"+d['posts'][0]['url']+"<br>"+d['posts'][1]['title']+"<br>"+d['posts'][1]['url']
148    else:
149        return d['posts'][0]['title']+"<br>"+d['posts'][0]['url']
150
151class MainPage(BaseHandler):
152    def get(self):
153        self.render("search-form.html")
154
155    def post(self):
156        q=self.request.get("q")
157        
158        params = dict(q=q)
159        
160        p=topic_summary(q)
161        p2=cb(q)
162        p3=twit(q)
163        p4=imdb(q)
164        img_url=gmaps(q)
165        p5=faceb(q)
166        p6=iris(q)
167               
168        if p:
169            self.write("<h1> Results wiki</h1>"+p)
170        if p2:
171            self.write("<br><br><h1> Results CB</h1>"+p2)
172        if p3:
173            self.write("<br><br><h1> Results Twitter</h1>"+p3)
174        if p4:
175            self.write("<br><br><h1> Results IMDB</h1>"+p4)
176        if img_url:
177            self.write('<br><br><h1> Results maps</h1>'+ '<img src="'+img_url+'" >')
178        if p5:
179            self.write("<br><br><h1> Results Fb</h1>"+p5)
180        if p6:
181            self.write("<br><br><h1> Results TechIRIS</h1>"+p6)
182
183        if not p and not p2 and not p3 and not p4 and not img_url and not p5 and not p6:
184            params['error_q']="That's not a valid term!"
185            self.render('search-form.html', **params)    
186
187        
188app = webapp2.WSGIApplication([('/', MainPage)], debug=True)