1. #!/usr/bin/env python
  2. # coding=UTF-8
  3. # tinypaste.py
  4. # writes the input string to tinypaste.com and returns the URL
  5. #
  6. # Code borrowed from Friedrich Preuß <[email protected]>
  7. import os, re
  8. class tinypaste(object):
  9. def __init__(self):
  10. self.name = None
  11. self.code = 0
  12. self.private = 0
  13. self.token = self.getauth()
  14. def send(self, code):
  15. import urllib
  16. import urllib2
  17. url = 'http://tny.cz/api/create.json'
  18. values = {'paste': code,
  19. 'is_code': self.code}
  20. if self.name is not None:
  21. values["title"] = self.name
  22. if self.token is not None:
  23. values["authenticate"] = self.token
  24. data = urllib.urlencode(values)
  25. req = urllib2.Request(url, data)
  26. response = urllib2.urlopen(req).read().strip()
  27. #Try for a good response
  28. res = re.findall(r'{"result":{"response":"([^"]+)"}}', response)
  29. if len(res) == 1:
  30. print "http://tny.cz/%s"%res[0]
  31. else:
  32. res = re.findall(r'{"result":{"error":"([^"]+)"}}', response)
  33. if len(res) > 0:
  34. import subprocess
  35. subp = subprocess.Popen(["osascript"], stdin=subprocess.PIPE, stdout=open(os.devnull, "w"), stderr=subprocess.STDOUT)
  36. subp.stdin.write('tell application "Finder"\nactivate\ndisplay dialog "Tinypaste error: %s\nPlease let the developer know."\nend tell\n'%res[0])
  37. subp.stdin.close()
  38. return(0)
  39. def getauth(self):
  40. try:
  41. fhand = open(os.path.join(os.environ['HOME'], ".tny.py"), "r")
  42. return fhand.read().strip()
  43. except:
  44. return None
  45. if __name__ == "__main__":
  46. import sys
  47. paste = tinypaste()
  48. for arg in sys.argv:
  49. if arg in ("-h", "--help"):
  50. print "Usage: STDOUT | gist.py [--title=TITLE] [--code] [--private=y]\Put your tny.cz username:md5(password) in $HOME/.tinypaste.py"
  51. sys.exit()
  52. if ("--name") in arg:
  53. paste.name = arg.split('=')[1]
  54. if ("--code") in arg:
  55. paste.code = 1
  56. if ("--private") in arg:
  57. paste.private = 1
  58. code = sys.stdin.read()
  59. if code == '':
  60. print "No input data to send …"
  61. sys.exit()
  62. paste.send(code)