Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #/usr/bin/env python
- import urllib, urllib2, re
- from xml.dom import minidom
- DEV_KEY = 'DEVELOPER-KEY' # http://pastebin.com/api#1
- FILE_FORMATS = {
- '4cs' : '4CS',
- '6502acme' : '6502 ACME Cross Assembler',
- '6502kickass' : '6502 Kick Assembler',
- '6502tasm' : '6502 TASM/64TASS',
- 'abap' : 'ABAP',
- 'actionscript' : 'ActionScript',
- 'actionscript3' : 'ActionScript 3',
- 'ada' : 'Ada',
- 'algol68' : 'ALGOL 68',
- 'apache' : 'Apache Log',
- 'applescript' : 'AppleScript',
- 'apt_sources' : 'APT Sources',
- 'asm' : 'ASM (NASM)',
- 'asp' : 'ASP',
- 'autoconf' : 'autoconf',
- 'autohotkey' : 'Autohotkey',
- 'autoit' : 'AutoIt',
- 'avisynth' : 'Avisynth',
- 'awk' : 'Awk',
- 'bascomavr' : 'BASCOM' 'AVR',
- 'bash' : 'Bash',
- 'basic4gl' : 'Basic4GL',
- 'bibtex' : 'BibTeX',
- 'blitzbasic' : 'Blitz Basic',
- 'bnf' : 'BNF',
- 'boo' : 'BOO',
- 'bf' : 'BrainFuck',
- 'c' : 'C',
- 'c_mac' : 'C for Macs',
- 'cil' : 'C Intermediate Language',
- 'csharp' : 'C#',
- 'cpp' : 'C++',
- 'cpp-qt' : 'C++ (with QT extensions)',
- 'c_loadrunner' : 'C: Loadrunner',
- 'caddcl' : 'CAD DCL',
- 'cadlisp' : 'CAD Lisp',
- 'cfdg' : 'CFDG',
- 'chaiscript' : 'ChaiScript',
- 'clojure' : 'Clojure',
- 'klonec' : 'Clone C',
- 'klonecpp' : 'Clone C++',
- 'cmake' : 'CMake',
- 'cobol' : 'COBOL',
- 'coffeescript' : 'CoffeeScript',
- 'cfm' : 'ColdFusion',
- 'css' : 'CSS',
- 'cuesheet' : 'Cuesheet',
- 'd' : 'D',
- 'dcs' : 'DCS',
- 'delphi' : 'Delphi',
- 'oxygene' : 'Delphi Prism (Oxygene)',
- 'diff' : 'Diff',
- 'div' : 'DIV',
- 'dos' : 'DOS',
- 'dot' : 'DOT',
- 'e' : 'E',
- 'ecmascript' : 'ECMAScript',
- 'eiffel' : 'Eiffel',
- 'email' : 'Email',
- 'epc' : 'EPC',
- 'erlang' : 'Erlang',
- 'fsharp' : 'F#',
- 'falcon' : 'Falcon',
- 'fo' : 'FO' 'Language',
- 'f1' : 'Formula One',
- 'fortran' : 'Fortran',
- 'freebasic' : 'FreeBasic',
- 'gambas' : 'GAMBAS',
- 'gml' : 'Game Maker',
- 'gdb' : 'GDB',
- 'genero' : 'Genero',
- 'genie' : 'Genie',
- 'gettext' : 'GetText',
- 'go' : 'Go',
- 'groovy' : 'Groovy',
- 'gwbasic' : 'GwBasic',
- 'haskell' : 'Haskell',
- 'hicest' : 'HicEst',
- 'hq9plus' : 'HQ9 Plus',
- 'html4strict' : 'HTML',
- 'html5' : 'HTML 5',
- 'icon' : 'Icon',
- 'idl' : 'IDL',
- 'ini' : 'INI file',
- 'inno' : 'Inno Script',
- 'intercal' : 'INTERCAL',
- 'io' : 'IO',
- 'j' : 'J',
- 'java' : 'Java',
- 'java5' : 'Java 5',
- 'javascript' : 'JavaScript',
- 'jquery' : 'jQuery',
- 'kixtart' : 'KiXtart',
- 'latex' : 'Latex',
- 'lb' : 'Liberty BASIC',
- 'lsl2' : 'Linden Scripting',
- 'lisp' : 'Lisp',
- 'llvm' : 'LLVM',
- 'locobasic' : 'Loco Basic',
- 'logtalk' : 'Logtalk',
- 'lolcode' : 'LOL Code',
- 'lotusformulas' : 'Lotus Formulas',
- 'lotusscript' : 'Lotus Script',
- 'lscript' : 'LScript',
- 'lua' : 'Lua',
- 'm68k' : 'M68000 Assembler',
- 'magiksf' : 'MagikSF',
- 'make' : 'Make',
- 'mapbasic' : 'MapBasic',
- 'matlab' : 'MatLab',
- 'mirc' : 'mIRC',
- 'mmix' : 'MIX Assembler',
- 'modula2' : 'Modula 2',
- 'modula3' : 'Modula 3',
- '68000devpac' : 'Motorola 68000 HiSoft Dev',
- 'mpasm' : 'MPASM',
- 'mxml' : 'MXML',
- 'mysql' : 'MySQL',
- 'newlisp' : 'newLISP',
- 'text' : 'None',
- 'nsis' : 'NullSoft Installer',
- 'oberon2' : 'Oberon 2',
- 'objeck' : 'Objeck Programming Langua',
- 'objc' : 'Objective C',
- 'ocaml-brief' : 'OCalm Brief',
- 'ocaml' : 'OCaml',
- 'pf' : 'OpenBSD PACKET FILTER',
- 'glsl' : 'OpenGL Shading',
- 'oobas' : 'Openoffice BASIC',
- 'oracle11' : 'Oracle 11',
- 'oracle8' : 'Oracle 8',
- 'oz' : 'Oz',
- 'pascal' : 'Pascal',
- 'pawn' : 'PAWN',
- 'pcre' : 'PCRE',
- 'per' : 'Per',
- 'perl' : 'Perl',
- 'perl6' : 'Perl 6',
- 'php' : 'PHP',
- 'php-brief' : 'PHP Brief',
- 'pic16' : 'Pic 16',
- 'pike' : 'Pike',
- 'pixelbender' : 'Pixel Bender',
- 'plsql' : 'PL/SQL',
- 'postgresql' : 'PostgreSQL',
- 'povray' : 'POV-Ray',
- 'powershell' : 'Power Shell',
- 'powerbuilder' : 'PowerBuilder',
- 'proftpd' : 'ProFTPd',
- 'progress' : 'Progress',
- 'prolog' : 'Prolog',
- 'properties' : 'Properties',
- 'providex' : 'ProvideX',
- 'purebasic' : 'PureBasic',
- 'pycon' : 'PyCon',
- 'python' : 'Python',
- 'q' : 'q/kdb+',
- 'qbasic' : 'QBasic',
- 'rsplus' : 'R',
- 'rails' : 'Rails',
- 'rebol' : 'REBOL',
- 'reg' : 'REG',
- 'robots' : 'Robots',
- 'rpmspec' : 'RPM Spec',
- 'ruby' : 'Ruby',
- 'gnuplot' : 'Ruby Gnuplot',
- 'sas' : 'SAS',
- 'scala' : 'Scala',
- 'scheme' : 'Scheme',
- 'scilab' : 'Scilab',
- 'sdlbasic' : 'SdlBasic',
- 'smalltalk' : 'Smalltalk',
- 'smarty' : 'Smarty',
- 'sql' : 'SQL',
- 'systemverilog' : 'SystemVerilog',
- 'tsql' : 'T-SQL',
- 'tcl' : 'TCL',
- 'teraterm' : 'Tera Term',
- 'thinbasic' : 'thinBasic',
- 'typoscript' : 'TypoScript',
- 'unicon' : 'Unicon',
- 'uscript' : 'UnrealScript',
- 'vala' : 'Vala',
- 'vbnet' : 'VB.NET',
- 'verilog' : 'VeriLog',
- 'vhdl' : 'VHDL',
- 'vim' : 'VIM',
- 'visualprolog' : 'Visual Pro Log',
- 'vb' : 'VisualBasic',
- 'visualfoxpro' : 'VisualFoxPro',
- 'whitespace' : 'WhiteSpace',
- 'whois' : 'WHOIS',
- 'winbatch' : 'Winbatch',
- 'xbasic' : 'XBasic',
- 'xml' : 'XML',
- 'xorg_conf' : 'Xorg Config',
- 'xpp' : 'XPP',
- 'yaml' : 'YAML',
- 'z80' : 'Z80 Assembler',
- 'zxbasic' : 'ZXBasic'
- }
- EXPIRATION_FORMAT = {
- 'N' : 'Never',
- '10M' : '10 Minutes',
- '1H' : '1 Hour',
- '1D' : '1 Day',
- '1M' : '1 Month'
- }
- KEY_URL = 'http://pastebin.com/api/api_login.php'
- POST_URL = 'http://pastebin.com/api/api_post.php'
- BAD_REQUEST = 'Bad API request'
- class BadRequestError(Exception):
- pass
- class InvalidArgumentError(Exception):
- pass
- class PasteBinComConnection(object):
- def __init__(self, auth=None):
- if auth:
- self.name, self.password = auth
- def open(self):
- if self.name and self.password:
- data = urllib.urlencode([
- ('api_dev_key', DEV_KEY),
- ('api_user_name', self.name),
- ('api_user_password', self.password)])
- response = urllib2.urlopen(KEY_URL, data).readline()
- if response.find(BAD_REQUEST) == -1:
- self.session = response
- else:
- self.__raiseError(response)
- else:
- self.session = ''
- def createPaste(self, code, name='', format='', isPrivate=0, expireDate='N'):
- self.__checkSession()
- if format and not format in FILE_FORMATS.keys():
- raise InvalidArgumentError('This format is not supported')
- if expireDate and not expireDate in EXPIRATION_FORMAT.keys():
- raise InvalidArgumentError('This expiration type is not supported')
- data = [
- ('api_option', 'paste'),
- ('api_user_key', self.session),
- ('api_paste_private', isPrivate),
- ('api_paste_expire_date', expireDate),
- ('api_dev_key', DEV_KEY),
- ('api_paste_code', code)]
- if name: data.append(('api_paste_name', name))
- if format: data.append(('api_paste_format', format))
- data = urllib.urlencode(data)
- response = urllib2.urlopen(POST_URL, data).readline()
- if response.find(BAD_REQUEST) == -1:
- return response
- else:
- self.__raiseError(response)
- def delete(self, paste):
- self.__checkSession()
- if not paste or not re.match(r'^\w{8}$', paste):
- raise InvalidArgumentError('Invalid paste key')
- data = urllib.urlencode([
- ('api_option', 'delete'),
- ('api_user_key', self.session),
- ('api_dev_key', DEV_KEY),
- ('api_paste_key', paste)])
- response = urllib2.urlopen(POST_URL, data).readline()
- if response.find(BAD_REQUEST) == -1:
- return response
- else:
- self.__raiseError(response)
- def list(self, limit=1000):
- self.__checkSession()
- data = urllib.urlencode([
- ('api_option', 'list'),
- ('api_user_key', self.session),
- ('api_dev_key', DEV_KEY),
- ('api_results_limit', limit)])
- response = urllib2.urlopen(POST_URL, data).readlines()
- if response[0].find(BAD_REQUEST) == -1:
- response = '<pastes>{0}</pastes>'.format(''.join(response))
- xml = minidom.parseString(response)
- result = []
- for infoNode in [node for node in xml.childNodes[0].childNodes if node.nodeType == node.ELEMENT_NODE]:
- info = {}
- for dataNode in [node for node in infoNode.childNodes if node.nodeType == node.ELEMENT_NODE]:
- if dataNode.hasChildNodes() and dataNode.childNodes[0].nodeType == dataNode.TEXT_NODE:
- info[dataNode.tagName] = dataNode.childNodes[0].data
- result.append(info)
- return result
- else:
- self.__raiseError(response)
- def getUserInfo(self):
- self.__checkSession()
- data = urllib.urlencode([
- ('api_option', 'userdetails'),
- ('api_user_key', self.session),
- ('api_dev_key', DEV_KEY)])
- response = urllib2.urlopen(POST_URL, data).readlines()
- if response[0].find(BAD_REQUEST) == -1:
- info = {}
- xml = minidom.parseString(''.join(response))
- for node in [node for node in xml.childNodes[0].childNodes if node.nodeType == node.ELEMENT_NODE]:
- if node.hasChildNodes():
- info[node.tagName] = node.firstChild.data
- return info
- else:
- self.__raiseError(response)
- def __raiseError(self, error):
- match = re.match(r'^Bad API request,\s*(?P<error>.+)$', error)
- if match:
- raise BadRequestError(match)
- def __checkSession(self):
- if not hasattr(self, 'session'):
- raise RuntimeError('Session must be started before calling this operation')
Advertisement
Add Comment
Please, Sign In to add comment