Advertisement
ditatompel

passtobin.py

Feb 4th, 2012
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.81 KB | None | 0 0
  1. #!/usr/bin/python
  2. # passtobin.py v 0.1
  3. # Uploads source file to pastebin.com using pastebin API.
  4. # You may use custom filename, post as guest / user
  5. # with private / public paste option and auto syntax
  6. # highlight for several filetypes, etc.
  7. #
  8. # coded by: ditatompel <ditatompel@gmail.com>
  9. # Thanks to : 5ynL0rd who always inspire me.
  10. # Greetings for all members of devilzc0de.org, all Indonesian c0ders,
  11. # and all GNU Generation ;-)
  12. # I glue you all my regards.
  13.  
  14. from optparse import OptionParser
  15. import sys, getpass, urllib, string, os.path
  16.  
  17. # Login as user and get your API http://pastebin.com/api
  18. # But it's ok you use API key below. ;)
  19. APIKEY = "81a5d31a05abe09be32cb6832f6904b9"
  20. URL = "http://pastebin.com"
  21.  
  22. menu = OptionParser()
  23. menu.add_option("-f", dest="file", help="file you want to upload (Required!) ")
  24. menu.add_option("-u", "--user", dest="user", help="your pastebin username, will be submit as guest if not specified", type="string")
  25. menu.add_option("-n", "--name", dest="name", default='untitled', help="your pastebin file title (optional)")
  26. menu.add_option("-p", "--private", action="store_false", default="0", help="set this param for private paste")
  27. menu.add_option("-t", "--type", dest="type", default='text', help="force format syntax highlight (Default: text)")
  28. menu.add_option("-e", dest="expire_date", default='N', help="Paste expires. Default: Never", metavar="<N|10M|1H|1D|1M>")
  29.  
  30. ExpOpt = { "N": "never", "10M": "10 minutes", "1H": "1 hour", "1D": "1 day", "1M": "1 month" }
  31.  
  32. FileType = {
  33.     "4cs": "4CS", "6502acme": "6502 ACME Cross Assembler", "6502kickass": "6502 Kick Assembler",
  34.     "6502tasm": "6502 TASM/64TASS", "abap": "ABAP", "actionscript": "ActionScript",
  35.     "actionscript3": "ActionScript 3", "ada": "Ada", "algol68": "ALGOL 68", "apache": "Apache Log",
  36.     "applescript": "AppleScript", "apt_sources": "APT Sources", "asm": "ASM (NASM)", "asp" : "ASP",
  37.     "autoconf": "autoconf", "autohotkey": "Autohotkey", "autoit": "AutoIt", "avisynth": "Avisynth",
  38.     "awk": "Awk", "bascomavr": "BASCOM AVR", "bash": "Bash", "basic4gl": "Basic4GL",
  39.     "bibtex": "BibTeX", "blitzbasic": "Blitz Basic", "bnf": "BNF", "boo": "BOO", "bf": "BrainFuck",
  40.     "c": "C", "c_mac": "C for Macs", "cil": "C Intermediate Language", "csharp": "C#",
  41.     "cpp": "C++", "cpp-qt": "C++ (with QT extensions)", "c_loadrunner": "C: Loadrunner",
  42.     "caddcl": "CAD DCL", "cadlisp": "CAD Lisp", "cfdg": "CFDG", "chaiscrip": "ChaiScript",
  43.     "clojure": "Clojure", "klonec": "Clone C", "klonecpp": "Clone C++", "cmake": "CMake",
  44.     "cobol": "COBOL", "coffeescript": "CoffeeScript", "cfm": "ColdFusion", "css": "CSS",
  45.     "cuesheet": "Cuesheet", "d": "D", "dcs": "DCS", "delphi": "Delphi",
  46.     "oxygene": "Delphi Prism (Oxygene)", "diff": "Diff", "div": "DIV", "dos": "DOS",
  47.     "dot": "DOT", "e": "E", "ecmascript": "ECMAScript", "eiffel": "Eiffel", "email": "Email",
  48.     "epc": "EPC", "erlang": "Erlang", "fsharp": "F#", "falcon": "Falcon", "fo": "FO Language",
  49.     "f1": "Formula One", "fortran": "Fortran", "freebasic": "FreeBasic", "freeswitch": "FreeSWITCH",
  50.     "gambas": "GAMBAS", "gml": "Game Maker", "gdb": "GDB", "genero": "Genero", "genie": "Genie",
  51.     "gettext": "GetText", "go": "Go", "groovy": "Groovy", "gwbasic": "GwBasic", "haskell": "Haskell",
  52.     "hicest": "HicEst", "hq9plus": "HQ9 Plus", "html4strict": "HTML", "html5": "HTML 5", "icon": "Icon",
  53.     "idl": "IDL", "ini": "INI file", "inno": "Inno Script", "intercal": "INTERCAL", "io": "IO", "j": "J",
  54.     "java": "Java", "java5": "Java 5", "javascript": "JavaScript", "jquery": "jQuery",
  55.     "kixtart": "KiXtart", "latex": "Latex", "lb": "Liberty BASIC", "lsl2": "Linden Scripting",
  56.     "lisp": "Lisp", "llvm": "LLVM", "locobasic": "Loco Basic", "logtalk": "Logtalk",
  57.     "lolcode": "LOL Code", "lotusformulas": "Lotus Formulas", "lotusscript": "Lotus Script",
  58.     "lscript": "LScript", "lua": "Lua", "m68k": "M68000 Assembler", "magiksf": "MagikSF", "make": "Make",
  59.     "mapbasic": "MapBasic", "matlab": "MatLab", "mirc": "mIRC", "mmix": "MIX Assembler",
  60.     "modula2": "Modula 2", "modula3": "Modula 3", "68000devpac": "Motorola 68000 HiSoft Dev",
  61.     "mpasm": "MPASM", "mxml": "MXML", "mysql": "MySQL", "newlisp": "newLISP", "text": "None",
  62.     "nsis": "NullSoft Installer", "oberon2": "Oberon 2", "objeck": "Objeck Programming Langua",
  63.     "objc": "Objective C", "ocaml-brief": "OCalm Brief", "ocaml": "OCaml", "pf": "OpenBSD PACKET FILTER",
  64.     "glsl": "OpenGL Shading", "oobas": "Openoffice BASIC", "oracle11": "Oracle 11", "oracle8": "Oracle 8",
  65.     "oz": "Oz", "pascal": "Pascal", "pawn": "PAWN", "pcre": "PCRE", "per": "Per", "perl": "Perl",
  66.     "perl6": "Perl 6", "php": "PHP", "php-brief": "PHP Brief", "pic16": "Pic 16", "pike": "Pike",
  67.     "pixelbender": "Pixel Bender", "plsql": "PL/SQL", "postgresql": "PostgreSQL", "povray": "POV-Ray",
  68.     "powershell": "Power Shell", "powerbuilder": "PowerBuilder", "proftpd": "ProFTPd",
  69.     "progress": "Progress", "prolog": "Prolog", "properties": "Properties", "providex": "ProvideX",
  70.     "purebasic": "PureBasic", "pycon": "PyCon", "python": "Python", "q": "q/kdb+", "qbasic": "QBasic",
  71.     "rsplus": "R", "rails": "Rails", "rebol": "REBOL", "reg": "REG", "robots": "Robots",
  72.     "rpmspec": "RPM Spec", "ruby": "Ruby", "gnuplot": "Ruby Gnuplot", "sas": "SAS", "scala": "Scala",
  73.     "scheme": "Scheme", "scilab": "Scilab", "sdlbasic": "SdlBasic", "smalltalk": "Smalltalk",
  74.     "smarty": "Smarty", "sql": "SQL", "systemverilog": "SystemVerilog", "tsql": "T-SQL", "tcl": "TCL",
  75.     "teraterm": "Tera Term", "thinbasic": "thinBasic", "typoscript": "TypoScript", "unicon": "Unicon",
  76.     "uscript": "UnrealScript", "vala": "Vala", "vbnet": "VB.NET", "verilog": "VeriLog", "vhdl": "VHDL",
  77.     "vim": "VIM", "visualprolog": "Visual Pro Log", "vb": "VisualBasic", "visualfoxpro": "VisualFoxPro",
  78.     "whitespace": "WhiteSpace", "whois": "WHOIS", "winbatch": "Winbatch", "xbasic": "XBasic", "xml": "XML",
  79.     "xorg_conf": "Xorg Config", "xpp": "XPP", "yaml": "YAML", "z80": "Z80 Assembler", "zxbasic": "ZXBasic"
  80. }
  81.  
  82. try:
  83.     (options, args) = menu.parse_args()
  84.    
  85.     if options.file:
  86.         f = open(options.file, 'r')
  87.         fileU = f.read()
  88.     else:
  89.         menu.print_help()
  90.         sys.exit(1)
  91.    
  92.     if options.user:
  93.         password = getpass.getpass("pastebin Password:")
  94.         user = options.user
  95.     else:
  96.         user = "guest"
  97.    
  98.     if options.private:
  99.         private = "0"
  100.     else:
  101.         private = "1"
  102.    
  103.     if options.expire_date:
  104.         if options.expire_date not in ExpOpt:
  105.             print "invalid paste expires argument!\nUse following syntax :"
  106.             for Opts in ExpOpt:
  107.                 print Opts + " \t=> " + ExpOpt[Opts]
  108.             sys.exit(1)
  109.  
  110. except IOError, err:
  111.     if "directory" in str(err[1]):
  112.         print "[!]\tCan't open file " + options.file
  113.         sys.exit(1)
  114.     else:
  115.         print "[!]\t" + err[1]
  116.         sys.exit(1)
  117.  
  118. def getCodeType():
  119.     ext = os.path.splitext(options.file)[1]
  120.     syntax = "text"
  121.     AutoExts = {
  122.         ".4cs": "4cs", ".abap": "abap", ".as": "actionscript", ".asm": "asm",
  123.         ".asp": "asp", ".sh": "bash", ".cs": "csharp", ".c": "c", ".cpp": "cpp",
  124.         ".css": "css", ".html": "html4strict", ".h": "c", ".java": "java",
  125.         ".js": "javascript", ".LUA": "lua", ".php": "php", ".pl": "perl",
  126.         ".py": "python", ".sql": "sql"
  127.         }
  128.     for AutoExt in AutoExts:
  129.         if AutoExt == ext:
  130.             syntax = AutoExts[AutoExt]
  131.    
  132.     return syntax
  133.  
  134. def PasteIt(paste_name, paste_format, paste_code, paste_private, paste_expire):
  135.     if user != "guest":
  136.         LoginParam = urllib.urlencode({'api_dev_key': APIKEY,
  137.         'api_user_name': user,
  138.         'api_user_password': password
  139.         })
  140.         PostLogin = urllib.urlopen(URL + '/api/api_login.php',LoginParam)
  141.         try:
  142.             LoginRespond = PostLogin.read()
  143.             if "Bad API" in LoginRespond:
  144.                 print "[!] Error : " + LoginRespond
  145.                 sys.exit(1)
  146.             else:
  147.                 PasteParam = urllib.urlencode({'api_option': 'paste',
  148.                 'api_user_key': LoginRespond,
  149.                 'api_dev_key': APIKEY,
  150.                 'api_paste_name': paste_name,
  151.                 'api_paste_format': paste_format,
  152.                 'api_paste_code': paste_code,
  153.                 'api_paste_private': paste_private,
  154.                 'api_paste_expire_date': paste_expire
  155.                 })
  156.         finally:
  157.             PostLogin.close()
  158.    
  159.     else:
  160.         PasteParam = urllib.urlencode({'api_option': 'paste',
  161.         'api_dev_key': APIKEY,
  162.         'api_paste_name': paste_name,
  163.         'api_paste_format': paste_format,
  164.         'api_paste_code': paste_code,
  165.         'api_paste_private': paste_private,
  166.         'api_paste_expire_date': paste_expire
  167.         })
  168.    
  169.     PostPaste = urllib.urlopen(URL + '/api/api_post.php',PasteParam)
  170.     try :
  171.         response = PostPaste.read()
  172.         print response
  173.     finally:
  174.         PostPaste.close()
  175.  
  176. if __name__ == "__main__":
  177.     if options.type != "text":
  178.         if options.type not in FileType:
  179.             print options.type + " syntax is not available on pastebin, using default filetype syntax.."
  180.             UsedSyntax = getCodeType()
  181.         else:
  182.             UsedSyntax = options.type
  183.     else:
  184.         UsedSyntax = getCodeType()
  185.     print "[+] File : " + options.file
  186.     print "[+] Syntax : " + UsedSyntax
  187.     print "[+] post as : " + user
  188.     print "[+] Expires : " + ExpOpt[options.expire_date]
  189.     PasteIt(options.name, UsedSyntax, fileU, private, options.expire_date)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement