Skip to content

Commit

Permalink
Replace defunct Yahoo! Finance API with openexchangerates.org
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Nov 2, 2017
1 parent 9d4a43a commit 4803b7b
Show file tree
Hide file tree
Showing 22 changed files with 2,468 additions and 1,927 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/build
.cache

# Python snippets from gitignore on github
Expand Down
Binary file not shown.
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ You can also add your own custom units.

**Note:** Currency conversions do require occasional Internet connectivity to update exchange rates. Alfred-Convert will otherwise work just fine without an Internet connection.

<!-- MarkdownTOC autolink="true" bracket="round" depth="2" autoanchor="true" -->

- [Downloading](#downloading)
- [Usage](#usage)
- [Conversions](#conversions)
- [Configuration](#configuration)
- [Active currencies](#active-currencies)
- [Custom units](#custom-units)
- [Supported units](#supported-units)
- [Supported currencies](#supported-currencies)
- [Adding custom units](#adding-custom-units)
- [Releases](#releases)
- [Thanks, copyright, licensing](#thanks-copyright-licensing)

<!-- /MarkdownTOC -->


<a name="downloading"></a>
Downloading
-----------

Expand All @@ -33,6 +36,7 @@ Download from [GitHub releases][ghreleases].
**Note**: Version 3.0 and above only supports Alfred 3. If you're still using Alfred 2, please download [v2.6][v2.6].


<a name="usage"></a>
Usage
-----

Expand All @@ -47,8 +51,11 @@ Usage
- `Edit Custom Units` — Edit the list of custom currencies in your default text editor


<a name="conversions"></a>
### Conversions ###

**NOTE**: To perform conversions between fiat currencies, you must set a key for the [openexchangerates.org][openx] API in the workflow's [configuration sheet](#configuration). You can sign up for a free account [here][openx-free].

- `conv <quantity> <from unit> [<to unit>]` — Perform a conversion
- `` or `⌘C` — Copy the result to the pasteboard
- `⌘↩` — Add/remove destination unit as default for this dimensionality
Expand All @@ -69,6 +76,7 @@ It doesn't matter if there is a space between the quantity and the units or not.
Actioning an item (selecting it and hitting ``) will copy it to the clipboard. Using `⌘+L` will display the result in Alfred's large text window, `⌘+C` will copy the selected result to the clipboard.


<a name="configuration"></a>
### Configuration ###

The workflow is configured via the configuration sheet (`[𝓍]`) in Alfred Preferences and via a couple of text files in its data directory.
Expand All @@ -80,6 +88,7 @@ Basic configuration is performed in the configuration sheet:

| Option | Meaning |
|-----------------------|----------------------------------------------------------------------------------------------|
| `APP_KEY` | API key for [openexchangerates.org][openx] |
| `COPY_UNIT` | Include unit when copying conversion result. Any value but `0` or empty turns this option on |
| `DECIMAL_PLACES` | Number of decimal places to show in results |
| `DECIMAL_SEPARATOR` | Character to separate whole numbers and decimal fractions |
Expand All @@ -89,7 +98,7 @@ Basic configuration is performed in the configuration sheet:

#### Active currencies ####

By default, all supported fiat currencies and a handful of the most popular cryptocurrencies are active.
By default, all supported fiat currencies (provided you've set `APP_KEY` in the [configuration sheet](#configuration)) and a handful of the most popular cryptocurrencies are active.

- `convinfo`
- `View All Supported Currencies`
Expand All @@ -105,6 +114,7 @@ You can use `View All Supported Currencies` to search for the currency you'd lik
See [Adding custom units](#adding-custom-units).


<a name="supported-units"></a>
Supported units
---------------

Expand All @@ -113,6 +123,7 @@ Currently, Alfred-Convert only supports [the units][pintunits] understood by the
You can [add your own custom units](#adding-custom-units) to the workflow. If you think they'd be useful to everyone, please create a corresponding [GitHub issue][ghissues] to request addition as a default unit or submit a [pull request][ghpulls].


<a name="supported-currencies"></a>
### Supported currencies ###

To convert, use the appropriate **abbreviation** for the relevant currencies, e.g. `conv 100 eur gbp`.
Expand All @@ -122,6 +133,7 @@ You can also view (and search) the list from within Alfred by using the keyword
[All supported currencies](./docs/currencies.md).


<a name="adding-custom-units"></a>
### Adding custom units ###

You can add your own custom units using the [format defined by Pint][pinthowto]. Add your definitions to the `unit_definitions.txt` file in the workflow's data directory.
Expand All @@ -131,13 +143,15 @@ To edit this file, enter `convinfo` in Alfred and select `Edit Custom Units`. Th
Please see the [Pint documentation][pinthowto] for the required format. See Pint's [default unit definitions][pintunits] for examples.


<a name="releases"></a>
Releases
--------

See [CHANGELOG][changelog] for more information.

| Release | Date |
|-------------|----------------|
| [3.1][v3.1] | 2017-11-02 |
| [3.0][v3.0] | 2017-07-16 |
| [2.6][v2.6] | 2017-06-15 |
| [2.5][v2.5] | 2015-12-11 |
Expand All @@ -150,12 +164,13 @@ See [CHANGELOG][changelog] for more information.
| [1.1][v1.1] | 2014-08-09 |


<a name="thanks-copyright-licensing"></a>
Thanks, copyright, licensing
----------------------------

- The Python [Pint][pintdocs] library does all the heavy lifting. See the [Pint GitHub repo][pintrepo] for Pint licensing or `LICENSE.txt` and `AUTHORS.txt` in the `vendor/pint` subdirectory.
- The Python [Pint][pintdocs] library does all the heavy lifting. See the [Pint GitHub repo][pintrepo] for Pint licensing or `LICENSE.txt` and `AUTHORS.txt` in the `pint` subdirectory.
- The workflow icons are from [Font Awesome][fontawesome]
- Exchange rates are downloaded from [Yahoo! Finance][yahoo-finance] and [CryptoCompare][cryptocompare] (for cryptocurrencies).
- Exchange rates are downloaded from [openexchangerates.org][openx] and [CryptoCompare][cryptocompare] (for cryptocurrencies).
- The [Alfred-Workflow][alfred-workflow] library is used heavily.

All other code/media are released under the [MIT Licence][mit].
Expand Down Expand Up @@ -184,5 +199,7 @@ All other code/media are released under the [MIT Licence][mit].
[v2.5]: https://github.com/deanishe/alfred-convert/releases/tag/v2.5
[v2.6]: https://github.com/deanishe/alfred-convert/releases/tag/v2.6
[v3.0]: https://github.com/deanishe/alfred-convert/releases/tag/v3.0
[yahoo-finance]: https://finance.yahoo.com/
[v3.1]: https://github.com/deanishe/alfred-convert/releases/tag/v3.1
[cryptocompare]: https://www.cryptocompare.com/
[openx]: https://openexchangerates.org/
[openx-free]: https://openexchangerates.org/signup/free
43 changes: 43 additions & 0 deletions bin/hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env zsh

# Reject if APP_KEY is set

# _error <arg>.. | Write red "error" and args to STDERR
_error() {
echo $(print -P '%F{red}error%f') "$@" >&2
}

# _fail <arg>.. | Write red "error" and args to STDERR, then exit with status 1
_fail() {
_error "$@"
exit 1
}

# _staged <path> | Is path staged
_staged() {
local p="$1"
git diff --name-only --cached | grep --silent "$p"
return $?
}

exec 1>&2 # Redirect output to stderr.

_staged info.plist || exit 0

root="$( git rev-parse --show-toplevel )"
ip="${root}/src/info.plist"

# _getvar <name> | Retrieve value for variable <name>
_getvar() {
local n=$1
/usr/libexec/PlistBuddy -c "Print :variables:$n" "$ip"
}

# Fail if info.plist can't be found
test -f "$ip" || { _fail "$ip not found"; }

api_key="$( _getvar APP_KEY )"

test -n "$api_key" && _fail "[info.plist] APP_KEY is not empty"

exit 0
Binary file modified currencies/ISO 4217 List One.xlsx
Binary file not shown.
29 changes: 15 additions & 14 deletions currencies/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@

# Currencies #
Currencies
==========

`currencies_yahoo.py` throws a whole bunch of currencies at Yahoo! Finance, and saves a list of the ones it appears to know about.
Lists of currencies and scripts to generate them.

| File | Description |
|---------------------------|-------------------------------------|
| `currencies_iso_4217.tsv` | All ISO 4217 currency codes |
| `currencies_custom.tsv` | Special currencies, e.g. Bitcoin |
| `currencies_yahoo.tsv` | Exchange rates offered by Yahoo! |
| `currencies_yahoo.py` | Script to generate above list |
| `ISO 4217 List One.xlsx` | Source list from the [ISO][iso4217] |
| File | Description |
|-------------------------------|-----------------------------------------------------------|
| `currencies_crypto.tsv` | Cryptocurrencies supported by [cryptocompare.com][crypto] |
| `currencies_crypto.py` | Script to generate above list |
| `currencies_iso_4217.tsv` | Most ISO 4217 currency codes |
| `currencies_custom.tsv` | Unofficial currencies |
| `currencies_openexchange.tsv` | Exchange rates offered by [openexchangerates.org][openx] |
| `currencies_openexchange.py` | Script to generate above list |
| `ISO 4217 List One.xlsx` | Source list from the [ISO][iso4217] |

The first two TSV files are the input list of currencies. `currencies_yahoo.tsv` is the list of currencies that Yahoo! Finance has exchange rates for.

This file is the `currencies.tsv` file distributed with the workflow.

[iso4217]: http://www.iso.org/iso/home/standards/currency_codes.htm
[iso4217]: https://www.iso.org/iso-4217-currency-codes.html
[openx]: https://openexchangerates.org/
[crypto]: https://www.cryptocompare.com/
48 changes: 37 additions & 11 deletions currencies/currencies_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

from collections import namedtuple
import csv
from itertools import izip_longest
import os
import requests
from time import sleep

reference_currency = 'EUR'
reference_currency = 'USD'

all_currencies_url = 'https://www.cryptocompare.com/api/data/coinlist/'
base_url = 'https://min-api.cryptocompare.com/data/price?fsym={}&tsyms={}'
Expand All @@ -32,6 +33,26 @@
Currency = namedtuple('Currency', 'symbol name')


def grouper(n, iterable):
"""Return iterable that splits `iterable` into groups of size `n`.
Args:
n (int): Size of each group.
iterable (iterable): The iterable to split into groups.
Returns:
list: Tuples of length `n` containing items
from `iterable`.
"""
sentinel = object()
args = [iter(iterable)] * n
groups = []
for l in izip_longest(*args, fillvalue=sentinel):
groups.append([v for v in l if v is not sentinel])
return groups


def main():
"""Generate list of cryptocurrencies with exchange rates."""
r = requests.get(all_currencies_url)
Expand All @@ -45,19 +66,24 @@ def main():

print('%d total currencies' % len(all_currencies))

for c in sorted(all_currencies):
url = base_url.format(reference_currency, c.symbol)
# for c in sorted(all_currencies):
for currencies in grouper(20, all_currencies):
url = base_url.format(reference_currency,
','.join([c.symbol for c in currencies]))
r = requests.get(url)
r.raise_for_status()
data = r.json()
if data.get('Response'):
print('[%s] ERROR: %s' % (c.symbol, data.get('Message')))
invalid.add(c.symbol)
else:
print('[%s] OK' % c.symbol)
sleep(1)

valid = [c for c in all_currencies if c.symbol not in invalid]
for c in currencies:
if c.symbol in data:
valid.append(c)
print('[%s] OK' % c.symbol)
else:
invalid.add(c)
print('[%s] ERROR' % c.symbol)

sleep(0.3)

# valid = [c for c in all_currencies if c.symbol not in invalid]
with open(crypto_currencies_file, 'wb') as fp:
w = csv.writer(fp, delimiter='\t')
for c in sorted(valid, key=lambda t: t.symbol):
Expand Down
Loading

0 comments on commit 4803b7b

Please sign in to comment.