Created
November 30, 2022 17:20
-
-
Save feeeper/c3bea5d75873ac09e07bc82f364e94ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import requests | |
from __future__ import print_function | |
import json | |
import sys | |
import inspect | |
class Wox(object): | |
""" | |
Wox python plugin base | |
""" | |
def __init__(self): | |
rpc_request = json.loads(sys.argv[1]) | |
# proxy is not working now | |
self.proxy = rpc_request.get("proxy",{}) | |
request_method_name = rpc_request.get("method") | |
request_parameters = rpc_request.get("parameters") | |
methods = inspect.getmembers(self, predicate=inspect.ismethod) | |
request_method = dict(methods)[request_method_name] | |
results = request_method(*request_parameters) | |
if request_method_name == "query" or request_method_name == "context_menu": | |
print(json.dumps({"result": results})) | |
def query(self,query): | |
""" | |
sub class need to override this method | |
""" | |
return [] | |
def context_menu(self, data): | |
""" | |
optional context menu entries for a result | |
""" | |
return [] | |
def debug(self,msg): | |
""" | |
alert msg | |
""" | |
print("DEBUG:{}".format(msg)) | |
sys.exit() | |
class WoxAPI(object): | |
@classmethod | |
def change_query(cls,query,requery = False): | |
""" | |
change wox query | |
""" | |
print(json.dumps({"method": "Wox.ChangeQuery","parameters":[query,requery]})) | |
@classmethod | |
def shell_run(cls,cmd): | |
""" | |
run shell commands | |
""" | |
print(json.dumps({"method": "Wox.ShellRun","parameters":[cmd]})) | |
@classmethod | |
def close_app(cls): | |
""" | |
close wox | |
""" | |
print(json.dumps({"method": "Wox.CloseApp","parameters":[]})) | |
@classmethod | |
def hide_app(cls): | |
""" | |
hide wox | |
""" | |
print(json.dumps({"method": "Wox.HideApp","parameters":[]})) | |
@classmethod | |
def show_app(cls): | |
""" | |
show wox | |
""" | |
print(json.dumps({"method": "Wox.ShowApp","parameters":[]})) | |
@classmethod | |
def show_msg(cls,title,sub_title,ico_path=""): | |
""" | |
show messagebox | |
""" | |
print(json.dumps({"method": "Wox.ShowMsg","parameters":[title,sub_title,ico_path]})) | |
@classmethod | |
def open_setting_dialog(cls): | |
""" | |
open setting dialog | |
""" | |
print(json.dumps({"method": "Wox.OpenSettingDialog","parameters":[]})) | |
@classmethod | |
def start_loadingbar(cls): | |
""" | |
start loading animation in wox | |
""" | |
print(json.dumps({"method": "Wox.StartLoadingBar","parameters":[]})) | |
@classmethod | |
def stop_loadingbar(cls): | |
""" | |
stop loading animation in wox | |
""" | |
print(json.dumps({"method": "Wox.StopLoadingBar","parameters":[]})) | |
@classmethod | |
def reload_plugins(cls): | |
""" | |
reload all wox plugins | |
""" | |
print(json.dumps({"method": "Wox.ReloadPlugins","parameters":[]})) | |
class Main(Wox): | |
def query(self, query): | |
amount, from_cur, to_cur = [x.strip() for x in query.split(' ')] | |
return self.main(from_cur, to_cur, amount) | |
def main(self, from_cur, to_cur, amount): | |
with open('./log.log', 'w', encoding='utf8') as f: | |
f.write(f'{from_cur=} {to_cur=} {amount=}\n') | |
try: | |
url = f"https://api.apilayer.com/exchangerates_data/convert?to={to_cur}&from={from_cur}&amount={amount}" | |
payload = {} | |
headers= { | |
# get yours apikey at https://exchangeratesapi.io/ | |
"apikey": "" | |
} | |
response = requests.request("GET", url, headers=headers, data = payload) | |
result = response.json() | |
amount_in_to_currency = result['result'] | |
rate = result['info']['rate'] | |
with open('./log.log', 'a', encoding='utf8') as f: | |
f.write(f'{from_cur=} {to_cur=} {amount=}\n') | |
return [{ | |
'Title': f'{amount_in_to_currency:,.2f}'.replace(',', ' '), | |
'SubTitle': f'Rate: {rate:.2f}', | |
'IcoPath': 'Images/app.png', | |
'ContextData': 'ContextData', # Data that is passed to context_menu function call | |
}] | |
except requests.exceptions.ConnectionError as timeout_err: | |
with open('./log.log', 'a', encoding='utf8') as f: | |
f.write(f'{str(timeout_err)}\n') | |
return [{ | |
'Title': 'Currency server does not response. Try again later, please', | |
'SubTitle': f'Exception', | |
'IcoPath': 'Images/app.png', | |
'ContextData': 'ContextData', # Data that is passed to context_menu function call | |
}] | |
except Exception as e: | |
with open('./log.log', 'a', encoding='utf8') as f: | |
f.write(f'{str(e)}\n') | |
return [{ | |
'Title': str(e), | |
'SubTitle': f'Exception', | |
'IcoPath': 'Images/app.png', | |
'ContextData': 'ContextData', # Data that is passed to context_menu function call | |
}] | |
if __name__ == '__main__': | |
Main() | |
''' | |
plugin.json | |
{ | |
"ID":"D2D2C23B084D411DB66FE0C79D6C2A6H", //Plugin ID,32 bit UUID | |
"ActionKeyword":"cc", //Plugin default action keyword | |
"Name":"currency", //Plugin name | |
"Description":"currency converter", //Plugin description | |
"Author":"feeeper", //Plugin Author | |
"Version":"1.0.0", //Plugin version,must be x.x.x format | |
"Language":"python", //Plugin language,we support csharp,python and executable now | |
"Website":"http://www.getwox.com", //Plugin website or author website | |
"IcoPath": "pic.png", //Plugin icon, relative path to the pluign folder | |
"ExecuteFileName":"currency.py" //Execution entry. Dll name for c# plugin, and python file for python plugin | |
} | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment