Script: teknik.py

Interact with the Teknik services, including file uploads, pastes, and URL shortening. (for WeeChat ≥ 1.5, requires: teknik)
Author: uncled1023 — Version: 1.0.0 — License: BSD.
Added: 2017-10-14.
Download

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Teknik created by Uncled1023 <admin@teknik.io>
from __future__ import print_function

import_success = True

import sys
import os
import threading
import json
import Tkinter as tk
import tkFileDialog

try:
    import weechat
except ImportError:
    print('This script must be run under WeeChat.')
    print('Get WeeChat now at: http://www.weechat.org/')
    import_success = False

# Requires Install
try:
    from teknik import uploads as teknik
except ImportError as e:
    print('Missing package(s) for %s: %s' % ('Teknik', e))
    import_success = False

# Weechat Registration
weechat.register("Teknik", "Uncled1023", "1.0.0", "BSD", "Interact with the Teknik Services, including file uploads, pastes, and url shortening.", "script_closed", "")

def upload_file(data):
  try:
    args = json.loads(data)
    if args['file'] is not None and os.path.exists(args['file']):
      # Try to upload the file
      jObj = teknik.UploadFile(args['apiUrl'], args['file'], args['apiUsername'], args['apiToken'])
      return json.dumps(jObj)
  except:
    e = sys.exc_info()[0]
    print("Exception: %s" %e, file=sys.stderr)
  return ''

def process_upload(data, command, return_code, out, err):
  if return_code == weechat.WEECHAT_HOOK_PROCESS_ERROR:
      weechat.prnt("", "Error with command '%s'" % command)
      return weechat.WEECHAT_RC_OK
  if return_code > 0:
      weechat.prnt("", "return_code = %d" % return_code)
  if out != "":
    results = json.loads(out)
    # Either print the result to the input box, or write the error message to the window
    if 'error' in results:
      weechat.prnt("", 'Error: ' + results['error']['message'])
    elif 'result' in results:      
      buffer = weechat.current_buffer()
      weechat.buffer_set(buffer, 'input', results['result']['url'])
    else:
      weechat.prnt("", 'Unknown Error')
  if err != "":
      weechat.prnt("", "stderr: %s" % err)
  return weechat.WEECHAT_RC_OK

def teknik_set_url(url):
  weechat.config_set_plugin('plugins.var.python.teknik.api_url', url)
  
def teknik_set_token(token):
  weechat.config_set_plugin('plugins.var.python.teknik.token', token)

def teknik_set_username(username):
  weechat.config_set_plugin('plugins.var.python.teknik.username', username)
  
def script_closed():
  # Clean Up Session
  return weechat.WEECHAT_RC_OK
      
def teknik_command(data, buffer, args):
  args = args.strip()
  if args == "":
    weechat.prnt("", "Error: You must specify a command")
  else:
    argv = args.split(" ")
    command = argv[0].lower()
    
    # Upload a File
    if command == 'upload':
      if len(argv) < 2:
        weechat.prnt("", "Error: You must specify a file")
      else:
        # Get current config values
        apiUrl = weechat.config_string(weechat.config_get('plugins.var.python.teknik.api_url'))
        apiUsername = weechat.config_string(weechat.config_get('plugins.var.python.teknik.username'))
        apiToken = weechat.config_string(weechat.config_get('plugins.var.python.teknik.token'))
        
        data = {'file': argv[1], 'apiUrl': apiUrl, 'apiUsername': apiUsername, 'apiToken': apiToken}
        hook = weechat.hook_process('func:upload_file', 0, "process_upload", json.dumps(data))
        
    # Set a config option
    elif command == 'set':
      if len(argv) < 2:
        weechat.prnt("", "Error: You must specify the option to set")
      else:
        option = argv[1].lower()
        if option == 'username':
          if len(argv) < 3:
            weechat.prnt("", "Error: You must specify a username")
          else:
            teknik_set_username(argv[2])
        elif option == 'token':
          if len(argv) < 3:
            weechat.prnt("", "Error: You must specify an auth token")
          else:
            teknik_set_token(argv[2])
        elif option == 'url':
          if len(argv) < 3:
            weechat.prnt("", "Error: You must specify an api url")
          else:
            teknik_set_url(argv[2])
        else:
          weechat.prnt("", "Error: Unrecognized Option")
    else:
      weechat.prnt("", "Error: Unrecognized Command")
  
  return weechat.WEECHAT_RC_OK

if __name__ == "__main__" and import_success:
  hook = weechat.hook_command("teknik", "Allows uploading of a file to Teknik and sharing the url directly to the chat.",
        "[upload <file>] | [set username|token|url <username|auth_token|api_url>]",
        '          file: The file you want to upload'
        '      username: The username for your Teknik account'
        '    auth_token: The authentication token for your Teknik Account'
        '       api_url: The URL for the Upload API',
        "",
        "teknik_command", "")