PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/main.py

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