Advertisement
kakaroto_BR

pastebin script

Mar 22nd, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1. from optparse import OptionParser
  2. import os
  3. import urllib
  4. import urllib2
  5.  
  6. API_KEY_FILE = os.path.join(os.path.expanduser('~'), 'pastebin_api_key')
  7.  
  8. def get_api_key():
  9.     """
  10.    Return the api_key stored or nil if not found
  11.    """
  12.     api_key = None
  13.     if os.path.exists(API_KEY_FILE):
  14.         f = open(API_KEY_FILE, 'r')
  15.         api_key = f.read()
  16.         f.close()
  17.     return api_key
  18.    
  19. def store_api_key(api_key):
  20.     """
  21.    Store api key in user home directory
  22.    """
  23.     f = open(API_KEY_FILE, 'w')
  24.     f.write(api_key)
  25.     f.close()
  26.  
  27. def check_api_key():
  28.     api_key = get_api_key()
  29.     if not api_key:
  30.         api_key = raw_input('Enter pastebin api key and press RETURN: ')
  31.         store_api_key(api_key)
  32.     return api_key
  33.  
  34. def parse_arguments():
  35.     """Parse command line arguments
  36.    """
  37.     parser = OptionParser()
  38.     parser.add_option('-k', '--api_dev_key', dest='api_dev_key',
  39.                       help='API Dev Key')
  40.     parser.add_option('-f', '--file', dest='filename', help='File to paste')
  41.     parser.add_option('-t', '--title', dest='title', help='Pastebin title')
  42.     parser.add_option('-l', '--format', dest='format', help='Syntax highlight format')
  43.     parser.add_option('-d', '--expire-date', dest='expire_date', help="""
  44.    N = Never
  45.    10M = 10 Minutes
  46.    1H = 1 Hour
  47.    1D = 1 Day
  48.    1M = 1 Month """)
  49.     parser.add_option('-g', '--guess', action='store_true', dest='guess', help='Paste as guess', default=False)
  50.     options, args = parser.parse_args()
  51.     return options
  52.  
  53. class Pastebin(object):
  54.     def __init__(self,
  55.                  api_key,
  56.                  api_option='paste',
  57.                  paste_code='',
  58.                  title='',
  59.                  paste_format='text',
  60.                  private='0',
  61.                  expire_date='',
  62.                  user_key=''):
  63.         self.api_dev_key = api_key
  64.         self.api_option = api_option
  65.         self.api_paste_code = paste_code
  66.         self.api_paste_name = title
  67.         self.api_paste_format = paste_format
  68.         self.api_paste_private = private
  69.         self.api_paste_expire_date = expire_date
  70.         self.api_user_key = user_key
  71.  
  72.     def paste(self):
  73.         """
  74.        Create a new paste in pastebin.com, returns a url pointing to it
  75.        """
  76.         fields =  dict((k,v) for k,v in p.__dict__.iteritems() if len(v) > 0)
  77.         data = urllib.urlencode(fields)
  78.         request = urllib2.Request('http://pastebin.com/api/api_post.php', data)
  79.         f = urllib2.urlopen(request)
  80.         result = f.read()
  81.         f.close()
  82.         return result
  83.  
  84. def create_pastebin_from_arguments():
  85.     """Create a Pastebin object from command line arguments
  86.    """
  87.     options = parse_arguments()
  88.     pastebin = Pastebin(check_api_key())
  89.     if options.api_dev_key:
  90.         pastebin.api_dev_key = options.api_dev_key
  91.     if options.filename:
  92.         f = open(options.filename, 'r')
  93.         pastebin.api_paste_code = f.read()
  94.         f.close()
  95.     if options.title:
  96.         pastebin.api_paste_name = options.title
  97.     if options.format:
  98.         pastebin.api_paste_format = options.format
  99.     if options.expire_date:
  100.         pastebin.api_paste_expire_date = options.expire_date
  101.     if not options.guess:
  102.         name = raw_input('User Name: ')
  103.         passwd = raw_input('User Password : ')
  104.         pastebin.api_user_key = get_api_user_key(pastebin.api_dev_key, name, passwd)
  105.     return pastebin
  106.  
  107. def get_api_user_key(api_dev_key, name, passwd):
  108.     data = urllib.urlencode({'api_dev_key' : api_dev_key, 'api_user_name' : name, 'api_user_password' : passwd})
  109.     request = urllib2.Request('http://pastebin.com/api/api_login.php', data)
  110.     f = urllib2.urlopen(request)
  111.     result = f.read()
  112.     f.close()
  113.     return result
  114.  
  115. if __name__ == '__main__':
  116.     p = create_pastebin_from_arguments()
  117.     print p.paste()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement