Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from optparse import OptionParser
- import os
- import urllib
- import urllib2
- API_KEY_FILE = os.path.join(os.path.expanduser('~'), 'pastebin_api_key')
- def get_api_key():
- """
- Return the api_key stored or nil if not found
- """
- api_key = None
- if os.path.exists(API_KEY_FILE):
- f = open(API_KEY_FILE, 'r')
- api_key = f.read()
- f.close()
- return api_key
- def store_api_key(api_key):
- """
- Store api key in user home directory
- """
- f = open(API_KEY_FILE, 'w')
- f.write(api_key)
- f.close()
- def check_api_key():
- api_key = get_api_key()
- if not api_key:
- api_key = raw_input('Enter pastebin api key and press RETURN: ')
- store_api_key(api_key)
- return api_key
- def parse_arguments():
- """Parse command line arguments
- """
- parser = OptionParser()
- parser.add_option('-k', '--api_dev_key', dest='api_dev_key',
- help='API Dev Key')
- parser.add_option('-f', '--file', dest='filename', help='File to paste')
- parser.add_option('-t', '--title', dest='title', help='Pastebin title')
- parser.add_option('-l', '--format', dest='format', help='Syntax highlight format')
- parser.add_option('-d', '--expire-date', dest='expire_date', help="""
- N = Never
- 10M = 10 Minutes
- 1H = 1 Hour
- 1D = 1 Day
- 1M = 1 Month """)
- parser.add_option('-g', '--guess', action='store_true', dest='guess', help='Paste as guess', default=False)
- options, args = parser.parse_args()
- return options
- class Pastebin(object):
- def __init__(self,
- api_key,
- api_option='paste',
- paste_code='',
- title='',
- paste_format='text',
- private='0',
- expire_date='',
- user_key=''):
- self.api_dev_key = api_key
- self.api_option = api_option
- self.api_paste_code = paste_code
- self.api_paste_name = title
- self.api_paste_format = paste_format
- self.api_paste_private = private
- self.api_paste_expire_date = expire_date
- self.api_user_key = user_key
- def paste(self):
- """
- Create a new paste in pastebin.com, returns a url pointing to it
- """
- fields = dict((k,v) for k,v in p.__dict__.iteritems() if len(v) > 0)
- data = urllib.urlencode(fields)
- request = urllib2.Request('http://pastebin.com/api/api_post.php', data)
- f = urllib2.urlopen(request)
- result = f.read()
- f.close()
- return result
- def create_pastebin_from_arguments():
- """Create a Pastebin object from command line arguments
- """
- options = parse_arguments()
- pastebin = Pastebin(check_api_key())
- if options.api_dev_key:
- pastebin.api_dev_key = options.api_dev_key
- if options.filename:
- f = open(options.filename, 'r')
- pastebin.api_paste_code = f.read()
- f.close()
- if options.title:
- pastebin.api_paste_name = options.title
- if options.format:
- pastebin.api_paste_format = options.format
- if options.expire_date:
- pastebin.api_paste_expire_date = options.expire_date
- if not options.guess:
- name = raw_input('User Name: ')
- passwd = raw_input('User Password : ')
- pastebin.api_user_key = get_api_user_key(pastebin.api_dev_key, name, passwd)
- return pastebin
- def get_api_user_key(api_dev_key, name, passwd):
- data = urllib.urlencode({'api_dev_key' : api_dev_key, 'api_user_name' : name, 'api_user_password' : passwd})
- request = urllib2.Request('http://pastebin.com/api/api_login.php', data)
- f = urllib2.urlopen(request)
- result = f.read()
- f.close()
- return result
- if __name__ == '__main__':
- p = create_pastebin_from_arguments()
- print p.paste()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement