UNPKG

mersul-trenurilor

Version:
93 lines (78 loc) 4.33 kB
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var scrapeIt = require("scrape-it"); var axios = require("axios"); var puppeteer = require("puppeteer"); var MersulTrenurilor = function () { function MersulTrenurilor() { _classCallCheck(this, MersulTrenurilor); } _createClass(MersulTrenurilor, null, [{ key: "train", /** * train * Information about a given train ID. * * @name train * @function * @param {Number} id The train ID (e.g. `367`, not IR367) * @return {Promise} A promise resolving with the scraped train information: * * - `train_category` (String): The train category. * - `train_number` (String): The train number. * - `route` (Array): An array of objects containing: * - `arrive_time` (String): The arrive time. * - `arrive_time_comment` (String): Arrive time comment (such as delays) * - `location` (String): The stop information. * - `distance` (String): The number of the km in the route. * - `stop_comment (String): The stop comment, such as how long the stop time is. * - `leave_time` (String): The leave time. * - `leave_time_comment` (String): The leave time comment. * */ value: async function train(id) { var pageUrl = "https://mersultrenurilor.infofer.ro/ro-RO/Tren/" + id; var browser = await puppeteer.launch(_defineProperty({ headless: true, args: [] }, "headless", "old")); var page = await browser.newPage(); await page.goto(pageUrl); await page.setViewport({ width: 1080, height: 1024 }); await page.waitForSelector("#div-results > .jumbotron"); var textSelector = await page.waitForSelector('#div-results'); var resultsHTML = await textSelector.evaluate(function (el) { return el.innerHTML; }); var res = await scrapeIt.scrapeHTML(resultsHTML, { train_category: { selector: ".span-train-category-ir:eq(0)", convert: function convert(t) { return t.trim(); } }, train_number: { selector: ".color-blue", eq: 0 }, route: { listItem: ".list-group > .list-group-item", data: { arrive_time: ".div-middle:eq(0) .text-1-3rem", arrive_time_comment: ".div-middle:eq(0) .text-0-8rem.color-gray", location: "a:eq(0)", distance: ".div-middle:eq(1) .col-md-2", stop_comment: ".col-md-3:eq(0)", leave_time: ".col-3.col-md-2 .div-middle .text-0-8rem.text-right", leave_time_comment: ".col-3.col-md-2 .div-middle .text-0-8rem.text-right.color-gray" } } }); res.route = res.route.filter(function (c) { return Object.keys(c).some(function (k) { return c[k]; }); }); await browser.close(); return res; } }]); return MersulTrenurilor; }(); module.exports = MersulTrenurilor;