Tools to interface with Teknik Services.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

teknik.py 3.3KB

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