#! /usr/bin/env python # -*- coding: utf-8; mode: python -*- """ PasteBox script for http://p.boxnet.eu/api/ Copyright (C) 2011 This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. pastebox.py is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with pastebox.py. If not, see . """ from __future__ import print_function # Python 2/3 compatibility ! import sys import urllib.request import urllib.parse import urllib.error import getopt URL = 'http://p.boxnet.eu/' class PasteBox: def __init__(self): self.replyid = 0 self.mode = 'text' self.ttl = 86400 self.authhash = None def create(self, content): VALUES = {'content': content, 'replyid': self.replyid, 'mode': self.mode, 'ttl': self.ttl} if self.authhash: VALUES['authhash'] = self.authhash data = urllib.parse.urlencode(VALUES) req = urllib.request.Request('%sapi/' % URL, data) response = urllib.request.urlopen(req) page = response.read() return '%s%s' % (URL, page.decode('utf-8').replace('\n', '')) def read(self, pasteid): if not self.authhash: url = '%s%s/download' % (URL, pasteid) else: url = '%s%s/%s/download' % (URL, pasteid, self.authhash) req = urllib.request.Request(url) try: res = urllib.request.urlopen(req) except urllib.error.HTTPError: res = None return res def stdout(self, pasteid): print("reading %s.." % pasteid) response = self.read(pasteid) if response: for i in response.readlines(): print(i.decode('utf-8').replace('\n', '')) else: print("Paste does not exist") def download(self, pasteid): print("downloading %s.." % pasteid) response = self.read(pasteid) if response: x = open('pastebox_%s.txt' % pasteid, 'w') for line in response.readlines(): x.write(line.decode('utf-8')) x.close() print('stored as: pastebox_%s.txt' % pasteid) else: print("Paste does not exist") def usage(): print(""" Usage: pastebox.py