Skip to content

Instantly share code, notes, and snippets.

@smoser
Last active December 29, 2015 08:59
Show Gist options
  • Save smoser/7647304 to your computer and use it in GitHub Desktop.
Save smoser/7647304 to your computer and use it in GitHub Desktop.
upget: ubuntu pastebin getUbuntu pastebin requires login to download raw files. Thats annoying.This works around that. Only works for files pasted with 'plain' type (ie no syntax highlighting).
#!/usr/bin/python
# ubuntu pastebin getter [https://gist.github.com/smoser/7647304/]
# upget http://paste.ubuntu.com/8508062/ f.txt
# upget http://paste.ubuntu.com/8508062/ > f.txt
# wget http://sn.im/upget -O upget && chmod 755 upget
from __future__ import unicode_literals
import sys
import urllib3
try:
from html.parser import HTMLParser
except ImportError as e:
from HTMLParser import HTMLParser
class MyParser(HTMLParser):
data = ""
path = []
hit = ['td.code', 'div.paste', 'pre']
def handle_starttag(self, tag, attrs):
r = tag
for k, v in attrs:
if k == "class":
r += "." + v
self.path.append(r)
def handle_endtag(self, tag):
self.path.pop()
def handle_data(self, data):
if self.path[-(len(self.hit)):] == self.hit:
if self.data is None:
self.data = ""
self.data += data
def handle_entityref(self, ref):
self.handle_data(self.unescape("&%s;" % ref))
url = sys.argv[1]
if len(sys.argv) == 2 or sys.argv[2] == "-":
output = sys.stdout
elif len(sys.argv) == 3:
output = open(sys.argv[2], "w")
if url.endswith("/plain/"):
url = url.replace("/plain/", "")
myp = MyParser()
myp.feed(urllib3.PoolManager().request('GET', url).data.decode('utf-8'))
output.write(myp.data)
if myp.data[-1] != "\n":
output.write("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment