import flask
import urllib
import re
import cgi
import xml.sax.saxutils
import curlfe

app = flask.Flask(__name__)
#app.debug = True

def replace(match):
    url = match.group(1)
    url = xml.sax.saxutils.unescape(url)
    url = url[:url.find('&')]
    #url = url[:url.find('&amp;')]
    url = urllib.unquote(url)
    #print url
    url = xml.sax.saxutils.quoteattr(url)
    return 'href=%s' % url

@app.route('/')
@app.route('/search')
def index():
    query = flask.request.args.get('q')
    if query:
        equery = cgi.escape(query)
        url = 'http://www.google.com/search?q=%s' % equery
    else:
        url = 'http://www.google.com/'
    
    fe = curlfe.CurlFe()
    # http://curl.haxx.se/mail/curlpython-2007-07/0001.html
    # curl insists on a str, not unicode, on python 2
    url = url.encode('utf8')
    content = fe.fetch(url)
    content = re.sub(r'<script[^>]*>.*?</script>', '', content)
    content = re.sub(r'href="/url\?q=([^"]+)"', replace, content)
    # this causes forms to submit to google, not good
    #content = content.replace(r'<head>', '<head><base href="http://www.google.com/">')
    content = re.sub(r'url\(/(?!/)', 'url(http://www.google.com/', content)
    return content

if __name__ == '__main__':
    app.run()

@@ -448,21 +448,70 @@
 ():%0A    
-query
+args = %7B%7D%0A    for key in %5B'q', 'start'%5D:%0A        value
  = flask
@@ -532,61 +532,121 @@
 get(
-'q')%0A
+key)%0A   
     
+ 
 if 
-query:%0A        equery = cgi.escape(
+value is not None:%0A            args%5Bkey%5D = value%0A    query = urllib.urlencode(args)%0A    if 
 query
-)
+:
 %0A   
@@ -690,17 +690,14 @@
 rch?
-q=
 %25s' %25 
-e
 quer

Make pagination work in search results