Tools to interface with Teknik Services.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

teknik.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. __module_name__ = "Teknik"
  2. __module_version__ = "1.0.1"
  3. __module_description__ = "Interact with the Teknik Services, including file uploads, pastes, and url shortening."
  4. import_success = True
  5. import tkinter as tk
  6. from tkinter.filedialog import askopenfilename
  7. try:
  8. import hexchat
  9. except ImportError:
  10. print('This script must be run under Hexchat.')
  11. print('Get Hexchat now at: https://hexchat.github.io/')
  12. import_ok = False
  13. # Requires Install
  14. try:
  15. from teknik import uploads as teknik
  16. except ImportError as e:
  17. print('Missing package(s) for %s: %s' % (__module_name__, e))
  18. import_ok = False
  19. def teknik_prompt():
  20. # Prompt for a file
  21. root = tk.Tk()
  22. root.withdraw()
  23. file_path = askopenfilename()
  24. if file_path is not None:
  25. # Upload the selected file
  26. upload_file(file_path)
  27. def upload_file(file):
  28. if file != '':
  29. # Get current config values
  30. apiUrl = hexchat.get_pluginpref('teknik_url')
  31. apiUsername = hexchat.get_pluginpref('teknik_username')
  32. apiToken = hexchat.get_pluginpref('teknik_auth_token')
  33. # Try to upload the file
  34. results = teknik.UploadFile(apiUrl, file, apiUsername, apiToken)
  35. # Either print the result to the input box, or write the error message to the window
  36. if 'error' in results:
  37. hexchat.prnt('Error: ' + results['error']['message'])
  38. elif 'result' in results:
  39. hexchat.command("settext " + results['result']['url'])
  40. else:
  41. hexchat.prnt('Unknown Error')
  42. else:
  43. hexchat.prnt('Error: Invalid File')
  44. def teknik_set_url(url):
  45. hexchat.set_pluginpref('teknik_url', url)
  46. def teknik_set_token(token):
  47. hexchat.set_pluginpref('teknik_auth_token', token)
  48. def teknik_set_username(username):
  49. hexchat.set_pluginpref('teknik_username', username)
  50. def teknik_command(word, word_eol, userdata):
  51. if len(word) < 2:
  52. hexchat.prnt("Error: You must specify a command")
  53. else:
  54. command = word[1].lower()
  55. if command == 'upload':
  56. if len(word) < 3:
  57. teknik_prompt()
  58. else:
  59. upload_file(word_eol[2])
  60. elif command == 'set':
  61. if len(word) < 3:
  62. hexchat.prnt("Error: You must specify a config option")
  63. else:
  64. option = word[2].lower()
  65. # Set value based on option
  66. if option == 'username':
  67. if len(word) < 4:
  68. hexchat.prnt("Error: You must specify a username")
  69. else:
  70. teknik_set_username(word_eol[3])
  71. elif option == 'token':
  72. if len(word) < 4:
  73. hexchat.prnt("Error: You must specify an auth token")
  74. else:
  75. teknik_set_token(word_eol[3])
  76. elif option == 'url':
  77. if len(word) < 4:
  78. hexchat.prnt("Error: You must specify an api url")
  79. else:
  80. teknik_set_url(word_eol[3])
  81. else:
  82. hexchat.prnt("Error: Unrecognized option")
  83. else:
  84. hexchat.prnt("Error: Unrecognized command")
  85. return hexchat.EAT_ALL
  86. if __name__ == "__main__" and import_success:
  87. hexchat.hook_command("TEKNIK", teknik_command, help="""Upload files, paste text, shorten URLs, or change script options.
  88. Usage: TEKNIK upload [<file>]
  89. TEKNIK set username <username>
  90. TEKNIK set token <auth_token>
  91. TEKNIK set url <api_url>""")