Added resources downloader and hash getter

This commit is contained in:
Jiiks 2016-10-31 18:47:59 +02:00
parent e7b56fe792
commit 9416be4105
1 changed files with 36 additions and 1 deletions

View File

@ -12,6 +12,36 @@
class Utils {
//Get the latest commit hash from github
getHash(callback) {
this.download("api.github.com", "/repos/Jiiks/BetterDiscordApp/commits/master", data => {
//TODO use the new soon to be implemented tryparse
callback(JSON.parse(data).sha);
});
}
download(host, path, callback) {
_logger.log(`Downloading Resource: ${host}${path}`);
var options = {
host: host,
path: path,
headers: this.headers
};
_https.get(options, res => {
var data = "";
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
callback(data);
});
}).on('error', () => {
//TODO error logging
callback(null);
});
}
//Returns a datestring: [DD/MM/YYYY - HH:MM:SS]
get dateString() {
var d = new Date();
@ -24,6 +54,10 @@ class Utils {
`${("00" + d.getSeconds()).slice(-2)}`;
}
get headers() {
return { 'user-agent': 'Mozilla/5.0' };
}
}
class Logger {
@ -59,7 +93,8 @@ class Logger {
const _utils = new Utils(),
_logger = new Logger(),
_fs = require('fs'),
{EOL} = require('os');
{EOL} = require('os'),
_https = require('https');
exports._utils = _utils;
exports._logger = _logger;