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.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Teknik created by Uncled1023 <admin@teknik.io>
  2. from __future__ import print_function
  3. import_success = True
  4. import sys
  5. import os
  6. import threading
  7. import json
  8. try:
  9. import weechat
  10. except ImportError:
  11. print('This script must be run under WeeChat.')
  12. print('Get WeeChat now at: http://www.weechat.org/')
  13. import_success = False
  14. # Requires Install
  15. try:
  16. from teknik import uploads as teknik
  17. except ImportError as e:
  18. print('Missing package(s) for %s: %s' % ('Teknik', e))
  19. import_success = False
  20. # Weechat Registration
  21. weechat.register("Teknik", "Uncled1023", "1.0.0", "BSD", "Interact with the Teknik Services, including file uploads, pastes, and url shortening.", "script_closed", "")
  22. def upload_file(data):
  23. try:
  24. args = json.loads(data)
  25. if args['file'] is not None and os.path.exists(args['file']):
  26. # Try to upload the file
  27. jObj = teknik.UploadFile(args['apiUrl'], args['file'], args['apiUsername'], args['apiToken'])
  28. return json.dumps(jObj)
  29. except:
  30. e = sys.exc_info()[0]
  31. print("Exception: %s" %e, file=sys.stderr)
  32. return ''
  33. def process_upload(data, command, return_code, out, err):
  34. if return_code == weechat.WEECHAT_HOOK_PROCESS_ERROR:
  35. weechat.prnt("", "Error with command '%s'" % command)
  36. return weechat.WEECHAT_RC_OK
  37. if return_code > 0:
  38. weechat.prnt("", "return_code = %d" % return_code)
  39. if out != "":
  40. results = json.loads(out)
  41. # Either print the result to the input box, or write the error message to the window
  42. if 'error' in results:
  43. weechat.prnt("", 'Error: ' + results['error']['message'])
  44. elif 'result' in results:
  45. buffer = weechat.current_buffer()
  46. weechat.buffer_set(buffer, 'input', results['result']['url'])
  47. else:
  48. weechat.prnt("", 'Unknown Error')
  49. if err != "":
  50. weechat.prnt("", "stderr: %s" % err)
  51. return weechat.WEECHAT_RC_OK
  52. def teknik_set_url(url):
  53. weechat.config_set_plugin('plugins.var.python.teknik.api_url', url)
  54. def teknik_set_token(token):
  55. weechat.config_set_plugin('plugins.var.python.teknik.token', token)
  56. def teknik_set_username(username):
  57. weechat.config_set_plugin('plugins.var.python.teknik.username', username)
  58. def script_closed():
  59. # Clean Up Session
  60. return weechat.WEECHAT_RC_OK
  61. def teknik_command(data, buffer, args):
  62. args = args.strip()
  63. if args == "":
  64. weechat.prnt("", "Error: You must specify a command")
  65. else:
  66. argv = args.split(" ")
  67. command = argv[0].lower()
  68. # Upload a File
  69. if command == 'upload':
  70. if len(argv) < 2:
  71. weechat.prnt("", "Error: You must specify a file")
  72. else:
  73. # Get current config values
  74. apiUrl = weechat.config_string(weechat.config_get('plugins.var.python.teknik.api_url'))
  75. apiUsername = weechat.config_string(weechat.config_get('plugins.var.python.teknik.username'))
  76. apiToken = weechat.config_string(weechat.config_get('plugins.var.python.teknik.token'))
  77. data = {'file': argv[1], 'apiUrl': apiUrl, 'apiUsername': apiUsername, 'apiToken': apiToken}
  78. hook = weechat.hook_process('func:upload_file', 0, "process_upload", json.dumps(data))
  79. # Set a config option
  80. elif command == 'set':
  81. if len(argv) < 2:
  82. weechat.prnt("", "Error: You must specify the option to set")
  83. else:
  84. option = argv[1].lower()
  85. if option == 'username':
  86. if len(argv) < 3:
  87. weechat.prnt("", "Error: You must specify a username")
  88. else:
  89. teknik_set_username(argv[2])
  90. elif option == 'token':
  91. if len(argv) < 3:
  92. weechat.prnt("", "Error: You must specify an auth token")
  93. else:
  94. teknik_set_token(argv[2])
  95. elif option == 'url':
  96. if len(argv) < 3:
  97. weechat.prnt("", "Error: You must specify an api url")
  98. else:
  99. teknik_set_url(argv[2])
  100. else:
  101. weechat.prnt("", "Error: Unrecognized Option")
  102. else:
  103. weechat.prnt("", "Error: Unrecognized Command")
  104. return weechat.WEECHAT_RC_OK
  105. if __name__ == "__main__" and import_success:
  106. hook = weechat.hook_command("teknik", "Upload files, paste text, shorten URLs, or change script options.",
  107. "[upload <file>] | [set username|token|url <username|auth_token|api_url>]",
  108. ' file: The file you want to upload'
  109. ' username: The username for your Teknik account'
  110. ' auth_token: The authentication token for your Teknik Account'
  111. ' api_url: The URL for the Upload API',
  112. "",
  113. "teknik_command", "")