Last active
August 29, 2015 14:18
-
-
Save lukem512/18dc58b0192700663320 to your computer and use it in GitHub Desktop.
StartCOIN Ticker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"START": "StartCOIN", | |
"BTC": "Bitcoin", | |
"USD": "US Dollar", | |
"EUR": "Eurozone Euro", | |
"GBP": "Pound Sterling", | |
"JPY": "Japanese Yen", | |
"CAD": "Canadian Dollar", | |
"AUD": "Australian Dollar", | |
"CNY": "Chinese Yuan", | |
"CHF": "Swiss Franc", | |
"SEK": "Swedish Krona", | |
"NZD": "New Zealand Dollar", | |
"KRW": "South Korean Won", | |
"AED": "UAE Dirham", | |
"AFN": "Afghan Afghani", | |
"ALL": "Albanian Lek", | |
"AMD": "Armenian Dram", | |
"ANG": "Netherlands Antillean Guilder", | |
"AOA": "Angolan Kwanza", | |
"ARS": "Argentine Peso", | |
"AWG": "Aruban Florin", | |
"AZN": "Azerbaijani Manat", | |
"BAM": "Bosnia-Herzegovina Convertible Mark", | |
"BBD": "Barbadian Dollar", | |
"BDT": "Bangladeshi Taka", | |
"BGN": "Bulgarian Lev", | |
"BHD": "Bahraini Dinar", | |
"BIF": "Burundian Franc", | |
"BMD": "Bermudan Dollar", | |
"BND": "Brunei Dollar", | |
"BOB": "Bolivian Boliviano", | |
"BRL": "Brasilian Real", | |
"BSD": "Bahamian Dollar", | |
"BTN": "Bhutanese Ngultrum", | |
"BWP": "Botswanan Pula", | |
"BYR": "Belarusian Ruble", | |
"BZD": "Belize Dollar", | |
"CDF": "Congolese Franc", | |
"CLF": "Chilean Unit of Account (UF)", | |
"CLP": "Chilean Peso", | |
"COP": "Colombian Peso", | |
"CRC": "Costa Rican Colón", | |
"CVE": "Cape Verdean Escudo", | |
"CZK": "Czech Koruna", | |
"DJF": "Djiboutian Franc", | |
"DKK": "Danish Krone", | |
"DOP": "Dominican Peso", | |
"DZD": "Algerian Dinar", | |
"EEK": "Estonian Kroon", | |
"EGP": "Egyptian Pound", | |
"ETB": "Ethiopian Birr", | |
"FJD": "Fijian Dollar", | |
"FKP": "Falkland Islands Pound", | |
"GEL": "Georgian Lari", | |
"GHS": "Ghanaian Cedi", | |
"GIP": "Gibraltar Pound", | |
"GMD": "Gambian Dalasi", | |
"GNF": "Guinean Franc", | |
"GTQ": "Guatemalan Quetzal", | |
"GYD": "Guyanaese Dollar", | |
"HKD": "Hong Kong Dollar", | |
"HNL": "Honduran Lempira", | |
"HRK": "Croatian Kuna", | |
"HTG": "Haitian Gourde", | |
"HUF": "Hungarian Forint", | |
"IDR": "Indonesian Rupiah", | |
"ILS": "Israeli Shekel", | |
"INR": "Indian Rupee", | |
"IQD": "Iraqi Dinar", | |
"ISK": "Icelandic Króna", | |
"JEP": "Jersey Pound", | |
"JMD": "Jamaican Dollar", | |
"JOD": "Jordanian Dinar", | |
"KES": "Kenyan Shilling", | |
"KGS": "Kyrgystani Som", | |
"KHR": "Cambodian Riel", | |
"KMF": "Comorian Franc", | |
"KWD": "Kuwaiti Dinar", | |
"KYD": "Cayman Islands Dollar", | |
"KZT": "Kazakhstani Tenge", | |
"LAK": "Laotian Kip", | |
"LBP": "Lebanese Pound", | |
"LKR": "Sri Lankan Rupee", | |
"LRD": "Liberian Dollar", | |
"LSL": "Lesotho Loti", | |
"LTL": "Lithuanian Litas", | |
"LVL": "Latvian Lats", | |
"LYD": "Libyan Dinar", | |
"MAD": "Moroccan Dirham", | |
"MDL": "Moldovan Leu", | |
"MGA": "Malagasy Ariary", | |
"MKD": "Macedonian Denar", | |
"MMK": "Myanma Kyat", | |
"MNT": "Mongolian Tugrik", | |
"MOP": "Macanese Pataca", | |
"MRO": "Mauritanian Ouguiya", | |
"MUR": "Mauritian Rupee", | |
"MVR": "Maldivian Rufiyaa", | |
"MWK": "Malawian Kwacha", | |
"MXN": "Mexican Peso", | |
"MYR": "Malaysian Ringgit", | |
"MZN": "Mozambican Metical", | |
"NAD": "Namibian Dollar", | |
"NGN": "Nigerian Naira", | |
"NIO": "Nicaraguan Córdoba", | |
"NOK": "Norwegian Krone", | |
"NPR": "Nepalese Rupee", | |
"OMR": "Omani Rial", | |
"PAB": "Panamanian Balboa", | |
"PEN": "Peruvian Nuevo Sol", | |
"PGK": "Papua New Guinean Kina", | |
"PHP": "Philippine Peso", | |
"PKR": "Pakistani Rupee", | |
"PLN": "Polish Zloty", | |
"PYG": "Paraguayan Guarani", | |
"QAR": "Qatari Rial", | |
"RON": "Romanian Leu", | |
"RSD": "Serbian Dinar", | |
"RUB": "Russian Ruble", | |
"RWF": "Rwandan Franc", | |
"SAR": "Saudi Riyal", | |
"SBD": "Solomon Islands Dollar", | |
"SCR": "Seychellois Rupee", | |
"SDG": "Sudanese Pound", | |
"SGD": "Singapore Dollar", | |
"SHP": "Saint Helena Pound", | |
"SLL": "Sierra Leonean Leone", | |
"SOS": "Somali Shilling", | |
"SRD": "Surinamese Dollar", | |
"STD": "São Tomé and Príncipe Dobra", | |
"SVC": "Salvadoran Colón", | |
"SYP": "Syrian Pound", | |
"SZL": "Swazi Lilangeni", | |
"THB": "Thai Baht", | |
"TJS": "Tajikistani Somoni", | |
"TMT": "Turkmenistani Manat", | |
"TND": "Tunisian Dinar", | |
"TOP": "Tongan Paʻanga", | |
"TRY": "Turkish Lira", | |
"TTD": "Trinidad and Tobago Dollar", | |
"TWD": "New Taiwan Dollar", | |
"TZS": "Tanzanian Shilling", | |
"UAH": "Ukrainian Hryvnia", | |
"UGX": "Ugandan Shilling", | |
"UYU": "Uruguayan Peso", | |
"UZS": "Uzbekistan Som", | |
"VEF": "Venezuelan Bolívar Fuerte", | |
"VND": "Vietnamese Dong", | |
"VUV": "Vanuatu Vatu", | |
"WST": "Samoan Tala", | |
"XAF": "CFA Franc BEAC", | |
"XAG": "Silver (troy ounce)", | |
"XAU": "Gold (troy ounce)", | |
"XCD": "East Caribbean Dollar", | |
"XOF": "CFA Franc BCEAO", | |
"XPF": "CFP Franc", | |
"YER": "Yemeni Rial", | |
"ZAR": "South African Rand", | |
"ZMW": "Zambian Kwacha", | |
"ZWL": "Zimbabwean Dollar" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "StartCOIN Ticker", | |
"description": "A ticker script to convert a price in StartCOIN to that in a number of other currencies.", | |
"version": "0.0.1", | |
"author": { | |
"name": "Luke Mitchell", | |
"email": "[email protected]" | |
} | |
"dependencies": { | |
"express": "4.12.3", | |
"request": "2.54.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require("fs"); | |
var host = "127.0.0.1"; | |
var port = 3000; | |
var express = require("express"); | |
var request = require('request'); | |
// Load currency names | |
var currencies = require('./currencies.json'); | |
var usage = function(res, req) { | |
var url = req.protocol + "://" + req.get("host"); | |
res.status(404).send("Usage: " + url + "/currency/CURRENCY or " + url + "/all"); | |
} | |
// Retrieve the BTC price from Bittrex | |
var getBittrexPrice = function(callback) { | |
var options = { | |
uri: "https://bittrex.com/api/v1.1/public/getticker?market=btc-start", | |
method: "GET", | |
timeout: 10000, | |
followRedirect: true, | |
maxRedirects: 10 | |
}; | |
request(options, function (err, res, body) { | |
// Catch any errors | |
if (err || res.statusCode !== 200 ) { | |
return callback(err); | |
} | |
// Success, parse and display | |
var obj = JSON.parse(body); | |
var price = parseFloat(obj.result.Last); | |
callback(null, price); | |
}); | |
} | |
var getBitcoinAveragePrice = function(currency, callback) { | |
var options = { | |
uri: "https://api.bitcoinaverage.com/ticker/global/" + currency, | |
method: "GET", | |
timeout: 10000, | |
followRedirect: true, | |
maxRedirects: 10 | |
}; | |
request(options, function (err, res, body) { | |
// Catch any errors | |
if (err || res.statusCode !== 200 ) { | |
return callback(err); | |
} | |
// Success, parse and display | |
if (currency == "all") { | |
var obj = JSON.parse(body); | |
var prices = []; | |
for(var pair in obj) { | |
if (obj[pair].last) { | |
prices[pair] = obj[pair].last; | |
} | |
} | |
callback(null, prices); | |
} else { | |
var obj = JSON.parse(body); | |
price = parseFloat(obj.last); | |
callback(null, price); | |
} | |
}); | |
} | |
var app = express(); | |
// Allow Cross-Origin-Resource-Sharing | |
app.use(function(req, res, next) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
next(); | |
}); | |
// Return all START-CURRENCY pairs | |
app.get("/all", function(req, res) { | |
getBittrexPrice(function(err, bittrexPrice) { | |
if (err) { | |
res.status(404); | |
return; | |
} | |
getBitcoinAveragePrice("all", function(err, bitcoinaveragePrices) { | |
if (err) { | |
res.status(404); | |
return; | |
} | |
var prices = []; | |
for(var currency in bitcoinaveragePrices) { | |
if (currencies[currency]) { | |
prices.push({ | |
"code": currency, | |
"name": currencies[currency], | |
"rate": (bitcoinaveragePrices[currency] * bittrexPrice).toFixed(8) | |
}); | |
} | |
} | |
res.send(JSON.stringify(prices)); | |
}); | |
}); | |
}); | |
// Return price for individual START-CURRENCY pair | |
app.get("/currency/:currency", function(req, res) { | |
var currency = req.params["currency"].toUpperCase(); | |
if (!currency) { | |
return usage(res, req); | |
} | |
getBittrexPrice(function(err, bittrexPrice) { | |
if (err) { | |
res.status(404); | |
return; | |
} | |
getBitcoinAveragePrice(currency, function(err, bitcoinaveragePrice) { | |
if (err) { | |
res.status(404); | |
return; | |
} | |
if (currencies[currency]) { | |
var result = { | |
"code": currency, | |
"name": currencies[currency], | |
"rate": (bitcoinaveragePrice * bittrexPrice).toFixed(8) | |
}; | |
} | |
res.send(JSON.stringify(result)); | |
}); | |
}); | |
}); | |
// 404 and show usage for all other requests | |
app.get("*", function(req, res) { | |
return usage(res, req); | |
}); | |
app.listen(port, host); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simple express app that will serve the current price of StartCOIN in a given pair currency