Skip to content

Commit

Permalink
Fixed issue about encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
gdoslu committed Jan 6, 2017
1 parent f1133cf commit 3413267
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
'use strict';
var request = require('request');
var parseString = require('xml2js').parseString;
var iconv = require('iconv-lite');


/**
* Converts first letter of the given parameter to lowercase
Expand Down Expand Up @@ -40,16 +42,29 @@ function valueProcessors(val) {
}
}


module.exports = function(cb, dataSourceURL = 'http://www.tcmb.gov.tr/kurlar/today.xml') { // XML Source for Indicative Exchange Rates Announced by the Central Bank of Turkey

return request(dataSourceURL, (error, response, xml) => {
var options = {
url: dataSourceURL,
encoding: null,
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.95 Safari/537.36',
'Accept-Language': 'en-US,en;q=0.8,tr;q=0.6',
'Accept-Charset': 'ISO-8859-9'
},

};
return request(options, (error, response, body) => {
if (error) {
return cb(error, null);
}
if (response.statusCode != 200) {
return cb('Response code is: ' + response.statusCode, null);
}

var xml = iconv.decode(body, 'ISO-8859-9');

if (!xml || xml.search('Tarih_Date') === -1) {
return cb('Invalid xml', null);
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tcmb-doviz-kuru",
"version": "2.0.2",
"version": "2.0.3",
"description": "Get Indicative Exchange Rates Announced by the Central Bank of Turkey",
"main": "index.js",
"scripts": {
Expand All @@ -26,6 +26,7 @@
"lira"
],
"dependencies": {
"iconv-lite": "^0.4.15",
"request": "^2.75.0",
"xml2js": "^0.4.17"
},
Expand Down

0 comments on commit 3413267

Please sign in to comment.