Skip to content

Commit

Permalink
Merge pull request #1 from martinzuern/fixed-api
Browse files Browse the repository at this point in the history
Changed API endpoint
  • Loading branch information
sorenlouv authored Feb 8, 2019
2 parents 8a36719 + c626628 commit 0949442
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 119 deletions.
Binary file modified Currency Converter.alfredworkflow
Binary file not shown.
40 changes: 25 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -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))
Binary file removed src/.DS_Store
Binary file not shown.
56 changes: 37 additions & 19 deletions src/currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code> <text>
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){
Expand All @@ -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)){

Expand All @@ -70,4 +88,4 @@ function getResult($query, $default_currency){
return $wf->toxml();
}

// echo getResult("40usd $");
// echo getResult("41 GBP", "EUR");
85 changes: 0 additions & 85 deletions src/info.plist

This file was deleted.

0 comments on commit 0949442

Please sign in to comment.