Browse Source

Updated hexchat script to have better option handling and allow manual file designation for uploads

Teknikode 2 years ago
parent
commit
ad13710ebd
2 changed files with 60 additions and 38 deletions
  1. 59
    37
      Hexchat/teknik.py
  2. 1
    1
      Weechat/teknik.py

+ 59
- 37
Hexchat/teknik.py View File

@@ -1,6 +1,6 @@
1
-__module_name__ = "Teknik Upload"
1
+__module_name__ = "Teknik"
2 2
 __module_version__ = "1.0.1"
3
-__module_description__ = "Upload files to Teknik"
3
+__module_description__ = "Interact with the Teknik Services, including file uploads, pastes, and url shortening."
4 4
 
5 5
 import_success = True
6 6
 
@@ -18,31 +18,39 @@ except ImportError:
18 18
 try:
19 19
     from teknik import uploads as teknik
20 20
 except ImportError as e:
21
-    print('Missing package(s) for %s: %s' % ('Teknik Upload', e))
21
+    print('Missing package(s) for %s: %s' % (__module_name__, e))
22 22
     import_ok = False
23 23
 
24 24
 def teknik_prompt():
25
+  # Prompt for a file
26
+  root = tk.Tk()
27
+  root.withdraw()
28
+  file_path = askopenfilename()
29
+  
30
+  if file_path is not None:
31
+    # Upload the selected file
32
+    upload_file(file_path)
33
+        
34
+def upload_file(file):
35
+  if file != '':
25 36
     # Get current config values    
26 37
     apiUrl = hexchat.get_pluginpref('teknik_url')
27 38
     apiUsername = hexchat.get_pluginpref('teknik_username')
28
-    apiToken = hexchat.get_pluginpref('teknik_auth_token')
39
+    apiToken = hexchat.get_pluginpref('teknik_auth_token')  
29 40
     
30
-    # Prompt for a file
31
-    root = tk.Tk()
32
-    root.withdraw()
33
-    file_path = askopenfilename()
41
+    # Try to upload the file
42
+    results = teknik.UploadFile(apiUrl, file, apiUsername, apiToken)
34 43
     
35
-    if file_path != '':
36
-      # Try to upload the file
37
-      results = teknik.UploadFile(apiUrl, file_path, apiUsername, apiToken)
38
-      
39
-      # Either print the result to the input box, or write the error message to the window
40
-      if 'error' in results:
41
-        hexchat.prnt('Error: ' + results['error']['message'])
42
-      elif 'result' in results:      
43
-        hexchat.command("settext " + results['result']['url'])
44
-      else:
45
-        hexchat.prnt('Unknown Error')
44
+    # Either print the result to the input box, or write the error message to the window
45
+    if 'error' in results:
46
+      hexchat.prnt('Error: ' + results['error']['message'])
47
+    elif 'result' in results:      
48
+      hexchat.command("settext " + results['result']['url'])
49
+    else:
50
+      hexchat.prnt('Unknown Error')
51
+  else:
52
+    hexchat.prnt('Error: Invalid File')
53
+
46 54
 
47 55
 def teknik_set_url(url):
48 56
   hexchat.set_pluginpref('teknik_url', url)
@@ -55,34 +63,48 @@ def teknik_set_username(username):
55 63
       
56 64
 def teknik_command(word, word_eol, userdata):
57 65
   if len(word) < 2:
58
-    teknik_prompt()
66
+    hexchat.prnt("Error: You must specify a command")
59 67
   else:
60 68
     command = word[1].lower()
61 69
     
62
-    if command == 'username':
63
-      if len(word) < 3:
64
-        hexchat.prnt("Error: You must specify a username")
65
-      else:
66
-        teknik_set_username(word[2])
67
-    elif command == 'token':
70
+    if command == 'upload':
68 71
       if len(word) < 3:
69
-        hexchat.prnt("Error: You must specify an auth token")
72
+        teknik_prompt()
70 73
       else:
71
-        teknik_set_token(word[2])
72
-    elif command == 'url':
74
+        upload_file(word_eol[2])
75
+    elif command == 'set':
73 76
       if len(word) < 3:
74
-        hexchat.prnt("Error: You must specify an api url")
77
+        hexchat.prnt("Error: You must specify a config option")
75 78
       else:
76
-        teknik_set_url(word[2])
79
+        option = word[2].lower()
80
+        
81
+        # Set value based on option
82
+        if option == 'username':
83
+          if len(word) < 4:
84
+            hexchat.prnt("Error: You must specify a username")
85
+          else:
86
+            teknik_set_username(word_eol[3])
87
+        elif option == 'token':
88
+          if len(word) < 4:
89
+            hexchat.prnt("Error: You must specify an auth token")
90
+          else:
91
+            teknik_set_token(word_eol[3])
92
+        elif option == 'url':
93
+          if len(word) < 4:
94
+            hexchat.prnt("Error: You must specify an api url")
95
+          else:
96
+            teknik_set_url(word_eol[3])
97
+        else:
98
+          hexchat.prnt("Error: Unrecognized option")
77 99
     else:
78
-      hexchat.prnt("Error: Unrecognized Command")
100
+      hexchat.prnt("Error: Unrecognized command")
79 101
   
80 102
   return hexchat.EAT_ALL
81 103
 
82 104
 if __name__ == "__main__" and import_success:
83
-  hexchat.hook_command("TEKNIK", teknik_command, help="""Allows uploading of a file to Teknik and sharing the url directly to the chat.
105
+  hexchat.hook_command("TEKNIK", teknik_command, help="""Upload files, paste text, shorten URLs, or change script options.
84 106
 
85
-Usage: TEKNIK
86
-       TEKNIK username <username>
87
-       TEKNIK token <auth_token>
88
-       TEKNIK url <api_url>""")
107
+Usage: TEKNIK upload [<file>]
108
+       TEKNIK set username <username>
109
+       TEKNIK set token <auth_token>
110
+       TEKNIK set url <api_url>""")

+ 1
- 1
Weechat/teknik.py View File

@@ -120,7 +120,7 @@ def teknik_command(data, buffer, args):
120 120
   return weechat.WEECHAT_RC_OK
121 121
 
122 122
 if __name__ == "__main__" and import_success:
123
-  hook = weechat.hook_command("teknik", "Allows uploading of a file to Teknik and sharing the url directly to the chat.",
123
+  hook = weechat.hook_command("teknik", "Upload files, paste text, shorten URLs, or change script options.",
124 124
         "[upload <file>] | [set username|token|url <username|auth_token|api_url>]",
125 125
         '          file: The file you want to upload'
126 126
         '      username: The username for your Teknik account'

Loading…
Cancel
Save