1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 __all__ = ['delete_paste', 'user_details', 'trending', 'pastes_by_user', 'generate_user_key',
30 'legacy_paste', 'paste', 'Pastebin', 'PastebinError']
31
32 import sys
33 import urllib
34
36 """Pastebin API error.
37
38 The error message returned by the web application is stored as the Python exception message."""
39
41 """Pastebin API interaction object.
42
43 Public functions:
44
45 paste -- Pastes a user-specified file or string using the new API-key POST method.
46
47 legacy_paste -- Pastes a user-specified file or string using the old anonymous POST method.
48
49 generate_user_key -- Generates a session-key that is required for other functions.
50
51 pastes_by_user -- Returns all public pastes submitted by the specified login credentials.
52
53 trending -- Returns the top trending paste.
54
55 user_details -- Returns details about the user for the specified API user key.
56
57 delete_paste -- Adds two numbers together and returns the result."""
58
59
60 _bad_request = 'Bad API request'
61
62
63 _base_domain = 'pastebin.com'
64
65
66 _prefix_url = 'http://%s/' % _base_domain
67
68
69 _subdomain_url = 'http://%%s.%s/' % _base_domain
70
71
72 _legacy_api_url= 'http://%s/api_public.php' % _base_domain
73
74
75 _api_url= 'http://%s/api/api_post.php' % _base_domain
76
77
78 _api_login_url= 'http://%s/api/api_login.php' % _base_domain
79
80
81 paste_expire_date = ('N', '10M', '1H', '1D', '1M')
82
83
84 paste_private = ('public', 'unlisted', 'private')
85
86
87 paste_format = (
88 '4cs',
89 '6502acme',
90 '6502kickass',
91 '6502tasm',
92 'abap',
93 'actionscript',
94 'actionscript3',
95 'ada',
96 'algol68',
97 'apache',
98 'applescript',
99 'apt_sources',
100 'asm',
101 'asp',
102 'autoconf',
103 'autohotkey',
104 'autoit',
105 'avisynth',
106 'awk',
107 'bascomavr',
108 'bash',
109 'basic4gl',
110 'bibtex',
111 'blitzbasic',
112 'bnf',
113 'boo',
114 'bf',
115 'c',
116 'c_mac',
117 'cil',
118 'csharp',
119 'cpp',
120 'cpp-qt',
121 'c_loadrunner',
122 'caddcl',
123 'cadlisp',
124 'cfdg',
125 'chaiscript',
126 'clojure',
127 'klonec',
128 'klonecpp',
129 'cmake',
130 'cobol',
131 'coffeescript',
132 'cfm',
133 'css',
134 'cuesheet',
135 'd',
136 'dcs',
137 'delphi',
138 'oxygene',
139 'diff',
140 'div',
141 'dos',
142 'dot',
143 'e',
144 'ecmascript',
145 'eiffel',
146 'email',
147 'epc',
148 'erlang',
149 'fsharp',
150 'falcon',
151 'fo',
152 'f1',
153 'fortran',
154 'freebasic',
155 'freeswitch',
156 'gambas',
157 'gml',
158 'gdb',
159 'genero',
160 'genie',
161 'gettext',
162 'go',
163 'groovy',
164 'gwbasic',
165 'haskell',
166 'hicest',
167 'hq9plus',
168 'html4strict',
169 'html5',
170 'icon',
171 'idl',
172 'ini',
173 'inno',
174 'intercal',
175 'io',
176 'j',
177 'java',
178 'java5',
179 'javascript',
180 'jquery',
181 'kixtart',
182 'latex',
183 'lb',
184 'lsl2',
185 'lisp',
186 'llvm',
187 'locobasic',
188 'logtalk',
189 'lolcode',
190 'lotusformulas',
191 'lotusscript',
192 'lscript',
193 'lua',
194 'm68k',
195 'magiksf',
196 'make',
197 'mapbasic',
198 'matlab',
199 'mirc',
200 'mmix',
201 'modula2',
202 'modula3',
203 '68000devpac',
204 'mpasm',
205 'mxml',
206 'mysql',
207 'newlisp',
208 'text',
209 'nsis',
210 'oberon2',
211 'objeck',
212 'objc',
213 'ocaml-brief',
214 'ocaml',
215 'pf',
216 'glsl',
217 'oobas',
218 'oracle11',
219 'oracle8',
220 'oz',
221 'pascal',
222 'pawn',
223 'pcre',
224 'per',
225 'perl',
226 'perl6',
227 'php',
228 'php-brief',
229 'pic16',
230 'pike',
231 'pixelbender',
232 'plsql',
233 'postgresql',
234 'povray',
235 'powershell',
236 'powerbuilder',
237 'proftpd',
238 'progress',
239 'prolog',
240 'properties',
241 'providex',
242 'purebasic',
243 'pycon',
244 'python',
245 'q',
246 'qbasic',
247 'rsplus',
248 'rails',
249 'rebol',
250 'reg',
251 'robots',
252 'rpmspec',
253 'ruby',
254 'gnuplot',
255 'sas',
256 'scala',
257 'scheme',
258 'scilab',
259 'sdlbasic',
260 'smalltalk',
261 'smarty',
262 'sql',
263 'systemverilog',
264 'tsql',
265 'tcl',
266 'teraterm',
267 'thinbasic',
268 'typoscript',
269 'unicon',
270 'uscript',
271 'vala',
272 'vbnet',
273 'verilog',
274 'vhdl',
275 'vim',
276 'visualprolog',
277 'vb',
278 'visualfoxpro',
279 'whitespace',
280 'whois',
281 'winbatch',
282 'xbasic',
283 'xml',
284 'xorg_conf',
285 'xpp',
286 'yaml',
287 'z80',
288 'zxbasic',
289 )
290
293
294
295 - def delete_paste(self, api_dev_key, api_user_key, api_paste_key):
296 """Delete the paste specified by the api_paste_key.
297
298
299 Usage Example::
300 >>> from pastebin import PastebinAPI
301 >>> x = PastebinAPI()
302 >>> paste_to_delete = x.delete_paste('453a994e0e2f1efae07f8759e59e075b',
303 ... 'c57a18e6c0ae228cd4bd16fe36da381a',
304 ... 'WkgcTFtv')
305 >>> print paste_to_delete
306 Paste Removed
307
308
309 @type api_dev_key: string
310 @param api_dev_key: The API Developer Key of a registered U{http://pastebin.com} account.
311
312 @type api_user_key: string
313 @param api_user_key: The API User Key of a U{http://pastebin.com} registered user.
314
315 @type api_paste_key: string
316 @param api_paste_key: The Paste Key of the paste to be deleted (string after final / in U{http://pastebin.com} URL).
317
318 @rtype: String
319 @returns: A successful deletion returns 'Paste Removed'.
320 """
321
322
323 argv = {'api_dev_key' : str(api_dev_key) }
324
325
326 if api_user_key is not None:
327 argv['api_user_key'] = str(api_user_key)
328
329
330 if api_paste_key is not None:
331 argv['api_paste_key'] = str(api_paste_key)
332
333
334 argv['api_option'] = str('delete')
335
336
337
338 request = urllib.urlopen(self._api_url, urllib.urlencode(argv))
339 response = self._submit_paste(request)
340
341 return response
342
343
345 """Return user details of the user specified by the api_user_key.
346
347
348 Usage Example::
349 >>> from pastebin import PastebinAPI
350 >>> x = PastebinAPI()
351 >>> details = x.user_details('453a994e0e2f1efae07f8759e59e075b',
352 ... 'c57a18e6c0ae228cd4bd16fe36da381a')
353 >>> print details
354 <user>
355 <user_name>MonkeyPuzzle</user_name>
356 <user_format_short>python</user_format_short>
357 <user_expiration>N</user_expiration>
358 <user_avatar_url>http://pastebin.com/i/guest.gif</user_avatar_url>
359 <user_private>0</user_private>
360 <user_website></user_website>
361 <user_email>user@email.com</user_email>
362 <user_location></user_location>
363 <user_account_type>0</user_account_type>
364 </user>
365
366
367 @type api_dev_key: string
368 @param api_dev_key: The API Developer Key of a registered U{http://pastebin.com} account.
369
370 @type api_user_key: string
371 @param api_user_key: The API User Key of a U{http://pastebin.com} registered user.
372
373 @rtype: string
374 @returns: Returns an XML string containing user information.
375 """
376
377
378 argv = {'api_dev_key' : str(api_dev_key) }
379
380
381 if api_user_key is not None:
382 argv['api_user_key'] = str(api_user_key)
383
384
385 argv['api_option'] = str('userdetails')
386
387
388 request_string = urllib.urlopen(self._api_url, urllib.urlencode(argv))
389 response = request_string.read()
390
391
392 if response.startswith(self._bad_request):
393 raise PastebinError(response)
394
395 elif not response.startswith('<user>'):
396 raise PastebinError(response)
397
398 return response
399
400
402 """Returns the top trending paste details.
403
404
405 Usage Example::
406 >>> from pastebin import PastebinAPI
407 >>> x = PastebinAPI()
408 >>> details = x.trending('453a994e0e2f1efae07f8759e59e075b')
409 >>> print details
410 <paste>
411 <paste_key>jjMRFDH6</paste_key>
412 <paste_date>1333230838</paste_date>
413 <paste_title></paste_title>
414 <paste_size>6416</paste_size>
415 <paste_expire_date>0</paste_expire_date>
416 <paste_private>0</paste_private>
417 <paste_format_long>None</paste_format_long>
418 <paste_format_short>text</paste_format_short>
419 <paste_url>http://pastebin.com/jjMRFDH6</paste_url>
420 <paste_hits>6384</paste_hits>
421 </paste>
422
423 Note: Returns multiple trending pastes, not just 1.
424
425
426 @type api_dev_key: string
427 @param api_dev_key: The API Developer Key of a registered U{http://pastebin.com} account.
428
429
430 @rtype: string
431 @return: Returns the string (XML formatted) containing the top trending pastes.
432 """
433
434
435 argv = {'api_dev_key' : str(api_dev_key) }
436
437
438 argv['api_option'] = str('trends')
439
440
441 request_string = urllib.urlopen(self._api_url, urllib.urlencode(argv))
442 response = request_string.read()
443
444
445 if response.startswith(self._bad_request):
446 raise PastebinError(response)
447
448 elif not response.startswith('<paste>'):
449 raise PastebinError(response)
450
451 return response
452
453
454 - def pastes_by_user(self, api_dev_key, api_user_key, results_limit = None):
455 """Returns all pastes for the provided api_user_key.
456
457
458 Usage Example::
459 >>> from pastebin import PastebinAPI
460 >>> x = PastebinAPI()
461 >>> details = x.user_details('453a994e0e2f1efae07f8759e59e075b',
462 ... 'c57a18e6c0ae228cd4bd16fe36da381a',
463 ... 100)
464 >>> print details
465 <paste>
466 <paste_key>DLiSspYT</paste_key>
467 <paste_date>1332714730</paste_date>
468 <paste_title>Pastebin.py - Python 3.2 Pastebin.com API</paste_title>
469 <paste_size>25300</paste_size>
470 <paste_expire_date>0</paste_expire_date>
471 <paste_private>0</paste_private>
472 <paste_format_long>Python</paste_format_long>
473 <paste_format_short>python</paste_format_short>
474 <paste_url>http://pastebin.com/DLiSspYT</paste_url>
475 <paste_hits>70</paste_hits>
476 </paste>
477
478 Note: Returns multiple pastes, not just 1.
479
480
481 @type api_dev_key: string
482 @param api_dev_key: The API Developer Key of a registered U{http://pastebin.com} account.
483
484 @type api_user_key: string
485 @param api_user_key: The API User Key of a U{http://pastebin.com} registered user.
486
487 @type results_limit: number
488 @param results_limit: The number of pastes to return between 1 - 1000.
489
490 @rtype: string
491 @returns: Returns an XML string containing number of specified pastes by user.
492 """
493
494
495 argv = {'api_dev_key' : str(api_dev_key) }
496
497
498 if api_user_key is not None:
499 argv['api_user_key'] = str(api_user_key)
500
501
502 if results_limit is None:
503 argv['api_results_limit'] = 50
504
505 if results_limit is not None:
506 if results_limit < 1:
507 argv['api_results_limit'] = 50
508 elif results_limit > 1000:
509 argv['api_results_limit'] = 1000
510 else:
511 argv['api_results_limit'] = int(results_limit)
512
513
514 argv['api_option'] = str('list')
515
516
517 request_string = urllib.urlopen(self._api_url, urllib.urlencode(argv))
518 response = request_string.read()
519
520
521 if response.startswith(self._bad_request):
522 raise PastebinError(response)
523
524 elif not response.startswith('<paste>'):
525 raise PastebinError(response)
526
527 return response
528
529
531 """Generate a user session key - needed for other functions.
532
533
534 Usage Example::
535 >>> from pastebin import PastebinAPI
536 >>> x = PastebinAPI()
537 >>> my_key = x.generate_user_key('453a994e0e2f1efae07f8759e59e075b',
538 ... 'MonkeyPuzzle',
539 ... '12345678')
540 >>> print my_key
541 c57a18e6c0ae228cd4bd16fe36da381a
542
543
544 @type api_dev_key: string
545 @param api_dev_key: The API Developer Key of a registered U{http://pastebin.com} account.
546
547 @type username: string
548 @param username: The username of a registered U{http://pastebin.com} account.
549
550 @type password: string
551 @param password: The password of a registered U{http://pastebin.com} account.
552
553 @rtype: string
554 @returns: Session key (api_user_key) to allow authenticated interaction to the API.
555
556 """
557
558 argv = {'api_dev_key' : str(api_dev_key) }
559
560
561 if username is not None:
562 argv['api_user_name'] = str(username)
563
564
565 if password is not None:
566 argv['api_user_password'] = str(password)
567
568
569 request_string = urllib.urlopen(self._api_url, urllib.urlencode(argv))
570 response = request_string.read()
571
572
573 if response.startswith(self._bad_request):
574 raise PastebinError(response)
575
576 return response
577
578
579 - def paste(self, api_dev_key, api_paste_code,
580 api_user_key = None, paste_name = None, paste_format = None,
581 paste_private = None, paste_expire_date = None):
582
583 """Submit a code snippet to Pastebin using the new API.
584
585
586 Usage Example::
587 >>> from pastebin import PastebinAPI
588 >>> x = PastebinAPI()
589 >>> url = x.paste('453a994e0e2f1efae07f8759e59e075b' ,
590 ... 'Snippet of code to paste goes here',
591 ... paste_name = 'title of paste',
592 ... api_user_key = 'c57a18e6c0ae228cd4bd16fe36da381a',
593 ... paste_format = 'python',
594 ... paste_private = 'unlisted',
595 ... paste_expire_date = '10M')
596 >>> print url
597 http://pastebin.com/tawPUgqY
598
599
600 @type api_dev_key: string
601 @param api_dev_key: The API Developer Key of a registered U{http://pastebin.com} account.
602
603 @type api_paste_code: string
604 @param api_paste_code: The file or string to paste to body of the U{http://pastebin.com} paste.
605
606 @type api_user_key: string
607 @param api_user_key: The API User Key of a U{http://pastebin.com} registered user.
608 If none specified, paste is made as a guest.
609
610 @type paste_name: string
611 @param paste_name: (Optional) Title of the paste.
612 Default is to paste anonymously.
613
614 @type paste_format: string
615 @param paste_format: (Optional) Programming language of the code being
616 pasted. This enables syntax highlighting when reading the code in
617 U{http://pastebin.com}. Default is no syntax highlighting (text is
618 just text and not source code).
619
620 @type paste_private: string
621 @param paste_private: (Optional) C{'public'} if the paste is public (visible
622 by everyone), C{'unlisted'} if it's public but not searchable.
623 C{'private'} if the paste is private and not searchable or indexed.
624 The Pastebin FAQ (U{http://pastebin.com/faq}) claims
625 private pastes are not indexed by search engines (aka Google).
626
627 @type paste_expire_date: str
628 @param paste_expire_date: (Optional) Expiration date for the paste.
629 Once past this date the paste is deleted automatically. Valid
630 values are found in the L{PastebinAPI.paste_expire_date} class member.
631 If not provided, the paste never expires.
632
633 @rtype: string
634 @return: Returns the URL to the newly created paste.
635 """
636
637
638
639 argv = {'api_dev_key' : str(api_dev_key) }
640
641
642 if paste_code is not None:
643 argv['api_paste_code'] = str(api_paste_code)
644
645
646 argv['api_option'] = str('paste')
647
648
649 if api_user_key is not None:
650 argv['api_user_key'] = str(api_user_key)
651 elif api_user_key is None:
652 argv['api_user_key'] = str('')
653
654
655 if paste_name is not None:
656 argv['api_paste_name'] = str(paste_name)
657
658
659 if paste_format is not None:
660 paste_format = str(paste_format).strip().lower()
661 argv['api_paste_format'] = paste_format
662
663
664 if paste_private is not None:
665 if paste_private == 'public':
666 argv['api_paste_private'] = int(0)
667 elif paste_private == 'unlisted':
668 argv['api_paste_private'] = int(1)
669 elif paste_private == 'private':
670 argv['api_paste_private'] = int(2)
671
672
673 if paste_expire_date is not None:
674 paste_expire_date = str(paste_expire_date).strip().upper()
675 argv['api_paste_expire_date'] = paste_expire_date
676
677
678 request_string = urllib.urlopen(self._api_url, urllib.urlencode(argv))
679 response = request_string.read()
680
681
682 if response.startswith(self._bad_request):
683 raise PastebinError(response)
684 elif not response.startswith(self._prefix_url):
685 raise PastebinError(response)
686
687 return response
688
689
690 - def legacy_paste(self, paste_code,
691 paste_name = None, paste_private = None,
692 paste_expire_date = None, paste_format = None):
693 """Unofficial python interface to the Pastebin legacy API.
694
695 Unlike the official API, this one doesn't require an API key, so it's
696 virtually anonymous.
697
698
699 Usage Example::
700 >>> from pastebin import PastebinAPI
701 >>> x = PastebinAPI()
702 >>> url = x.legacy_paste('Snippet of code to paste goes here',
703 ... paste_name = 'title of paste',
704 ... paste_private = 'unlisted',
705 ... paste_expire_date = '10M',
706 ... paste_format = 'python')
707 >>> print url
708 http://pastebin.com/tawPUgqY
709
710
711 @type paste_code: string
712 @param paste_code: The file or string to paste to body of the U{http://pastebin.com} paste.
713
714 @type paste_name: string
715 @param paste_name: (Optional) Title of the paste.
716 Default is to paste with no title.
717
718 @type paste_private: string
719 @param paste_private: (Optional) C{'public'} if the paste is public (visible
720 by everyone), C{'unlisted'} if it's public but not searchable.
721 C{'private'} if the paste is private and not searchable or indexed.
722 The Pastebin FAQ (U{http://pastebin.com/faq}) claims
723 private pastes are not indexed by search engines (aka Google).
724
725 @type paste_expire_date: string
726 @param paste_expire_date: (Optional) Expiration date for the paste.
727 Once past this date the paste is deleted automatically. Valid
728 values are found in the L{PastebinAPI.paste_expire_date} class member.
729 If not provided, the paste never expires.
730
731 @type paste_format: string
732 @param paste_format: (Optional) Programming language of the code being
733 pasted. This enables syntax highlighting when reading the code in
734 U{http://pastebin.com}. Default is no syntax highlighting (text is
735 just text and not source code).
736
737 @rtype: string
738 @return: Returns the URL to the newly created paste.
739 """
740
741
742 argv = { 'paste_code' : str(paste_code) }
743
744
745 if paste_name is not None:
746 argv['paste_name'] = str(paste_name)
747
748
749 if paste_private is not None:
750 argv['paste_private'] = int(bool(int(paste_private)))
751
752
753 if paste_expire_date is not None:
754 paste_expire_date = str(paste_expire_date).strip().upper()
755 argv['paste_expire_date'] = paste_expire_date
756
757
758 if paste_format is not None:
759 paste_format = str(paste_format).strip().lower()
760 argv['paste_format'] = paste_format
761
762
763 request_string = urllib.urlopen(self._api_url, urllib.urlencode(argv))
764 response = request_string.read()
765
766
767 if response.startswith(self._bad_request):
768 raise PastebinError(response)
769 elif not response.startswith(self._prefix_url):
770 raise PastebinError(response)
771
772 return response
773
774
775
776
777
778
779
780
781 delete_paste = PastebinAPI.delete_paste
782 user_details = PastebinAPI.user_details
783 trending = PastebinAPI.trending
784 pastes_by_user = PastebinAPI.pastes_by_user
785 generate_user_key = PastebinAPI.generate_user_key
786 legacy_paste = PastebinAPI.legacy_paste
787 paste = PastebinAPI.paste
788
789
790
791 if __name__ == "__main__":
792 main()
793