Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- var __assign = (this && this.__assign) || function () {
- __assign = Object.assign || function(t) {
- for (var s, i = 1, n = arguments.length; i < n; i++) {
- s = arguments[i];
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
- t[p] = s[p];
- }
- return t;
- };
- return __assign.apply(this, arguments);
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- var express = require("express");
- var axios = require("axios");
- var resources = require("./resources");
- var _a = require("country-emoji"), flag = _a.flag, code = _a.code, name = _a.name, countries = _a.countries;
- /**
- * Definizione Router
- */
- var router = express.Router();
- var setRoute = function (path, data) {
- return router.get(path, function (req, res) {
- return res.status(200).send({
- data: data(),
- success: true
- });
- });
- };
- // Qui i Controllers
- var countriesAutocomplete = [];
- var continents = {};
- var timeseries = {};
- var world = {};
- setRoute("/api/world", function () { return world; });
- //Gli Helpers
- var cronDataInterval = function (cb, refreshMilliseconds) {
- if (refreshMilliseconds) {
- setInterval(function () { return cb(); }, refreshMilliseconds);
- }
- cb();
- };
- cronDataInterval(function () {
- return axios
- .get(resources.timeseries)
- .then(function (response) {
- var cTimeseries = {};
- for (var prop in response.data) {
- if (response.data.hasOwnProperty(prop)) {
- var newProp = void 0;
- switch (prop) {
- case "Mainland China":
- newProp = "China";
- break;
- case "US":
- case "USA":
- newProp = "United States";
- break;
- default:
- newProp = prop;
- }
- if (prop.indexOf("*") !== -1) {
- newProp = prop.split("*").join("");
- }
- cTimeseries[newProp] = response.data[prop];
- }
- }
- timeseries = cTimeseries;
- console.log("\u2705 " + new Date().toString() + " - Timeseries loaded");
- })
- .catch(function (error) {
- // errori vari
- console.log(error);
- });
- }, 1000 * 60 * 3);
- cronDataInterval(function () {
- return axios
- .all([
- axios.get(resources.scmp),
- axios.get(resources.countries),
- axios.get(resources.allStats)
- ])
- .then(function (responseArr) {
- var scmpResponse = responseArr[0];
- var countriesResponse = responseArr[1];
- var stats = responseArr[2];
- var tempMap = {};
- countriesResponse.data.forEach(function (value, index, array) {
- switch (value.country) {
- case "Mainland China":
- value.country = "China";
- case "US":
- case "USA":
- value.country = "United States";
- break;
- }
- if (value.country.indexOf("*") !== -1) {
- value.country = value.country.split("*").join("");
- }
- if (!tempMap[value.country]) {
- tempMap[value.country] = value;
- }
- });
- var countries = (scmpResponse.data && scmpResponse.data.entries) || [];
- // Ordino i Paesi
- countries = countries.sort(function (a, b) {
- return parseCommaNumber(a.cases) > parseCommaNumber(b.cases) ? -1 : 1;
- });
- var extraStats = {
- countriesImpacted: 0,
- countriesDeaths: 0,
- active: 0,
- critical: 0,
- todayDeaths: 0,
- todayCases: 0,
- unresolved: stats.data.cases - stats.data.deaths - stats.data.recovered
- };
- countries.forEach(function (value, index, array) {
- switch (value.country) {
- case "Mainland China":
- value.country = "China";
- break;
- case "US":
- case "USA":
- value.country = "United States";
- break;
- }
- if (value.country.indexOf("*") !== -1) {
- value.country = value.country.split("*").join("");
- }
- value.casesPerOneMillion = -1;
- value.critical = -1;
- value.active = -1;
- if (tempMap[value.country]) {
- value.casesPerOneMillion =
- tempMap[value.country].casesPerOneMillion;
- value.critical = tempMap[value.country].critical;
- value.active = tempMap[value.country].active;
- extraStats.active += tempMap[value.country].active || 0;
- extraStats.critical += tempMap[value.country].critical || 0;
- extraStats.todayCases += tempMap[value.country].todayCases || 0;
- extraStats.todayDeaths += tempMap[value.country].todayDeaths || 0;
- }
- value.cases = parseCommaNumber(value.cases);
- value.deaths = parseCommaNumber(value.deaths);
- value.recovered = parseCommaNumber(value.recovered);
- value.unresolved = value.cases - value.deaths - value.recovered;
- value.fatalityRate =
- value.cases >= 0 && value.deaths >= 0
- ? Number(((value.deaths / value.cases) * 100).toFixed(2))
- : null;
- extraStats.countriesImpacted += value.cases > 0 ? 1 : 0;
- extraStats.countriesDeaths += value.deaths > 0 ? 1 : 0;
- value.flag = flag(value.country);
- value.code = code(value.country);
- setRoute("/api/country/" + value.code, function () {
- return __assign({ ac: countriesAutocomplete, timeseries: timeseries[value.country] || [] }, value);
- });
- var f = countriesAutocomplete.find(function (item) { return item.data == value.code; });
- if (!f) {
- countriesAutocomplete.push({
- value: value.flag + " " + value.country,
- data: value.code
- });
- }
- var continent = value.continent.charAt(0).toUpperCase() + value.continent.slice(1);
- if (!continents[continent]) {
- continents[continent] = {
- cases: 0,
- deaths: 0,
- recovered: 0,
- critical: 0,
- active: 0,
- unresolved: 0,
- fatalityRate: 0
- };
- }
- continents[continent].cases += value.cases >= 0 ? value.cases : 0;
- continents[continent].deaths += value.deaths >= 0 ? value.deaths : 0;
- continents[continent].recovered += value.recovered >= 0 ? value.recovered : 0;
- continents[continent].critical += value.critical >= 0 ? value.critical : 0;
- continents[continent].active += value.active >= 0 ? value.active : 0;
- continents[continent].unresolved += value.unresolved >= 0 ? value.unresolved : 0;
- });
- scmpResponse.data.stats = __assign(__assign({}, stats.data), extraStats);
- scmpResponse.data.ac = countriesAutocomplete;
- //scmpResponse.data.continents = []; qui cerco di svuotare, il problema me lo crea continents[]
- scmpResponse.data.continents = continents;
- world = scmpResponse.data;
- console.log("\u2705 " + new Date().toString() + " - World SCMP loaded");
- })
- .catch(function (error) {
- console.log(error);
- });
- }, 1000 * 60 * 3);
- var parseCommaNumber = function (x) {
- return parseInt(parseFloat(x.replace(/,/g, "")));
- };
- module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement