- #!/usr/bin/env python
- # coding=UTF-8
-
- # tinypaste.py
- # writes the input string to tinypaste.com and returns the URL
- #
- # Code borrowed from Friedrich Preuà <[email protected]>
-
- import os, re
-
- class tinypaste(object):
- def __init__(self):
- self.name = None
- self.code = 0
- self.private = 0
- self.token = self.getauth()
-
- def send(self, code):
- import urllib
- import urllib2
-
- url = 'http://tny.cz/api/create.json'
- values = {'paste': code,
- 'is_code': self.code}
- if self.name is not None:
- values["title"] = self.name
- if self.token is not None:
- values["authenticate"] = self.token
- data = urllib.urlencode(values)
- req = urllib2.Request(url, data)
- response = urllib2.urlopen(req).read().strip()
- #Try for a good response
- res = re.findall(r'{"result":{"response":"([^"]+)"}}', response)
- if len(res) == 1:
- print "http://tny.cz/%s"%res[0]
- else:
- res = re.findall(r'{"result":{"error":"([^"]+)"}}', response)
- if len(res) > 0:
- import subprocess
- subp = subprocess.Popen(["osascript"], stdin=subprocess.PIPE, stdout=open(os.devnull, "w"), stderr=subprocess.STDOUT)
- subp.stdin.write('tell application "Finder"\nactivate\ndisplay dialog "Tinypaste error: %s\nPlease let the developer know."\nend tell\n'%res[0])
- subp.stdin.close()
- return(0)
-
- def getauth(self):
- try:
- fhand = open(os.path.join(os.environ['HOME'], ".tny.py"), "r")
- return fhand.read().strip()
- except:
- return None
-
- if __name__ == "__main__":
- import sys
- paste = tinypaste()
- for arg in sys.argv:
- if arg in ("-h", "--help"):
- print "Usage: STDOUT | gist.py [--title=TITLE] [--code] [--private=y]\Put your tny.cz username:md5(password) in $HOME/.tinypaste.py"
- sys.exit()
- if ("--name") in arg:
- paste.name = arg.split('=')[1]
- if ("--code") in arg:
- paste.code = 1
- if ("--private") in arg:
- paste.private = 1
-
- code = sys.stdin.read()
- if code == '':
- print "No input data to send â¦"
- sys.exit()
-
- paste.send(code)