Skip to content

Instantly share code, notes, and snippets.

@clckwrkbdgr
Created March 6, 2016 16:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clckwrkbdgr/4865cc5da17f67ae929d to your computer and use it in GitHub Desktop.
Save clckwrkbdgr/4865cc5da17f67ae929d to your computer and use it in GitHub Desktop.
Using Pastebin as a free online data storage
import requests
import xml.etree.ElementTree as ET
import sys
class Pastebin:
def __init__(self, api_key, user_key):
self.api_key, self.user_key = api_key, user_key
self.session = requests.session()
def _post(self, option, data = None):
data = data.copy() if data else {}
data['api_dev_key'] = self.api_key
data['api_user_key'] = self.user_key
data['api_option'] = option
method = 'raw' if option == 'show_paste' else 'post'
url = 'http://pastebin.com/api/api_{0}.php'.format(method)
response = self.session.request('POST', url, data=data)
return response
def _get_paste_key(self, name):
response = self._post('list')
if response.status_code != 200:
return None
root = ET.fromstring('<root>' + response.content.decode('utf-8') + '</root>')
for paste in root:
if paste.find('paste_title').text == name:
return paste.find('paste_key').text
return None
def get_paste(self, name):
paste_key = self._get_paste_key(name)
if paste_key is None:
return None
response = self._post('show_paste', {'api_paste_key':paste_key})
if response.status_code != 200:
return None
return response.content
def _delete_paste(self, name):
paste_key = self._get_paste_key(name)
if paste_key is not None:
self._post('delete', {'api_paste_key':paste_key})
def _put_paste(self, name, content, expire):
response = self._post('paste', {
'api_paste_name':name,
'api_paste_code':content,
'api_paste_private':2,
'api_paste_expire_date':expire
})
if response.status_code != 200:
return None
supposed_paste_key = response.content.rsplit(b'/', 1)[-1]
real_paste_key = self._get_paste_key(name)
if supposed_paste_key != real_paste_key:
return None
return real_paste_key
def _backup(self, name):
paste_key = self._get_paste_key(name)
if paste_key is None:
return
self._delete_paste(name + '_backup')
content = self.get_paste(name)
paste_key = self._put_paste(name + '_backup', content, 'N')
return paste_key is not None
def edit_paste(self, name, content, expire=None):
expire = expire if expire else 'N'
if self._backup(name):
self._delete_paste(name)
paste_key = self._put_paste(name, content, expire)
return paste_key
if __name__ == "__main__":
if len(sys.argv) > 1:
API_KEY = '$API_KEY' # Available after registration on Pastebin API page.
USER_KEY = '$USER_KEY' # See Pastebin API documentation on how to create User Key.
pastebin = Pastebin(API_KEY, USER_KEY)
if sys.argv[1] == "get":
print(pastebin.get_paste('paste name'))
elif sys.argv[1] == "put":
print(pastebin.edit_paste('paste name', 'data to be stored', '1D'))
else:
print("unknown method")
@Kreijstal
Copy link

how do I use dis

@averageperon4
Copy link

did u figure it out?

@clckwrkbdgr
Copy link
Author

did u figure it out?

Sorry, I don't use pastebin for these purposes for a long time now, don't even remember what was the issue.
I think pastebin did not allow frequent updates, something like 1 hour between updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment