UNPKG

factbook

Version:

Serves as an independent data scraping module, complete with ontology and full scraping ability for the CIA World Factbook site

56 lines (55 loc) 2.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var funktologies_1 = require("funktologies"); var getUuid = require("uuid-by-string"); var constants_1 = require("../constants/constants"); var globalStore_1 = require("../constants/globalStore"); function getFlag(cheerioElem, country, countryId) { var objectProperties = globalStore_1.store.countries[countryId].objectProperties; var flag = funktologies_1.getRelation(objectProperties, constants_1.consts.ONTOLOGY.HAS_FLAG); var fId = constants_1.consts.ONTOLOGY.INST_FLAG + getUuid(country); var objectProp = {}; var bailOut = true; cheerioElem('div.flagBox').each(function () { if (!flag) { if (globalStore_1.store.nationalFlags[fId]) { objectProp[constants_1.consts.ONTOLOGY.HAS_FLAG] = globalStore_1.store.nationalFlags[fId]; } else { objectProp = funktologies_1.entityMaker(constants_1.consts.ONTOLOGY.HAS_FLAG, constants_1.consts.ONTOLOGY.ONT_FLAG, fId, "National Flag of " + country); globalStore_1.store.nationalFlags[fId] = objectProp[constants_1.consts.ONTOLOGY.HAS_FLAG]; } flag = objectProp[constants_1.consts.ONTOLOGY.HAS_FLAG]; globalStore_1.store.countries[countryId].objectProperties.push(funktologies_1.entityRefMaker(constants_1.consts.ONTOLOGY.HAS_FLAG, objectProp)); } bailOut = false; }); if (bailOut) { return; } cheerioElem('div.flagBox').each(function (index, element) { var a = cheerioElem(element).find('img').attr('src'); var flagImgUrl; if (a && a.replace('../', '')) { flagImgUrl = constants_1.consts.BASE.URL_BASE + a.replace('../', ''); } if (flagImgUrl) { flag.datatypeProperties[constants_1.consts.ONTOLOGY.DT_LOCATOR_URI] = flagImgUrl; } // TODO: scrape physical image from url and store it. }); cheerioElem('div.modalFlagDesc').each(function (index, element) { var b = cheerioElem(element).find('div.photogallery_captiontext').text().trim(); if (!b) { return; } if (flag) { flag.datatypeProperties[constants_1.consts.ONTOLOGY.DT_CONTENT_DESCRIPTION] = b.replace(/\\n/g, '').trim(); } else { flag.datatypeProperties[constants_1.consts.ONTOLOGY.DT_CONTENT_DESCRIPTION] = b.trim(); } }); } exports.getFlag = getFlag; ;