-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwox_bt_dmhy.py
More file actions
105 lines (94 loc) · 3.79 KB
/
wox_bt_dmhy.py
File metadata and controls
105 lines (94 loc) · 3.79 KB
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
#encoding=utf8
#Your class must inherit from Wox base class https://github.com/qianlifeng/Wox/blob/master/PythonHome/wox.py
#The wox class here did some works to simplify the communication between Wox and python plugin.
from wox import Wox,WoxAPI
from dmhy_helper import DMHYHelper
from synology_download_station_api import SynologyDownloadStationAPI
import pyperclip
class Wox_BT_DMHY(Wox):
def __init__(self):
super().__init__()
from os.path import abspath, join, dirname
from json import load
file_path = join(abspath(dirname(__file__)), "config.json")
config = {}
with open(file_path, 'r',encoding='utf-8') as json_file:
config = load(json_file)
if config['use_synology_download_station']:
download_station = SynologyDownloadStationAPI(config['synology_myds_url'])
res = download_station.login(config['synology_account'], config['synology_passwd'])
helper = DMHYHelper(config['use_filter'], config['allow_sort'])
def onclick_event(self, magnet_link):
"""
user click resource
"""
if self.config['use_synology_download_station']:
self.download_magnet(magnet_link)
else:
self.copy_link(magnet_link)
def download_magnet(self, magnet_link):
"""
Creat download magnet task
"""
self.download_station.creat_task(magnet_link)
def copy_link(self, magnet_link):
"""
copy magnet_link into clipboard
"""
pyperclip.copy(magnet_link)
def get_wox_result(self, req_data):
"""
paser req_data to wox_option
"""
results = []
for data in req_data:
results.append({
"Title":data['title'],
"SubTitle":data['time'],
"IcoPath":"magnet.png",
"JsonRPCAction":{"method": "onclick_event", "parameters": [data['magnet']]},
"dontHideAfterAction":True
})
return results
def query(self, query):
"""
search resource
"""
results = []
if self.config['use_synology_download_station'] and self.download_station.has_error():
error_message = self.download_station.get_error_message()
results.append({
"Title":'SynologyDownloadStation Error',
"SubTitle":error_message,
"IcoPath":"magnet.png",
"JsonRPCAction":{"method": "copy_link", "parameters": [error_message]},
"dontHideAfterAction":True
})
return results
query = query.strip()
req_data = []
if query:
if query == 'all':
req_data = self.helper.get_all_resource()
else:
req_data = self.helper.search_resource(query)
return self.get_wox_result(req_data)
else:
if self.config['default_all']:
req_data = self.helper.get_all_resource()
return self.get_wox_result(req_data)
else:
for defult_key in self.config['default_key']:
results.append({
"Title":defult_key['Name'],
"SubTitle": defult_key['Key'],
"IcoPath":"magnet.png",
"JsonRPCAction":{"method": "Wox.ChangeQuery"
, "parameters": ['dmhy {}'.format(defult_key['Key'])
, True]},
"dontHideAfterAction":True
})
return results
#Following statement is necessary
if __name__ == "__main__":
Wox_BT_DMHY()