Tools to interface with Teknik Services.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

teknik_upload.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import_success = True
  2. import tkinter as tk
  3. from tkinter.filedialog import askopenfilename
  4. try:
  5. import weechat
  6. except ImportError:
  7. print('This script must be run under WeeChat.')
  8. print('Get WeeChat now at: http://www.weechat.org/')
  9. import_ok = False
  10. # Requires Install
  11. try:
  12. from teknik import uploads as teknik
  13. except ImportError as e:
  14. print('Missing package(s) for %s: %s' % ('Teknik Upload', e))
  15. import_ok = False
  16. # Weechat Registration
  17. weechat.register("Teknik Upload", "Uncled1023", "1.0", "BSDv3", "Uploads files to the Teknik Services", "", "")
  18. def teknik_prompt():
  19. # Get current config values
  20. apiUrl = weechat.config_string(weechat.config_get('plugins.var.python.teknik.api_url'))
  21. apiUsername = weechat.config_string(weechat.config_get('plugins.var.python.teknik.username'))
  22. apiToken = weechat.config_string(weechat.config_get('plugins.var.python.teknik.token'))
  23. # Prompt for a file
  24. root = tk.Tk()
  25. root.withdraw()
  26. file_path = askopenfilename()
  27. if file_path != '':
  28. # Try to upload the file
  29. results = teknik.UploadFile(apiUrl, file_path, apiUsername, apiToken)
  30. # Either print the result to the input box, or write the error message to the window
  31. if 'error' in results:
  32. print('Error: ' + results['error']['message'])
  33. elif 'result' in results:
  34. buffer = weechat.current_buffer()
  35. weechat.buffer_set(buffer, 'input', results['result']['url'])
  36. else:
  37. print('Unknown Error')
  38. def teknik_set_url(url):
  39. weechat.config_set_plugin('plugins.var.python.teknik.api_url', url)
  40. def teknik_set_token(token):
  41. weechat.config_set_plugin('plugins.var.python.teknik.token', token)
  42. def teknik_set_username(username):
  43. weechat.config_set_plugin('plugins.var.python.teknik.username', username)
  44. def teknik_command(data, buffer, args):
  45. args = args.strip()
  46. if args == "":
  47. teknik_prompt()
  48. else:
  49. argv = args.split(" ")
  50. command = argv[0].lower()
  51. if command == 'username':
  52. if len(argv) < 2:
  53. print("Error: You must specify a username")
  54. else:
  55. teknik_set_username(argv[1])
  56. elif command == 'token':
  57. if len(argv) < 2:
  58. print("Error: You must specify an auth token")
  59. else:
  60. teknik_set_token(argv[1])
  61. elif command == 'url':
  62. if len(argv) < 2:
  63. print("Error: You must specify an api url")
  64. else:
  65. teknik_set_url(argv[1])
  66. else:
  67. print("Error: Unrecognized Command")
  68. return weechat.WEECHAT_RC_OK
  69. if __name__ == "__main__" and import_success:
  70. hook = weechat.hook_command("teknik", "Allows uploading of a file to Teknik and sharing the url directly to the chat.",
  71. "[username|token|url <username|auth_token|api_url>]",
  72. ' username: The username for your Teknik account'
  73. ' auth_token: The authentication token for your Teknik Account'
  74. ' api_url: The URL for the Upload API',
  75. "",
  76. "teknik_command", "")