Guest User

Untitled

a guest
Jun 19th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. #! /usr/bin/env python
  2. import urllib2, urllib, optparse, sys, os.path
  3.  
  4. # user editable config
  5. #
  6. # user key
  7. # use -k option to get this
  8. userkey = ""
  9. #
  10. # automatic syntax highlighting choice by file extension
  11. # defaults are pastebin's common extensions from web interface
  12. # add your own based on the filetypes you use
  13. syntax = {".sh":"bash", ".c":"c", ".h":"c", ".cpp":"cpp", ".cs":"csharp", ".css":"css", ".html":"html", ".htm":"html", ".java":"java", ".js":"javascript", ".lua":"lua", ".pl":"perl", ".php":"php", ".py":"python", ".rb":"rails", ".patch":"diff"}
  14. #
  15. # end of config
  16.  
  17. parser = optparse.OptionParser(usage="usage: %prog [OPTIONS] [FILE]...\nPastebin FILE(s) or standard input")
  18. parser.add_option("-n", "--name", dest="name", metavar="NAME", help="Name/title of paste (ignored for multiple files)")
  19. parser.add_option("-s", "--syntax", dest="syntax", metavar="FORMAT", help="Specify syntax format (common types will be guessed from filename otherwise, use -s none to stop this)")
  20. parser.add_option("-p", "--private", dest="private", action="store_true", default=False, help="Private paste")
  21. parser.add_option("-e", "--expire", dest="expire", metavar="EXPIRY", help="Specify expiry date: N = never, 10M = 10 minutes, 1H = 1 hour, 1D = 1 day, 1M = 1 month")
  22. parser.add_option("-i", "--interactive", dest="interactive", default=False, action="store_true", help="Interactive mode - echoes everything received on stdin to stdout so programs which require console input can be used piped into Pastebin")
  23. parser.add_option("-q", "--quiet", dest="quiet", action="store_true", default=False, help="Suppress information messages and print only URLs")
  24. parser.add_option("-l", "--login", dest="login", action="store_true", default=False, help="Login to Pastebin")
  25. parser.add_option("-k", "--key", dest="getkey", action="store_true", default=False, help="Return user key to avoid giving username and password in future")
  26.  
  27. (options, args) = parser.parse_args()
  28. devkey = "6c71766cdadff9f33347e80131397ac2" # don't edit this
  29.  
  30. def get_user_key():
  31. username = raw_input("Enter username: ")
  32. from getpass import getpass
  33. password = getpass()
  34.  
  35. request = {"api_dev_key":devkey, "api_user_name":username, "api_user_password":password}
  36.  
  37. try:
  38. reply = urllib2.urlopen("http://pastebin.com/api/api_login.php", urllib.urlencode(request))
  39. except urllib2.URLError:
  40. if not options.quiet:
  41. print "Error uploading", filename + ":", "Network error"
  42. exit(2)
  43. else:
  44. reply = reply.read()
  45. if "Bad API request" in reply:
  46. if not options.quiet:
  47. print "Pastebin login error:", reply
  48. exit(2)
  49. else:
  50. return reply
  51.  
  52. def guess_syntax(ext):
  53. return syntax.get(ext, "text")
  54.  
  55. if options.getkey:
  56. userkey = get_user_key()
  57. print "Your user key is:", userkey
  58. print "You may edit the userkey value at the top of this script to skip username/password requests in future."
  59. if not args:
  60. exit()
  61.  
  62. # validate expiry/format options
  63. if options.syntax:
  64. syntaxes = ('4cs', '6502acme', '6502kickass', '6502tasm', 'abap', 'actionscript',
  65. 'actionscript3', 'ada', 'algol68', 'apache', 'applescript', 'apt_sources', 'asm', 'asp',
  66. 'autoconf', 'autohotkey', 'autoit', 'avisynth', 'awk', 'bascomavr', 'bash', 'basic4gl',
  67. 'bibtex', 'blitzbasic', 'bnf', 'boo', 'bf', 'c', 'c_mac', 'cil', 'csharp', 'cpp', 'cpp-qt',
  68. 'c_loadrunner', 'caddcl', 'cadlisp', 'cfdg', 'chaiscript', 'clojure', 'klonec', 'klonecpp',
  69. 'cmake', 'cobol', 'coffeescript', 'cfm', 'css', 'cuesheet', 'd', 'dcs', 'delphi', 'oxygene',
  70. 'diff', 'div', 'dos', 'dot', 'e', 'ecmascript', 'eiffel', 'email', 'epc', 'erlang', 'fsharp',
  71. 'falcon', 'fo', 'f1', 'fortran', 'freebasic', 'gambas', 'gml', 'gdb', 'genero', 'genie',
  72. 'gettext', 'go', 'groovy', 'gwbasic', 'haskell', 'hicest', 'hq9plus', 'html4strict', 'html5',
  73. 'icon', 'idl', 'ini', 'inno', 'intercal', 'io', 'j', 'java', 'java5', 'javascript', 'jquery',
  74. 'kixtart', 'latex', 'lb', 'lsl2', 'lisp', 'llvm', 'locobasic', 'logtalk', 'lolcode',
  75. 'lotusformulas', 'lotusscript', 'lscript', 'lua', 'm68k', 'magiksf', 'make', 'mapbasic',
  76. 'matlab', 'mirc', 'mmix', 'modula2', 'modula3', '68000devpac', 'mpasm', 'mxml', 'mysql',
  77. 'newlisp', 'text', 'nsis', 'oberon2', 'objeck', 'objc', 'ocaml-brief', 'ocaml', 'pf', 'glsl',
  78. 'oobas', 'oracle11', 'oracle8', 'oz', 'pascal', 'pawn', 'pcre', 'per', 'perl', 'perl6',
  79. 'php', 'php-brief', 'pic16', 'pike', 'pixelbender', 'plsql', 'postgresql', 'povray',
  80. 'powershell', 'powerbuilder', 'proftpd', 'progress', 'prolog', 'properties', 'providex',
  81. 'purebasic', 'pycon', 'python', 'q', 'qbasic', 'rsplus', 'rails', 'rebol', 'reg', 'robots',
  82. 'rpmspec', 'ruby', 'gnuplot', 'sas', 'scala', 'scheme', 'scilab', 'sdlbasic', 'smalltalk',
  83. 'smarty', 'sql', 'systemverilog', 'tsql', 'tcl', 'teraterm', 'thinbasic', 'typoscript',
  84. 'unicon', 'uscript', 'vala', 'vbnet', 'verilog', 'vhdl', 'vim', 'visualprolog', 'vb',
  85. 'visualfoxpro', 'whitespace', 'whois', 'winbatch', 'xbasic', 'xml', 'xorg_conf', 'xpp',
  86. 'yaml', 'z80', 'zxbasic')
  87. if options.syntax.lower() not in syntaxes:
  88. if not options.quiet:
  89. print "Error: unknown syntax. Valid values are (detailed explanation: http://pastebin.com/api#5 ):"
  90. print syntaxes
  91. exit(1)
  92.  
  93. if options.expire:
  94. expires = ("N", "10M", "1H", "1D", "1M")
  95. if options.expire.upper() not in expires:
  96. if not options.quiet:
  97. print "Error: unknown expiry time. Valid values are (see --help):"
  98. print expires
  99. exit(1)
  100.  
  101. if not args:
  102. # read stdin until EOF
  103. lines = []
  104. for line in sys.stdin:
  105. lines.append(line)
  106. print line,
  107. add = {"api_paste_code":"".join(lines), "filename":"stdin"}
  108. if options.syntax:
  109. add["api_paste_format"] = options.syntax.lower()
  110. if options.name:
  111. add["api_paste_name"] = options.name
  112. targets = [add]
  113.  
  114. else:
  115. # read files from arguments
  116. targets = []
  117. for i in args:
  118. with open(i) as f:
  119. add = {"api_paste_code":f.read(), "filename":i}
  120. if (len(args) == 1) & (options.name != None):
  121. add["api_paste_name"] = options.name
  122. if options.syntax:
  123. add["api_paste_format"] = options.syntax.lower()
  124. else:
  125. add["api_paste_format"] = guess_syntax(os.path.splitext(i)[1])
  126. targets.append(add.copy())
  127.  
  128. if options.login:
  129. if not userkey:
  130. userkey = get_user_key()
  131. else:
  132. userkey = "" # unset userkey
  133.  
  134.  
  135. for target in targets:
  136. # make actual paste requests
  137. filename = target.pop("filename")
  138. target["api_dev_key"] = devkey
  139. target["api_option"] = "paste"
  140. if userkey:
  141. target["api_user_key"] = userkey
  142. if options.private:
  143. target["api_paste_private"] = "1"
  144. if options.expire:
  145. target["api_paste_expire_date"] = options.expire.upper()
  146. #print target
  147. try:
  148. req = urllib2.urlopen("http://pastebin.com/api/api_post.php", urllib.urlencode(target))
  149. except urllib2.URLError:
  150. if not options.quiet:
  151. print "Error uploading", filename + ":", "Network error"
  152. exit(2)
  153. else:
  154. reply = req.read()
  155. if "Bad API request" in reply:
  156. if not options.quiet:
  157. print "Error uploading", filename + ":", reply
  158. exit(2)
  159. else:
  160. if not options.quiet:
  161. print filename, "uploaded to:", reply
  162. else:
  163. print reply
Add Comment
Please, Sign In to add comment