diff --git a/Currency Converter.alfredworkflow b/Currency Converter.alfredworkflow index f95f0a9..caf0714 100644 Binary files a/Currency Converter.alfredworkflow and b/Currency Converter.alfredworkflow differ diff --git a/README.md b/README.md index 133da32..4b4f33a 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,42 @@ -Currency Converter for Alfred App -========= +# Currency Converter for Alfred App -Currency Converter is a workflow for Alfred App on Mac. +Currency Converter is a workflow for Alfred App on Mac. This is based on the Google currency conversion syntax, such as `30 USD in GBP`. The basic way to use the converter is to call Alfred with: -> 30USD to EUR -The default currency is DKK (can be changed) so the following will convert from USD to DKK: +> 30 USD in EUR + +Note that `in` and `to` are interchangable. The default currency is GBP (can be changed) so the following will convert from USD to GBP: + > 30USD Another shorthand is: + > 30$ to € -And even shorter: +And even shorter: + > 30$ € -Installation -========= -[Download](https://github.com/sqren/alfred-currency/raw/master/Currency%20Converter.alfredworkflow) and open the workflow file +All exchange rates come from [exchangeratesapi.io](https://exchangeratesapi.io/). + +## Usage + +The alfred command is `c`, and hitting return will copy the converted currency value to the clipboard. + +## Installation + +[Download](./Currency%20Converter.alfredworkflow) and open the workflow file. + +## Requirements -Requirements -========= - You must installed: +You must installed: - [Alfred 2.0](http://www.alfredapp.com/) or higher - [Alfred PowerPack](http://www.alfredapp.com/powerpack/) +## Author -Author -======= -Søren Louv-Jansen, [Konscript](http://www.konscript.com) +- Martin Zürn +- [Remy sharp](https://remysharp.com) ([Repo](https://github.com/remy/alfred-currency)) +- Søren Louv-Jansen, [Konscript](http://www.konscript.com) ([original author](https://github.com/sqren/alfred-currency)) diff --git a/src/.DS_Store b/src/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/src/.DS_Store and /dev/null differ diff --git a/src/currency.php b/src/currency.php index 0904d4a..cab64a5 100644 --- a/src/currency.php +++ b/src/currency.php @@ -6,32 +6,42 @@ function getRow($currency_from, $currency_to, $amount){ global $wf; // build URL - $url = "http://rate-exchange.appspot.com/currency?from=" . $currency_from . "&to=" . $currency_to; + $url = "https://api.exchangeratesapi.io/latest?base=" . $currency_from . "&symbols=" . $currency_to; // get exchange rate - $data_json = file_get_contents($url); + $options = array('http' => array('ignore_errors' => TRUE)); + $context = stream_context_create($options); + $data_json = file_get_contents($url, false, $context); $data = json_decode($data_json); - // Error occurec. Probabl invalid currency - if(isset($data->err)){ - $subtext = "Supported currencies are: DKK, EUR, USD, SEK, GBP, CHF, AUD, NOK"; - $wf->result(time(), $data->err, "Invalid currency", $subtext, 'icon.png'); + // print_r($data); - // No problems - }else{ - $exchange_rate = $data->rate; - - $clipboard = round($exchange_rate * $amount, 1); - $text = round($exchange_rate * $amount, 1) . " " . $currency_to; + if(!isset($data->error) && getHttpCode($http_response_header) == 200){ + $exchange_rate = $data->rates->$currency_to; + $clipboard = number_format($exchange_rate * $amount, 2); + $text = $clipboard . " " . $currency_to; $sub_text = $currency_from . ' to ' . $currency_to; - $wf->result(time(), $clipboard, $text, $sub_text, 'icon.png'); + } else { + $subtext = $data->error . " Please check if currencies are valid."; + $wf->result(time(), $data->err, "Could not fetch data", trim($subtext), 'icon.png'); } } +function getHttpCode($http_response_header) +{ + if(is_array($http_response_header)) + { + $parts=explode(' ',$http_response_header[0]); + if(count($parts)>1) //HTTP/1.0 + return intval($parts[1]); //Get code + } + return 0; +} + function getWaitingRow(){ global $wf; - $wf->result(time(), "Waiting for input", "Waiting for input...", "Type in like: 50USD to EUR", 'icon.png'); + $wf->result(time(), "Waiting for input", "Waiting for input...", "Type in like: 50 USD to EUR", 'icon.png'); } function getResult($query, $default_currency){ @@ -44,15 +54,23 @@ function getResult($query, $default_currency){ $query ); + // uppercase + $query = strtoupper($query); + // strip " to " - $query = str_replace(" to ", " ", $query); + $query = str_replace(" TO ", " ", $query); + $query = str_replace(" IN ", " ", $query); + + // trim + $query = trim($query); - // Uppercase and trim - $query = strtoupper(trim($query)); + // print_r('>>> query'); + // print_r($query); // parse query - preg_match("/^(\d+)([A-Z]{3})\s?([A-Z]{3})?$/", $query, $matches); + preg_match("/^(\d+)\s?([A-Z]{3})\s?([A-Z]{3})?$/", $query, $matches); + // print_r('>>> matches'); // print_r($matches); if(!empty($matches)){ @@ -70,4 +88,4 @@ function getResult($query, $default_currency){ return $wf->toxml(); } -// echo getResult("40usd $"); \ No newline at end of file +// echo getResult("41 GBP", "EUR"); diff --git a/src/info.plist b/src/info.plist deleted file mode 100644 index e85087e..0000000 --- a/src/info.plist +++ /dev/null @@ -1,85 +0,0 @@ - - - - - bundleid - currencyconverter - connections - - 63F60794-BB56-4415-9372-BAF974C3A7E1 - - - createdby - Søren Louv-Jansen - description - Convert currencies with ease - disabled - - name - Currency Converter - objects - - - config - - argumenttype - 0 - escaping - 36 - keyword - c - runningsubtext - Please wait while I fetch the newest exchange rates - script - // Type your default currency below. -// Must be in the official three-letter format -// Eg. EUR, USD, GBP -$default_currency = "DKK"; - -// ======= DO NOT EDIT BELOW THIS LINE ====== -require_once('currency.php'); -$query = "{query}"; -echo getResult($query, $default_currency); - subtext - Easily convert between currencies - title - Currency Converter - type - 1 - withspace - - - type - alfred.workflow.input.scriptfilter - uid - 63F60794-BB56-4415-9372-BAF974C3A7E1 - version - 0 - - - readme - Currency Converter is a workflow for Alfred App on Mac. - -The basic way to use the converter is to call Alfred with: -30USD to EUR - -The default currency is DKK (can be changed) so the following will will convert from USD to DKK: -30USD - -Another shorthand is: -30$ to € - -And even shorter: -30$ € - uidata - - 63F60794-BB56-4415-9372-BAF974C3A7E1 - - ypos - 60 - - - webaddress - konscript.com - -