Advertisement
Guest User

untitled

a guest
Apr 3rd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 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 pastes")
  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 assword")
  47. print()
  48. pass
  49.  
  50. def main(argv):
  51. username = ''
  52. password = ''
  53. syntax = ''
  54. expiry = ''
  55. clip = False
  56. silent = False
  57. try:
  58. opts, args = getopt.gnu_getopt(argv, 'hu:p:t:s:e:cs', ["username=", "password=", "title=", "syntax=", "expiry="])
  59. except getopt.GetoptError:
  60. print_help()
  61. sys.exit(2)
  62.  
  63. for opt, arg in opts:
  64. if opt == '-h':
  65. print_help()
  66. sys.exit()
  67. elif opt in ('-u', '--username'):
  68. username = arg
  69. elif opt in ('-p', '--password'):
  70. password = arg
  71. elif opt in ('-t', '--title'):
  72. title = arg
  73. elif opt in ('-s', '--syntax'):
  74. syntax = arg
  75. elif opt in ('-e', '--expiry'):
  76. expiry = arg
  77. elif opt == '-c':
  78. clip = True
  79. elif opt == '-s':
  80. silent = True
  81.  
  82. if silent:
  83. f = open(os.devnull, 'w')
  84. sys.stdout = f
  85.  
  86. if username and password:
  87. res = login_user(username, password)
  88. if res['success']:
  89. print('Successful login')
  90. else:
  91. print(res['data'])
  92.  
  93. handle_input(args, title, syntax, expiry, clip)
  94.  
  95. if __name__ == "__main__":
  96. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement