pastebin/pastebin.py

61 lines
2.5 KiB
Python
Raw Permalink Normal View History

2019-06-28 05:28:33 +00:00
#!/usr/bin/env python
2023-05-18 20:34:33 +00:00
# PasteBin API Class - Developed by acidvegas in Python (https://git.acid.vegas/pastebin)
2019-06-28 05:28:33 +00:00
'''
2023-08-20 01:42:38 +00:00
API Documentation: https://pastebin.com/doc_api
2019-06-28 05:28:33 +00:00
'''
import urllib.parse
import urllib.request
class PasteBin:
2023-05-18 20:34:33 +00:00
def __init__(self, api_dev_key, api_user_key=None):
2019-06-28 05:28:33 +00:00
self.api_dev_key = api_dev_key
self.api_user_key = api_user_key
def api_call(self, method, params):
2023-08-20 01:42:38 +00:00
'''Make a call to the PasteBin API.'''
2023-05-18 20:34:33 +00:00
response = urllib.request.urlopen('https://pastebin.com/api/' + method, urllib.parse.urlencode(params).encode('utf-8'), timeout=10)
2019-06-28 05:28:33 +00:00
return response.read().decode()
def create_user_key(self, username, password):
2023-08-20 01:42:38 +00:00
'''Create a user key for the PasteBin API.'''
2019-06-28 05:28:33 +00:00
params = {'api_dev_key':self.api_dev_key, 'api_user_name':username, 'api_user_password':password}
2019-06-28 09:55:43 +00:00
return self.api_call('api_login.php', params)
2019-06-28 05:28:33 +00:00
def paste(self, data, guest=False, name=None, format=None, private=None, expire=None):
2023-08-20 01:42:38 +00:00
'''Create a paste on PasteBin.'''
2019-06-28 05:28:33 +00:00
params = {'api_dev_key':self.api_dev_key, 'api_option':'paste', 'api_paste_code':data}
if not guest : params['api_user_key'] = self.api_user_key
if name : params['api_paste_name'] = name
if format : params['api_paste_format'] = format
if private : params['api_paste_private'] = private
if expire : params['api_paste_expire_date'] = expire
return self.api_call('api_post.php', params)
def list_pastes(self, results_limit=None):
2023-08-20 01:42:38 +00:00
'''List pastes created by the user.'''
2019-06-28 05:28:33 +00:00
params = {'api_dev_key':self.api_dev_key, 'api_user_key':self.api_user_key, 'api_option':'list'}
2023-08-20 01:42:38 +00:00
if results_limit:
2019-06-28 05:28:33 +00:00
params['api_results_limit'] = results_limit
return self.api_call('api_post.php', params)
def trending_pastes(self):
2023-08-20 01:42:38 +00:00
'''List trending pastes.'''
2019-06-28 05:28:33 +00:00
params = {'api_dev_key':self.api_dev_key, 'api_option':'trends'}
return self.api_call('api_post.php', params)
def delete_paste(self, paste_key):
2023-08-20 01:42:38 +00:00
'''Delete a paste.'''
2019-06-28 05:28:33 +00:00
params = {'api_dev_key':self.api_dev_key, 'api_user_key':self.api_user_key, 'api_paste_key':paste_key, 'api_option':'delete'}
return self.api_call('api_post.php', params)
def user_info(self):
2023-08-20 01:42:38 +00:00
'''Get information about the user.'''
2019-06-28 05:28:33 +00:00
params = {'api_dev_key':self.api_dev_key, 'api_user_key':self.api_user_key, 'api_option':'userdetails'}
return self.api_call('api_post.php', params)
def raw_pastes(self, paste_key):
2023-08-20 01:42:38 +00:00
'''Get the raw data of a paste.'''
2019-06-28 05:28:33 +00:00
params = {'api_dev_key':self.api_dev_key, 'api_user_key':self.api_user_key, 'api_paste_key':paste_key, 'api_option':'show_paste'}
return self.api_call('api_raw.php', params)