Skip to content

Instantly share code, notes, and snippets.

@yonderbread
Last active October 5, 2020 05:09
Show Gist options
  • Save yonderbread/b2718327d10afd0669ce11f3c6e66c9b to your computer and use it in GitHub Desktop.
Save yonderbread/b2718327d10afd0669ce11f3c6e66c9b to your computer and use it in GitHub Desktop.
Simple pastebin api wrapper for Python
import requests
'''Visibility modes for your paste'''
class VisibilityType:
PUBLIC = 0
UNLISTED = 1
PRIVATE = 2
'''Different durations pastebin accepts for paste expiration'''
class ExpireDuration:
NEVER = 'N'
TEN_MINUTES = '10M'
ONE_HOUR = '1H'
ONE_DAY = '1D'
ONE_WEEK = '1W'
TWO_WEEKS = '2W'
ONE_MONTH = '1M'
ONE_YEAR = '1Y'
class Paste:
'''Object that represents a pastes attributes, pass these to Pastebin.create_new_paste'''
def __init__(self, title: str, content: str, visibilty_type: int = 0, paste_expire_date='N',
paste_code_type='text'):
self.title = title
self.content = content
self.paste_private = visibilty_type
self.paste_expire_date = paste_expire_date
self.paste_format = paste_code_type
class Pastebin:
'''Create a new Pastebin API wrapper instance by providing username, password, and your api dev key'''
def __init__(self, username: str, password: str, api_dev_key: str):
self.username = username
self.password = password
self.api_dev_key = api_dev_key
self.login_data = {'api_dev_key': self.api_dev_key,
'api_user_name': self.username,
'api_user_password': self.password}
self.token = None
'''If you want to post pastes as yourself'''
def login(self):
res = requests.post('https://pastebin.com/api/api_login.php', data=self.login_data)
if res.ok:
self.token = res.text
return True
return False
'''Post a new paste'''
def create_new_paste(self, paste: Paste):
payload = {
'api_option': 'paste',
'api_dev_key': self.api_dev_key,
'api_user_key': self.token,
'api_paste_name': paste.title,
'api_paste_expire_date': paste.paste_expire_date,
'api_paste_format': paste.paste_format,
'api_paste_private': paste.paste_private,
'api_paste_code': paste.content
}
res = requests.post('https://pastebin.com/api/api_post.php', data=payload)
if res.ok:
return res.text
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment