yahoo-exchange
Advanced tools
Comparing version 1.5.2 to 1.5.3
150
index.js
@@ -28,6 +28,11 @@ "use strict"; | ||
}, (err, response, html) => { | ||
if (err) | ||
errorHandler(err, pair); | ||
else | ||
callback(parseHTML2(html.toString().replace(',', ''), parseR(html.toString())), pair); | ||
try { | ||
if (err) | ||
errorHandler(err, pair); | ||
else | ||
callback(parseHTML2(html.toString().replace(',', ''), parseR(html.toString())), pair); | ||
} | ||
catch (e) { | ||
errorHandler(e, 'yahoo-exchange: Unknown Error'); | ||
} | ||
}); | ||
@@ -52,60 +57,70 @@ function getExchangeDataLowTrafficP() { | ||
function getExchangeDataLowTraffic(callback, errorHandler = err => console.log(err)) { | ||
request({ | ||
url: 'https://finance.yahoo.com/currencies', | ||
encoding: null | ||
}, (err, response, html) => { | ||
let h = html.toString().split(`data-reactid=\"75\"`)[2]; | ||
if (err) | ||
errorHandler(err); | ||
else { | ||
const pair = h.match(/>(...\/...)/gmi); | ||
const price = h.match(/">([0-9,.]+)/gmi); | ||
const changes = h.match(/ -->[^0-9reactspa/><\-]*[0-9.\-]+/gmi); | ||
callback(arrayLen24.map((v, a) => [remove(pair[a], '>'), parseFloat(remove(price[a], '\">')), parseFloat(remove(changes[a * 2], " -->")), parseFloat(remove(changes[a * 2 + 1], ' -->'))])); | ||
} | ||
}); | ||
try { | ||
request({ | ||
url: 'https://finance.yahoo.com/currencies', | ||
encoding: null | ||
}, (err, response, html) => { | ||
let h = html.toString().split(`data-reactid=\"75\"`)[2]; | ||
if (err) | ||
errorHandler(err); | ||
else { | ||
const pair = h.match(/>(...\/...)/gmi); | ||
const price = h.match(/">([0-9,.]+)/gmi); | ||
const changes = h.match(/ -->[^0-9reactspa/><\-]*[0-9.\-]+/gmi); | ||
callback(arrayLen24.map((v, a) => [remove(pair[a], '>'), parseFloat(remove(price[a], '\">')), parseFloat(remove(changes[a * 2], " -->")), parseFloat(remove(changes[a * 2 + 1], ' -->'))])); | ||
} | ||
}); | ||
} | ||
catch (e) { | ||
errorHandler(e, 'yahoo-exchange: Unknown Error'); | ||
} | ||
} | ||
exports.getExchangeDataLowTraffic = getExchangeDataLowTraffic; | ||
function getFxYahooJapan(callback, errorHandler = err => console.log(err)) { | ||
request({ | ||
url: 'https://info.finance.yahoo.co.jp/fx/list/', | ||
encoding: null | ||
}, (err, response, html) => { | ||
let h = html.toString(); | ||
if (err) | ||
errorHandler(err); | ||
else { | ||
const data = h.match(/......_chart_...">[0-9.]*/gmi); | ||
let dv = { | ||
USDJPY: {}, | ||
EURJPY: {}, | ||
AUDJPY: {}, | ||
GBPJPY: {}, | ||
NZDJPY: {}, | ||
CADJPY: {}, | ||
CHFJPY: {}, | ||
ZARJPY: {}, | ||
CNHJPY: {}, | ||
EURUSD: {}, | ||
GBPUSD: {}, | ||
AUDUSD: {}, | ||
NZDUSD: {}, | ||
HKDJPY: {}, | ||
EURGBP: {}, | ||
EURAUD: {}, | ||
USDCHF: {}, | ||
EURCHF: {}, | ||
GBPCHF: {}, | ||
AUDCHF: {}, | ||
CADCHF: {}, | ||
USDHKD: {} | ||
}; | ||
Object.keys(dv).map(v => dv[v] = data.reduce((prev, current) => { | ||
if (current.replace(/_.*/, '') === v) | ||
prev.push(parseFloat(current.replace(/......_chart_....>/, ''))); | ||
return prev; | ||
}, [])); | ||
callback(dv); | ||
} | ||
}); | ||
try { | ||
request({ | ||
url: 'https://info.finance.yahoo.co.jp/fx/list/', | ||
encoding: null | ||
}, (err, response, html) => { | ||
let h = html.toString(); | ||
if (err) | ||
errorHandler(err); | ||
else { | ||
const data = h.match(/......_chart_...">[0-9.]*/gmi); | ||
let dv = { | ||
USDJPY: {}, | ||
EURJPY: {}, | ||
AUDJPY: {}, | ||
GBPJPY: {}, | ||
NZDJPY: {}, | ||
CADJPY: {}, | ||
CHFJPY: {}, | ||
ZARJPY: {}, | ||
CNHJPY: {}, | ||
EURUSD: {}, | ||
GBPUSD: {}, | ||
AUDUSD: {}, | ||
NZDUSD: {}, | ||
HKDJPY: {}, | ||
EURGBP: {}, | ||
EURAUD: {}, | ||
USDCHF: {}, | ||
EURCHF: {}, | ||
GBPCHF: {}, | ||
AUDCHF: {}, | ||
CADCHF: {}, | ||
USDHKD: {} | ||
}; | ||
Object.keys(dv).map(v => dv[v] = data.reduce((prev, current) => { | ||
if (current.replace(/_.*/, '') === v) | ||
prev.push(parseFloat(current.replace(/......_chart_....>/, ''))); | ||
return prev; | ||
}, [])); | ||
callback(dv); | ||
} | ||
}); | ||
} | ||
catch (e) { | ||
errorHandler(e, 'yahoo-exchange: Unknown Error'); | ||
} | ||
} | ||
@@ -144,8 +159,13 @@ exports.getFxYahooJapan = getFxYahooJapan; | ||
function getExchangeDataArray(pair, callback, errorHandler = err => console.log(err)) { | ||
if (typeof pair === 'string') | ||
req(pair, errorHandler, callback); | ||
else if (Array.isArray(pair)) | ||
pair.forEach(v => req(v, errorHandler, callback)); | ||
else | ||
errorHandler(new Error('A pair must be "string" or "array".')); | ||
try { | ||
if (typeof pair === 'string') | ||
req(pair, errorHandler, callback); | ||
else if (Array.isArray(pair)) | ||
pair.forEach(v => req(v, errorHandler, callback)); | ||
else | ||
errorHandler(new Error('A pair must be "string" or "array".')); | ||
} | ||
catch (e) { | ||
errorHandler(e, 'yahoo-exchange: Unknown Error'); | ||
} | ||
} | ||
@@ -152,0 +172,0 @@ exports.getExchangeDataArray = getExchangeDataArray; |
134
index.ts
@@ -1,2 +0,2 @@ | ||
import request = require("request"); | ||
import request = require('request'); | ||
@@ -26,4 +26,8 @@ const arrayLen24 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
}, (err, response, html) => { | ||
if (err) errorHandler(err, pair); | ||
else callback(parseHTML2(html.toString().replace(',', ''), parseR(html.toString())), pair); | ||
try { | ||
if (err) errorHandler(err, pair); | ||
else callback(parseHTML2(html.toString().replace(',', ''), parseR(html.toString())), pair); | ||
} catch (e) { | ||
errorHandler(e, 'yahoo-exchange: Unknown Error'); | ||
} | ||
}); | ||
@@ -48,58 +52,66 @@ | ||
export function getExchangeDataLowTraffic(callback: (data: Array<Array<any>>) => any, errorHandler: (error: Error, pair?: String) => any = err => console.log(err)): void { | ||
request({ | ||
url: 'https://finance.yahoo.com/currencies', | ||
encoding: null | ||
}, (err, response, html) => { | ||
let h = html.toString().split(`data-reactid=\"75\"`)[2]; | ||
if (err) errorHandler(err); | ||
else { | ||
const pair = h.match(/>(...\/...)/gmi); | ||
const price = h.match(/">([0-9,.]+)/gmi); | ||
const changes = h.match(/ -->[^0-9reactspa/><\-]*[0-9.\-]+/gmi); | ||
callback(arrayLen24.map((v, a) => [remove(pair[a], '>'), parseFloat(remove(price[a], '\">')), parseFloat(remove(changes[a * 2], " -->")), parseFloat(remove(changes[a * 2 + 1], ' -->'))])); | ||
} | ||
}); | ||
try { | ||
request({ | ||
url: 'https://finance.yahoo.com/currencies', | ||
encoding: null | ||
}, (err, response, html) => { | ||
let h = html.toString().split(`data-reactid=\"75\"`)[2]; | ||
if (err) errorHandler(err); | ||
else { | ||
const pair = h.match(/>(...\/...)/gmi); | ||
const price = h.match(/">([0-9,.]+)/gmi); | ||
const changes = h.match(/ -->[^0-9reactspa/><\-]*[0-9.\-]+/gmi); | ||
callback(arrayLen24.map((v, a) => [remove(pair[a], '>'), parseFloat(remove(price[a], '\">')), parseFloat(remove(changes[a * 2], " -->")), parseFloat(remove(changes[a * 2 + 1], ' -->'))])); | ||
} | ||
}); | ||
} catch (e) { | ||
errorHandler(e, 'yahoo-exchange: Unknown Error'); | ||
} | ||
} | ||
export function getFxYahooJapan(callback: (data: Object) => any, errorHandler: (error: Error, pair?: String) => any = err => console.log(err)): void { | ||
request({ | ||
url: 'https://info.finance.yahoo.co.jp/fx/list/', | ||
encoding: null | ||
}, (err, response, html) => { | ||
let h = html.toString(); | ||
if (err) errorHandler(err); | ||
else { | ||
const data = h.match(/......_chart_...">[0-9.]*/gmi); | ||
let dv = { | ||
USDJPY: {}, | ||
EURJPY: {}, | ||
AUDJPY: {}, | ||
GBPJPY: {}, | ||
NZDJPY: {}, | ||
CADJPY: {}, | ||
CHFJPY: {}, | ||
ZARJPY: {}, | ||
CNHJPY: {}, | ||
EURUSD: {}, | ||
GBPUSD: {}, | ||
AUDUSD: {}, | ||
NZDUSD: {}, | ||
HKDJPY: {}, | ||
EURGBP: {}, | ||
EURAUD: {}, | ||
USDCHF: {}, | ||
EURCHF: {}, | ||
GBPCHF: {}, | ||
AUDCHF: {}, | ||
CADCHF: {}, | ||
USDHKD: {} | ||
}; | ||
Object.keys(dv).map(v => | ||
dv[v] = data.reduce((prev, current) => { | ||
if (current.replace(/_.*/, '') === v) prev.push(parseFloat(current.replace(/......_chart_....>/, ''))); | ||
return prev; | ||
}, [])); | ||
callback(dv); | ||
} | ||
}); | ||
try { | ||
request({ | ||
url: 'https://info.finance.yahoo.co.jp/fx/list/', | ||
encoding: null | ||
}, (err, response, html) => { | ||
let h = html.toString(); | ||
if (err) errorHandler(err); | ||
else { | ||
const data = h.match(/......_chart_...">[0-9.]*/gmi); | ||
let dv = { | ||
USDJPY: {}, | ||
EURJPY: {}, | ||
AUDJPY: {}, | ||
GBPJPY: {}, | ||
NZDJPY: {}, | ||
CADJPY: {}, | ||
CHFJPY: {}, | ||
ZARJPY: {}, | ||
CNHJPY: {}, | ||
EURUSD: {}, | ||
GBPUSD: {}, | ||
AUDUSD: {}, | ||
NZDUSD: {}, | ||
HKDJPY: {}, | ||
EURGBP: {}, | ||
EURAUD: {}, | ||
USDCHF: {}, | ||
EURCHF: {}, | ||
GBPCHF: {}, | ||
AUDCHF: {}, | ||
CADCHF: {}, | ||
USDHKD: {} | ||
}; | ||
Object.keys(dv).map(v => | ||
dv[v] = data.reduce((prev, current) => { | ||
if (current.replace(/_.*/, '') === v) prev.push(parseFloat(current.replace(/......_chart_....>/, ''))); | ||
return prev; | ||
}, [])); | ||
callback(dv); | ||
} | ||
}); | ||
} catch (e) { | ||
errorHandler(e, 'yahoo-exchange: Unknown Error'); | ||
} | ||
} | ||
@@ -137,5 +149,9 @@ | ||
export function getExchangeDataArray(pair: any, callback: (data: Array<number>, pair?: string) => any, errorHandler: (error: Error, pair?: String) => any = err => console.log(err)): void { | ||
if (typeof pair === 'string') req(pair, errorHandler, callback); | ||
else if (Array.isArray(pair)) pair.forEach(v => req(v, errorHandler, callback)); | ||
else errorHandler(new Error('A pair must be "string" or "array".')); | ||
try { | ||
if (typeof pair === 'string') req(pair, errorHandler, callback); | ||
else if (Array.isArray(pair)) pair.forEach(v => req(v, errorHandler, callback)); | ||
else errorHandler(new Error('A pair must be "string" or "array".')); | ||
} catch (e) { | ||
errorHandler(e, 'yahoo-exchange: Unknown Error'); | ||
} | ||
} | ||
@@ -142,0 +158,0 @@ |
@@ -14,6 +14,6 @@ { | ||
"main": "index.js", | ||
"version": "1.5.2", | ||
"version": "1.5.3", | ||
"license": "MIT", | ||
"dependencies": { | ||
"request": "^2.83.0" | ||
"request": "^2.85.0" | ||
}, | ||
@@ -20,0 +20,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1048842
357
Updatedrequest@^2.85.0