Last active
September 2, 2015 18:20
-
-
Save dkuryakin/35b7ab3518eb40322a09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lxml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
import re | |
import lxml.html | |
import time | |
import urllib | |
import threading | |
import webbrowser | |
import SocketServer | |
import SimpleHTTPServer | |
from optparse import OptionParser | |
from StringIO import StringIO | |
def get_options(): | |
parser = OptionParser() | |
parser.add_option("-p", "--port", dest="port", help="Port. Default is 12345", metavar="PORT", | |
default=12345, type='int') | |
parser.add_option("-s", "--site", dest="site", help="Site. Default is 'http://gist.github.com'", | |
metavar="SITE", default='http://gist.github.com') | |
return parser.parse_args()[0] | |
def replacer(text): | |
def repl(m): | |
word = m.groups()[0] | |
if len(word) == 6: | |
return word + u'™' | |
return word | |
return re.sub(u'([\w\-]*)', repl, text, flags=re.U | re.M) | |
def serve(port, site): | |
class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): | |
def do_GET(self): | |
page_source = urllib.urlopen(site + self.path).read() | |
if '</html>' in page_source.lower(): | |
try: | |
doc = lxml.html.document_fromstring(page_source.decode('utf-8')) | |
for tag in doc.xpath('//*'): | |
tag.text = replacer(tag.text) if tag.text else tag.text | |
tag.tail = replacer(tag.tail) if tag.tail else tag.tail | |
new_page_source = lxml.html.tostring(doc, encoding='unicode') | |
ifile = StringIO(new_page_source.encode('utf-8')) | |
except UnicodeError: | |
ifile = StringIO(page_source) | |
else: | |
ifile = StringIO(page_source) | |
self.copyfile(ifile, self.wfile) | |
httpd = SocketServer.ForkingTCPServer(('', port), Proxy) | |
httpd.serve_forever() | |
def open_browser(url): | |
time.sleep(3) | |
webbrowser.open_new(url) | |
if __name__ == "__main__": | |
options = get_options() | |
start_url = 'http://127.0.0.1:{}/anonymous/06e0bd519490c8f03404'.format(options.port) | |
thread = threading.Thread(target=open_browser, args=(start_url,)) | |
thread.start() | |
serve(options.port, options.site) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment