1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch
import string
import urllib
import re

class MainPage(webapp.RequestHandler):
  def get(self):
    q = urllib.quote(self.request.get('q').encode('utf8'))
    url = 'http://www.google.com.tw/search?q=' + q + '&start=' + str(self.request.get('start',0))
    callback = self.request.get('callback')
    self.response.headers['Content-Type'] = 'text/html'
    #"""
    try:

      urlObject = urlfetch.fetch(url, None, 'GET', {}, False, True)
      searchResult = re.findall('href="(http[-a-zA-Z0-9@:%_\+.~#?&//=]+)"', urlObject.content)
      searchResult = filter(lambda x:'google.com' not in x,searchResult)
      json = '["' + '","'.join(searchResult) + '"]'

      if callback:
        callback = str(callback)
        self.response.out.write(callback)
        self.response.out.write('(')
        self.response.out.write(json)
        self.response.out.write(');')
      else:
        self.response.out.write(json)

    except:
    #"""
      self.response.out.write(url)
    
application = webapp.WSGIApplication(
                                     [('/', MainPage)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()