Advertisement
Guest User

PythonBin

a guest
Oct 29th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. import fileinput
  2. import pyperclip
  3. import sys, getopt, os
  4. from pastebin_connection import paste_create, login_user
  5.  
  6.  
  7. def add_paste(content, title, syntax, expiry, succ, error):
  8.     if content:
  9.         result = paste_create(content, title, syntax, expiry)
  10.         if result['success']:
  11.             succ.append(result['data'])
  12.         else:
  13.             error.append(result['data'])
  14.         paste_content = ""
  15.  
  16. def handle_input(args, title, syntax, expiry, clip):
  17.     paste_content = ""
  18.     succ = []
  19.     error = []
  20.  
  21.     for line in fileinput.input(args):
  22.         if fileinput.isfirstline():
  23.             add_paste(paste_content, title, syntax, expiry, succ, error)
  24.  
  25.         paste_content += line
  26.  
  27.     add_paste(paste_content, title, syntax, expiry, succ, error)
  28.     if succ:
  29.         print("Succesful paste")
  30.         for url in succ:
  31.             print(url)
  32.  
  33.         print()
  34.  
  35.         if clip and len(succ) == 1:
  36.             pyperclip.copy(succ[0])
  37.  
  38.     if error:
  39.         print("Error")
  40.         for url in error:
  41.             print(url)
  42.  
  43. def print_help():
  44.     print()
  45.     print("Use like cat program to upload to pastebin.")
  46.     print("[-u|--username] [-p|--password] - for username and password")
  47.     print("[-t|--title] for title")
  48.     print("[-s|--syntax] for syntax")
  49.     print("[-e|--expiry] for expiry time")
  50.     print("[-c] for auto copy to clipboard when only one input is used")
  51.     print("[-m] for silent mode(no prints)")
  52.     print()
  53.     pass
  54.  
  55. def main(argv):
  56.     username = ''
  57.     password = ''
  58.     syntax = ''
  59.     title = ''
  60.     expiry = ''
  61.     clip = False
  62.     silent = False
  63.     try:
  64.         opts, args = getopt.gnu_getopt(argv, 'hu:p:t:s:e:cm', ["username=", "password=", "title=", "syntax=", "expiry="])
  65.     except getopt.GetoptError:
  66.         print_help()
  67.         sys.exit(2)
  68.  
  69.     for opt, arg in opts:
  70.         if opt == '-h':
  71.             print_help()
  72.             sys.exit()
  73.         elif opt in ('-u', '--username'):
  74.             username = arg
  75.         elif opt in ('-p', '--password'):
  76.             password = arg
  77.         elif opt in ('-t', '--title'):
  78.             title = arg
  79.         elif opt in ('-s', '--syntax'):
  80.             syntax = arg
  81.         elif opt in ('-e', '--expiry'):
  82.             expiry = arg
  83.         elif opt == '-c':
  84.             clip = True
  85.         elif opt == '-m':
  86.             silent = True
  87.  
  88.     if silent:
  89.         f = open(os.devnull, 'w')
  90.         sys.stdout = f
  91.  
  92.     if username and password:
  93.         res = login_user(username, password)
  94.         if res['success']:
  95.             print('Successful login')
  96.         else:
  97.             print(res['data'])
  98.  
  99.     handle_input(args, title, syntax, expiry, clip)
  100.  
  101. if __name__ == "__main__":
  102.     main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement