<commit_before>var path = require('path');
var oxr = require('open-exchange-rates');
var fx = require('money');
var fs = require('fs');
var rfr = require('rfr');
var config = rfr('includes/config.js');

var openExchangeRatesAppId = config.openexchangerates.app_id || '';
var openExchangeRatesUpdateInterval = config.openexchangerates.update_interval || 86400;

if (config.openexchangerates.use_env_variable)
	openExchangeRatesAppId = process.env[openExchangeRatesAppId];

console.log(openExchangeRatesAppId);

var cache_filename = path.join(__dirname, '..', 'data/cache/exchangerates.json');
var cached = null;

var reloadRates = function(callback) {
	if (!openExchangeRatesAppId)
		return false;

	oxr.latest(function() {
		cached = {};
		cached.rates = oxr.rates;
		cached.base = oxr.base;
		fs.writeFile(cache_filename, JSON.stringify(cached, null, 2));

		if (typeof(callback) === 'function')
			callback(cached);
	});
};

var reloadIfNeeded = function(callback) {
	fs.stat(cache_filename, function(err, stats) {
		if (!stats || (stats.mtime.getTime() / 1000) < (Date.now() / 1000 | 0) - openExchangeRatesUpdateInterval)
			reloadRates(callback);
	});
};

if (!openExchangeRatesAppId) {
	console.error('Please specify openexchangerates app_id in config/config.json. Without it, dimeshift will not be able to do currency conversions');
} else {
	oxr.set({
		app_id: openExchangeRatesAppId
	});

	try {
		var cached = JSON.parse("" + fs.readFileSync(cache_filename, 'utf8'));
	} catch (e) {
		console.log("No cached exchange rates\n");
	}

	if (cached && typeof(cached.rates) !== 'undefined' && typeof(cached.base) !== 'undefined') {
		fx.rates = cached.rates;
		fx.base = cached.base;

		reloadIfNeeded();
	} else {
		reloadRates(function() {
			fx.rates = cached.rates;
			fx.base = cached.base;
		});
	}
}

var getRates = function() {
	reloadIfNeeded();
	return cached;
};

exports.fx = fx;
exports.getRates = getRates;
// fx(100).from('HKD').to('GBP'); // ~8.0424<commit_msg>x rates<commit_after>var path = require('path');
var oxr = require('open-exchange-rates');
var fx = require('money');
var fs = require('fs');
var rfr = require('rfr');
var config = rfr('includes/config.js');

var openExchangeRatesAppId = config.openexchangerates.app_id || '';
var openExchangeRatesUpdateInterval = config.openexchangerates.update_interval || 86400;

if (config.openexchangerates.use_env_variable)
	openExchangeRatesAppId = process.env[openExchangeRatesAppId];

var cache_filename = path.join(__dirname, '..', 'data/cache/exchangerates.json');
var cached = null;

var reloadRates = function(callback) {
	if (!openExchangeRatesAppId)
		return false;

	oxr.latest(function() {
		cached = {};
		cached.rates = oxr.rates;
		cached.base = oxr.base;
		fs.writeFile(cache_filename, JSON.stringify(cached, null, 2));

		if (typeof(callback) === 'function')
			callback(cached);
	});
};

var reloadIfNeeded = function(callback) {
	fs.stat(cache_filename, function(err, stats) {
		if (!stats || (stats.mtime.getTime() / 1000) < (Date.now() / 1000 | 0) - openExchangeRatesUpdateInterval) {
			reloadRates(callback);
		}
	});
};

var cached = {
	rates: {},
	base: 'USD'
};

if (!openExchangeRatesAppId) {
	cached.error = 'Please specify openexchangerates app_id in config/config.json. Without it, dimeshift will not be able to do currency conversions';
	console.error(cached.error);
} else {
	oxr.set({
		app_id: openExchangeRatesAppId
	});

	try {
		cached = JSON.parse("" + fs.readFileSync(cache_filename, 'utf8'));
	} catch (e) {
		console.log("No cached exchange rates\n");
	}

	if (cached && typeof(cached.rates) !== 'undefined' && typeof(cached.base) !== 'undefined') {
		fx.rates = cached.rates;
		fx.base = cached.base;

		reloadIfNeeded();
	} else {
		reloadRates(function() {
			fx.rates = cached.rates;
			fx.base = cached.base;
		});
	}
}

var getRates = function() {
	console.log(cached);
	reloadIfNeeded();
	return cached;
};

exports.fx = fx;
exports.getRates = getRates;
// fx(100).from('HKD').to('GBP'); // ~8.0424
{'fraction_non_alphanumeric': 0.1625996015936255, 'fraction_numerical': 0.01195219123505976, 'mean_word_length': 4.763271162123386, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5354787', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1396, 'n_words': 280}