Skip to content

Commit

Permalink
python version and change API to jisuapi
Browse files Browse the repository at this point in the history
  • Loading branch information
祝文博 committed May 4, 2023
1 parent bc6a418 commit 5b67fb2
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 103 deletions.
32 changes: 8 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,13 @@ Alfred Workflow Currency Exchange. Convert any currency to CNY.

## API

The free API is from juhe.cn
The free API is from [jisuapi.com](https://www.jisuapi.com/)

Please apply from here:
[https://www.juhe.cn/docs/api/id/80](https://www.juhe.cn/docs/api/id/80)
[https://www.jisuapi.com/api/exchange/](https://www.jisuapi.com/api/exchange/)

Then fill in the appkey in ```CurrencyExchange.php```.

```php
<?php

class CurrencyExchange
{
// Please fill in appkey here.
private $_appKey = "appkey";
private $_api = "http://op.juhe.cn/onebox/exchange/currency";
...
```
Then fill in the appkey in Alfred Workflows Environment Variables.
![app_key](app_key.jpg)

## Usage
```
Expand All @@ -41,16 +31,10 @@ Press enter than the result will copy to you clipboard.

Here is the code in workflow editor.

```php
require_once('CurrencyExchange.php');

$currencyExchange = new CurrencyExchange();

$currencyExchange->caculate("USD", "CNY", {query});
```shell
python3 Calculate.py THB CNY $1
```

You can convert any currency to another by modify the code of last line, like JPY to USD.
You can convert any currency to another by modify the Script text field, like THB to CNY.

```
$currencyExchange->caculate("JPY", "USD", {query});
```
![customize](customize.jpg)
19 changes: 19 additions & 0 deletions Source/Calculate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/local/bin/python3
# -*- coding: UTF-8 -*-

import sys
import os
from CurrencyExchange import CurrencyExchange

# please config app_key in Workflow Environment Variables
app_key = os.getenv('app_key')
# from currency
original_currency = sys.argv[1]
# to currency
destination_currency = sys.argv[2]
# alfred input amount
input = sys.argv[3].replace(",", "")
query = float(input)

currency_exchange = CurrencyExchange(app_key)
currency_exchange.calculate(original_currency, destination_currency, query)
79 changes: 0 additions & 79 deletions Source/CurrencyExchange.php

This file was deleted.

34 changes: 34 additions & 0 deletions Source/CurrencyExchange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import urllib.parse
from urllib import request
import json


class CurrencyExchange:
api = "https://api.jisuapi.com/exchange/convert"

def __init__(self, app_key):
self.app_key = app_key

def calculate(self, original_currency, destination_currency, input_amount):
param = {'from': original_currency, 'to': destination_currency, 'appkey': self.app_key, 'amount': input_amount}
param_string = urllib.parse.urlencode(param)
url = self.api + "?" + param_string
with request.urlopen(url) as f:
data = f.read()
if f.status == 200:
content = data.decode('utf-8')
result = json.loads(content)
if result['status'] == 0:
exchange = result['result']['rate']
output_amount = result['result']['camount']
subtitle = str(input_amount) + " " + original_currency + " to " + destination_currency + " with exchange rate " + exchange + " = " + str(
output_amount)
output_dict = {'items': [{'title': output_amount, 'subtitle': subtitle, 'arg': output_amount}]}
print(json.dumps(output_dict))
else:
print(str(result['status']) + ':' + result['msg'])
else:
print('request fail')
Binary file added app_key.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added customize.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5b67fb2

Please sign in to comment.