Skip to content

Instantly share code, notes, and snippets.

@ketanmistry
Last active July 8, 2018 01:19
Show Gist options
  • Save ketanmistry/2c9df76628346d5538887ac2f209d34a to your computer and use it in GitHub Desktop.
Save ketanmistry/2c9df76628346d5538887ac2f209d34a to your computer and use it in GitHub Desktop.
BitBar PHP script to return GBP to AUD rate using fixer.io's API.
#!/usr/bin/php
<?php
# -*- coding: utf-8 -*-
# <bitbar.title>Currency Tracker</bitbar.title>
# <bitbar.version>1.0</bitbar.version>
# <bitbar.author>Ketan Mistry</bitbar.author>
# <bitbar.author.github></bitbar.author.github>
# <bitbar.desc>Currency checker using fixer.io.</bitbar.desc>
# <bitbar.image></bitbar.image>
// This script returns the current GBP -> AUD rate from the base EUR currency.
// Fixer.io API credentials
$api_key = '';
$currency_from = 'EUR';
$currency_to = array('GBP','AUD');
$endpoint = sprintf('http://data.fixer.io/api/latest?access_key=%s&base=%s&symbols=%s', $api_key, $currency_from, implode(',', $currency_to));
// Do the call and return php array
$data = file_get_contents($endpoint);
$data = json_decode($data, true);
// Set the rates we know from the response
$eur_gbp = $data['rates']['GBP'];
$eur_aud = $data['rates']['AUD'];
// Now work out the conversion of 1 GBP to AUD
$gbp_aud = ($eur_aud / $eur_gbp);
// Some settings
$color = '';
$label = ':moneybag:';
$rounding = 3;
$good_rate = 1.76;
$mid_rate = 1.7;
// Add color! :)
if ($gbp_aud >= $good_rate) {
$color = ' | color=springgreen';
$label = ':fire:';
} else if ($gbp_aud < $mid_rate) {
$color = ' | color=crimson';
} else if ($gbp_aud >= $mid_rate && $gbp_aud < $high_rate) {
$color = ' | color=orange';
}
// Echo data to BitBar for display
echo sprintf("%s%s%s\n", $label, number_format($gbp_aud, $rounding), $color);
echo "---\n";
echo sprintf("Base Currency: %s\n", 'GBP');
echo sprintf("Last Checked: %s UTC", $data['date']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment