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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. __module_name__ = "Teknik Upload"
  2. __module_version__ = "1.0"
  3. __module_description__ = "Upload files to Teknik"
  4. import hexchat
  5. import base64
  6. import json
  7. import tkinter as tk
  8. from tkinter.filedialog import askopenfilename
  9. # Requires Install
  10. import requests
  11. defaultUrl = 'https://api.teknik.io/v1/Upload'
  12. defaultUsername = ''
  13. defaultToken = ''
  14. def teknik_prompt():
  15. # Get current config values
  16. apiUrl = defaultUrl
  17. apiUsername = defaultUsername
  18. apiToken = defaultToken
  19. cfgUrl = hexchat.get_pluginpref('teknik_url')
  20. if cfgUrl is not None:
  21. apiUrl = cfgUrl
  22. cfgUsername = hexchat.get_pluginpref('teknik_username')
  23. if cfgUsername is not None:
  24. apiUsername = cfgUsername
  25. cfgToken = hexchat.get_pluginpref('teknik_auth_token')
  26. if cfgToken is not None:
  27. apiToken = cfgToken
  28. # Prompt for a file
  29. root = tk.Tk()
  30. root.withdraw()
  31. file_path = askopenfilename()
  32. if file_path != '':
  33. # Create the request
  34. files = {'file': open(file_path, "rb")}
  35. # Create a header if they have added auth info
  36. headers = {}
  37. if apiUsername != '' and apiToken != '':
  38. encAuth = base64.b64encode(apiUsername + ':' + apiToken)
  39. headers = {'Authorization': 'Basic ' + encAuth}
  40. r = requests.post(apiUrl, files=files, headers=headers)
  41. jObj = json.loads(r.text)
  42. # Either print the result to the input box, or write the error message to the window
  43. if 'error' in jObj:
  44. hexchat.prnt('Error: ' + jObj['error']['message'])
  45. elif 'result' in jObj:
  46. hexchat.command("settext " + jObj['result']['url'])
  47. else:
  48. hexchat.prnt('Unknown Error')
  49. def teknik_set_url(url):
  50. hexchat.set_pluginpref('teknik_url', url)
  51. def teknik_set_token(token):
  52. hexchat.set_pluginpref('teknik_auth_token', token)
  53. def teknik_set_username(username):
  54. hexchat.set_pluginpref('teknik_username', username)
  55. def teknik_command(word, word_eol, userdata):
  56. if len(word) < 2:
  57. teknik_prompt()
  58. else:
  59. command = word[1].lower()
  60. if command == 'username':
  61. if len(word) < 3:
  62. hexchat.prnt("Error: You must specify a username")
  63. else:
  64. teknik_set_username(word[2])
  65. elif command == 'token':
  66. if len(word) < 3:
  67. hexchat.prnt("Error: You must specify an auth token")
  68. else:
  69. teknik_set_token(word[2])
  70. elif command == 'url':
  71. if len(word) < 3:
  72. hexchat.prnt("Error: You must specify an api url")
  73. else:
  74. teknik_set_url(word[2])
  75. else:
  76. hexchat.prnt("Error: Unrecognized Command")
  77. return hexchat.EAT_ALL
  78. hexchat.hook_command("TEKNIK", teknik_command, help="""Allows uploading of a file to Teknik and sharing the url directly to the chat.
  79. Usage: TEKNIK
  80. TEKNIK username <username>
  81. TEKNIK token <auth_token>
  82. TEKNIK url <api_url>""")