Guest User

PythonBin

a guest
Oct 29th, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.23 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:])
  103. import fileinput
  104. import pyperclip
  105. import sys, getopt, os
  106. from pastebin_connection import paste_create, login_user
  107.  
  108.  
  109. def add_paste(content, title, syntax, expiry, succ, error):
  110.     if content:
  111.         result = paste_create(content, title, syntax, expiry)
  112.         if result['success']:
  113.             succ.append(result['data'])
  114.         else:
  115.             error.append(result['data'])
  116.         paste_content = ""
  117.  
  118. def handle_input(args, title, syntax, expiry, clip):
  119.     paste_content = ""
  120.     succ = []
  121.     error = []
  122.  
  123.     for line in fileinput.input(args):
  124.         if fileinput.isfirstline():
  125.             add_paste(paste_content, title, syntax, expiry, succ, error)
  126.  
  127.         paste_content += line
  128.  
  129.     add_paste(paste_content, title, syntax, expiry, succ, error)
  130.     if succ:
  131.         print("Succesful paste")
  132.         for url in succ:
  133.             print(url)
  134.  
  135.         print()
  136.  
  137.         if clip and len(succ) == 1:
  138.             pyperclip.copy(succ[0])
  139.  
  140.     if error:
  141.         print("Error")
  142.         for url in error:
  143.             print(url)
  144.  
  145. def print_help():
  146.     print()
  147.     print("Use like cat program to upload to pastebin.")
  148.     print("[-u|--username] [-p|--password] - for username and password")
  149.     print("[-t|--title] for title")
  150.     print("[-s|--syntax] for syntax")
  151.     print("[-e|--expiry] for expiry time")
  152.     print("[-c] for auto copy to clipboard when only one input is used")
  153.     print("[-m] for silent mode(no prints)")
  154.     print()
  155.     pass
  156.  
  157. def main(argv):
  158.     username = ''
  159.     password = ''
  160.     syntax = ''
  161.     title = ''
  162.     expiry = ''
  163.     clip = False
  164.     silent = False
  165.     try:
  166.         opts, args = getopt.gnu_getopt(argv, 'hu:p:t:s:e:cm', ["username=", "password=", "title=", "syntax=", "expiry="])
  167.     except getopt.GetoptError:
  168.         print_help()
  169.         sys.exit(2)
  170.  
  171.     for opt, arg in opts:
  172.         if opt == '-h':
  173.             print_help()
  174.             sys.exit()
  175.         elif opt in ('-u', '--username'):
  176.             username = arg
  177.         elif opt in ('-p', '--password'):
  178.             password = arg
  179.         elif opt in ('-t', '--title'):
  180.             title = arg
  181.         elif opt in ('-s', '--syntax'):
  182.             syntax = arg
  183.         elif opt in ('-e', '--expiry'):
  184.             expiry = arg
  185.         elif opt == '-c':
  186.             clip = True
  187.         elif opt == '-m':
  188.             silent = True
  189.  
  190.     if silent:
  191.         f = open(os.devnull, 'w')
  192.         sys.stdout = f
  193.  
  194.     if username and password:
  195.         res = login_user(username, password)
  196.         if res['success']:
  197.             print('Successful login')
  198.         else:
  199.             print(res['data'])
  200.  
  201.     handle_input(args, title, syntax, expiry, clip)
  202.  
  203. if __name__ == "__main__":
  204.     main(sys.argv[1:])
Add Comment
Please, Sign In to add comment