Share Pastebin
Guest
Public paste!

phriedrich

By: a guest | Mar 15th, 2010 | Syntax: Python | Size: 2.33 KB | Hits: 241 | Expires: Never
Copy text to clipboard
  1. #!/usr/bin/env python
  2. # coding=UTF-8
  3.  
  4. # pastebin.py
  5. # writes the input string to pastebin.com and returns the URL
  6. #
  7. # Copyright 2007-2010 Friedrich Preuß <info@phriedrich.de>
  8. # all rights reserved, released under the terms of GPLv2
  9.  
  10. class pastebin:
  11.     def __init__(self):
  12.         from getpass import getuser
  13.         self.name = getuser()
  14.         self.expiry = "1D"
  15.         self.format = "Bash"
  16.         self.formatTable = {
  17.                 'Bash' : 8,
  18.                 'C' : 9,
  19.                 'C++' : 13,
  20.                 'CSS' : 16,
  21.                 'HTML' : 25,
  22.                 'Java' : 27,
  23.                 'JavaScript' : 28,
  24.                 'Lua' : 30,
  25.                 'None' : 1,
  26.                 'Perl' : 40,
  27.                 'PHP' : 41,
  28.                 'Python' : 42,
  29.                 'Rails' : 67
  30.                 }
  31.  
  32.     def send(self, code):
  33.         import urllib.request, urllib.parse, urllib.error
  34.  
  35.         try:
  36.             format = self.formatTable[self.format.title()]
  37.         except:
  38.             format = 1
  39.  
  40.         url = 'http://pastebin.com/post.php'
  41.         values = {'paste_form' : '/post.php',
  42.                 'paste_parent_key': '',
  43.                 'paste_subdomain' : '',
  44.                 'paste_code' : code,
  45.                 'paste_format' : format,
  46.                 'paste_expire_date' : self.expiry.upper(),
  47.                 'paste_private' : '0',
  48.                 'paste_name' : self.name,
  49.                 'paste_email' : '',
  50.                 'submit' : 'submit'}
  51.         data = urllib.parse.urlencode(values)
  52.         response = urllib.request.urlopen(url, data)
  53.         new_url = response.geturl()
  54.         response.close()
  55.         return(new_url)
  56.  
  57. if __name__ == "__main__":
  58.     import sys
  59.     paste = pastebin()
  60.     for arg in sys.argv:
  61.         if arg in ("-h", "--help"):
  62.             print("Usage: STDOUT | pastebin.py [--name=USERNAME] [--format=FORMAT] [--expiry=(N|10M|1H|1D|1M)]")
  63.             sys.exit()  
  64.         if ("--name") in arg:
  65.             paste.name = arg.split('=')[1]
  66.         if ("--format") in arg:
  67.             paste.format = arg.split('=')[1]
  68.         if ("--expiry") in arg:
  69.             paste.expiry = arg.split('=')[1]
  70.  
  71.     code = sys.stdin.read()
  72.     if code == '':
  73.         print("No input data to send …")
  74.         sys.exit()
  75.  
  76.     ret_url = paste.send(code)
  77.     print(ret_url)