#!/usr/bin/env python # coding=UTF-8 # tinypaste.py # writes the input string to tinypaste.com and returns the URL import os import re import hashlib import urllib.request, urllib.parse, urllib.error class tinypaste(): def __init__(self): self.name = None self.private = 0 self.mode = '' self.authFile = os.path.join(os.environ['HOME'], ".tinypaste.py") self.token = self.authGet() def send(self, code): url = 'http://tinypaste.com/api/create.json' info = '' values = {'paste': code, 'is_code': '1'} if self.name is not None: values["title"] = self.name if self.token is not None: values["authenticate"] = self.token if self.private == 1: values["is_private"] = 1 values["password"] = self.password info = ' (Use the password "' + self.password \ + '" to show the posting.)' data = urllib.parse.urlencode(values).encode('ASCII') response = urllib.request.urlopen(url, data) responseData = str(response.read()) try: new_url = re.findall(r'{"result":{"response":"([^"]+)"}}', \ responseData)[0] return("http://tinypaste.com/" + new_url + info) except: return(responseData) def authGet(self): try: authFile = open(self.authFile, "r") return(authFile.read().strip()) except: return(None) def authWrite(self, credentials): try: # fix string if it has been splited on an = in the name or password tmp = "" for i in credentials: if tmp == "": tmp += i else: tmp += '=' + i tmp = tmp.split(':') username = tmp[0] password = str(tmp[1]).encode('UTF-8') passHash = hashlib.md5(password).hexdigest() authFile = open(self.authFile, "w") authFile.write(username + ':' + passHash) authFile.close() print('Credentials written to ~/.tinypaste.py') except: print('Can not write credentials to ~/.tinypaste.py') def authClear(self): try: os.remove(self.authFile) print('Cleared credential file.') except: print('Can not clear credential file ~/.tinypaste.py. \ Maybe it just does not exist.') if __name__ == "__main__": import sys paste = tinypaste() for arg in sys.argv: if arg in ("-h", "--help"): bold = '\033[1m' reset = '\033[0;0m' print("Usage: STDOUT | tinypaste.py [OPTIONS]\n\nOptions:") print(bold + " --name=NAME" \ + reset + "\t\t\tsets title for posting") print(bold + " --private=PASSWORD" \ + reset + "\t\tnon-public posting with password protection") print(bold + " --auth='USERNAME:PASSWORD'" \ + reset + "\tstores username and hashed password \ \n\t\t\t\tin ~/.tinypaste.py") print(bold + " --clearauth" \ + reset + "\t\t\tremoves the saved username and password") sys.exit() if ("--name") in arg: paste.name = arg.split('=')[1] if ("--private") in arg: paste.private = 1 paste.password = arg.split('=')[1] if ("--auth") in arg: paste.mode = 'authWrite' credentials = arg.split('=')[1:] if ("--clearauth") in arg: paste.mode = 'authClear' if paste.mode == 'authWrite': paste.authWrite(credentials) elif paste.mode == 'authClear': paste.authClear() else: code = sys.stdin.buffer.read().decode(sys.stdout.encoding) if code == '': print("No input data to send …") sys.exit() new_url = paste.send(code) print(new_url)