Created
May 27, 2009 20:21
-
-
Save rtyler/118887 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| import HTMLParser | |
| import os | |
| import re | |
| import subprocess | |
| import sys | |
| import urllib | |
| PASTEBIN = 'http://pastebin' | |
| SUFFIX_MAP = { 'py' : 'python', 'js' : 'javascript', 'tmpl' : 'html4strict', | |
| 'html' : 'html4strict', 'css' : 'css', 'sql' : 'mysql'} | |
| def usage(): | |
| prog = sys.argv[0] | |
| print ''' %s can be used with pipes or arguments, i.e.''' % prog | |
| print ''' %% cat filename | %s''' % prog | |
| print ''' %% %s <filename> (this will try to autodetect file type''' % prog | |
| print ''' %s can also read pastes by the hash in the URL, i.e.:''' % prog | |
| print ''' %% %s <hash>''' % prog | |
| return 0 | |
| def main(): | |
| if '-h' in sys.argv or '--help' in sys.argv: | |
| return usage() | |
| if len(sys.argv) > 2: | |
| print 'ERROR! %s can only take one file at a time!' % sys.argv[0] | |
| usage() | |
| return -1 | |
| paste = None | |
| data = dict( | |
| format = 'python', | |
| paste = 'Send', | |
| ) | |
| if len(sys.argv) == 1: | |
| paste = sys.stdin.read() | |
| else: | |
| filename = sys.argv[1] | |
| try: | |
| fd = open(filename, 'r') | |
| except IOError: | |
| # Perhaps this is a hash? | |
| response = urllib.urlopen(PASTEBIN + sys.argv[1]).read() | |
| code = PastebinParser.process(response) | |
| proc = subprocess.Popen(os.getenv('PAGER', default='less'), | |
| stdin=subprocess.PIPE, shell=True) | |
| proc.stdin.write(code) | |
| proc.stdin = sys.stdin | |
| return proc.wait() | |
| paste = fd.read() | |
| fd.close() | |
| if filename[-2:] in SUFFIX_MAP: | |
| data['format'] = SUFFIX_MAP[filename[-2:]] | |
| if not paste: | |
| print 'ERROR! Couldn\'t get a paste :(' | |
| return -1 | |
| data['code2'] = paste | |
| if os.getenv('USER'): | |
| data['poster'] = os.getenv('USER') | |
| response = urllib.urlopen(PASTEBIN, urllib.urlencode(data)).read() | |
| exp = '<input type="hidden" name="parent_pid" value="([\w:/\.]*)"/>' | |
| pid = re.compile(exp).search(response).group(1) | |
| url = PASTEBIN + pid | |
| print 'pasted to: %s (%s)' % (url, pid) | |
| return 0 | |
| class PastebinParser(HTMLParser.HTMLParser): | |
| buffer = [] | |
| in_code = False | |
| def handle_starttag(self, tag, attrs): | |
| attrs = dict(attrs) | |
| if tag == 'textarea' and attrs.get('name') == 'code2': | |
| self.in_code = True | |
| def handle_endtag(self, tag): | |
| if self.in_code and tag == 'textarea': | |
| self.in_code = False | |
| def handle_data(self, data): | |
| if self.in_code: | |
| self.buffer.append(data) | |
| def handle_entityref(self, name): | |
| if self.in_code: | |
| replacewith = None | |
| if name == 'gt': | |
| replacewith = '>' | |
| if name == 'lt': | |
| replacewith = '<' | |
| if name == 'amp': | |
| replacewith = '&' | |
| if name == 'quot': | |
| replacewith = '\"' | |
| if name == 'apost': | |
| replacewith = '\'' | |
| if replacewith: | |
| self.buffer.append(replacewith) | |
| @classmethod | |
| def process(cls, response): | |
| _p = cls() | |
| _p.feed(response) | |
| _p.close() | |
| return ''.join(_p.buffer) | |
| if __name__ == '__main__': | |
| rc = main() | |
| sys.exit(rc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment