Guest User

Untitled

a guest
Jun 11th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const https = require('https');
  2. const url = 'https://www.foreca.fi';
  3.  
  4. const findValues = (data, startIndex) => {
  5.     const windDirections = ['N','NE','E','SE','S','SW','W','NW']
  6.     let min = data.indexOf('cclist', startIndex);
  7.     let max = data.indexOf('cclist', min+1);
  8.     let from=0, to=0, value="", location="", windDirection="", unit;
  9.     if (max < 0) max = data.length-1;
  10.     from = data.indexOf('val ', min);
  11.     try {
  12.         while (from >= 0) {
  13.             from = data.indexOf('">', from);
  14.             if(from > max || from < 0) break;
  15.             to = data.indexOf(' ', from);
  16.             value = data.slice(from+'">'.length, to);
  17.    
  18.             const indexTemp = from;
  19.             from = data.indexOf('title="', from);
  20.             if(from > max) from = indexTemp;
  21.             else {
  22.                 to = data.indexOf('"', from+'title="'.length);
  23.                 windDirection = data.slice(from+'title="'.length, to);
  24.                 if(!windDirections.includes(windDirection)) from = indexTemp;
  25.             }
  26.             from = data.indexOf('blue">', from);
  27.             if(from > max) break;
  28.             to = data.indexOf('</', from);
  29.             location = data.slice(from+'blue">'.length, to);
  30.  
  31.             unit = windDirections.includes(windDirection) ? "m/s " + windDirection : "°C";
  32.             console.log(location, value, unit);
  33.             from = data.indexOf('val ', to);
  34.             if(from > max) break;
  35.         }
  36.     } catch (e) {
  37.         console.log(e);
  38.     }
  39. }
  40. const findTime = (data, className) => {
  41.     let text = "";
  42.     let time = "";
  43.     const searchString = `<h3 class="${className}"><em>`
  44.     let index = data.indexOf(searchString);
  45.     if (index >= 0) {
  46.         let from = index+searchString.length;
  47.         try {
  48.             let to = data.indexOf('</',from);
  49.             text = data.slice(from, to);
  50.             from = data.indexOf('klo', to);
  51.             to = data.indexOf('</', from);
  52.             time = data.slice(from, to);            
  53.         } catch (e) {
  54.             console.log(e);
  55.         }
  56.     console.log(text, time);
  57.     console.log("==========================================");
  58.     findValues(data, index)
  59.     }
  60. }
  61. const getPage = async () => {
  62.     await https.get(url, (res) => {
  63.         let data = '';
  64.         res.on('data', (chunk) => {
  65.             data += chunk;
  66.         });
  67.     // called when the complete response is received.
  68.         res.on('end', () => {
  69.             try {
  70.               findTime(data, "tr_tmax");
  71.               console.log("");
  72.               findTime(data, "tr_tmin");
  73.               console.log("");
  74.               findTime(data, "tr_wind");
  75.               console.log("");
  76.             } catch (e) {
  77.               console.error(e)
  78.             }    
  79.         });
  80.     }).on("error", (err) => {
  81.         console.log("Error: ", err.message);
  82.     });
  83. }
  84.  
  85. getPage();
Advertisement
Add Comment
Please, Sign In to add comment