Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const https = require('https');
- const url = 'https://www.foreca.fi';
- const findValues = (data, startIndex) => {
- const windDirections = ['N','NE','E','SE','S','SW','W','NW']
- let min = data.indexOf('cclist', startIndex);
- let max = data.indexOf('cclist', min+1);
- let from=0, to=0, value="", location="", windDirection="", unit;
- if (max < 0) max = data.length-1;
- from = data.indexOf('val ', min);
- try {
- while (from >= 0) {
- from = data.indexOf('">', from);
- if(from > max || from < 0) break;
- to = data.indexOf(' ', from);
- value = data.slice(from+'">'.length, to);
- const indexTemp = from;
- from = data.indexOf('title="', from);
- if(from > max) from = indexTemp;
- else {
- to = data.indexOf('"', from+'title="'.length);
- windDirection = data.slice(from+'title="'.length, to);
- if(!windDirections.includes(windDirection)) from = indexTemp;
- }
- from = data.indexOf('blue">', from);
- if(from > max) break;
- to = data.indexOf('</', from);
- location = data.slice(from+'blue">'.length, to);
- unit = windDirections.includes(windDirection) ? "m/s " + windDirection : "°C";
- console.log(location, value, unit);
- from = data.indexOf('val ', to);
- if(from > max) break;
- }
- } catch (e) {
- console.log(e);
- }
- }
- const findTime = (data, className) => {
- let text = "";
- let time = "";
- const searchString = `<h3 class="${className}"><em>`
- let index = data.indexOf(searchString);
- if (index >= 0) {
- let from = index+searchString.length;
- try {
- let to = data.indexOf('</',from);
- text = data.slice(from, to);
- from = data.indexOf('klo', to);
- to = data.indexOf('</', from);
- time = data.slice(from, to);
- } catch (e) {
- console.log(e);
- }
- console.log(text, time);
- console.log("==========================================");
- findValues(data, index)
- }
- }
- const getPage = async () => {
- await https.get(url, (res) => {
- let data = '';
- res.on('data', (chunk) => {
- data += chunk;
- });
- // called when the complete response is received.
- res.on('end', () => {
- try {
- findTime(data, "tr_tmax");
- console.log("");
- findTime(data, "tr_tmin");
- console.log("");
- findTime(data, "tr_wind");
- console.log("");
- } catch (e) {
- console.error(e)
- }
- });
- }).on("error", (err) => {
- console.log("Error: ", err.message);
- });
- }
- getPage();
Advertisement
Add Comment
Please, Sign In to add comment