Advertisement
Guest User

Untitled

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