-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClimaTempo.engine.ts
115 lines (106 loc) · 5.71 KB
/
ClimaTempo.engine.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import fetch from 'node-fetch';
import DailyWeatherModel from '../../Models/DailyWeather.model';
import InstantWeatherModel from '../../Models/InstantWeather.model';
import LocaleModel from '../../Models/Locale.model';
import DetailedLocaleModel from '../../Models/DetailedLocale.model';
/**
* This is a web scrapper engine to get the weather info I wanted to from any city.
* Yeah, I could use the official API but was too expensive and web scrapping was too easy
* */
class ClimaTempoEngine {
static baseUrl = 'https://www.climatempo.com.br';
async detailedWeatherCollectionByCityCode(code: number): Promise<DailyWeatherModel[]> {
return await fetch(`${ ClimaTempoEngine.baseUrl }/previsao-do-tempo/15-dias/cidade/${ code }`)
.then(res => res.text())
.then(data => {
const weathers = data.split('<section')
.filter(data => data.startsWith(' class="accordion-card'))
.map((data, index) => {
const list = data.slice(data.indexOf('wrapper-variables-cards')).split('variable-card')
list[2] = list[2].slice(list[2].indexOf('Chuva'));
list[3] = list[3].slice(list[3].indexOf('Vento'));
const temperature = list[1].split('<span');
const humidity = list[4].split('</span>');
const moon = this.partOf(list[7], 'Lua', '</span>', '</p>').split('</span>');
const date = new Date(new Date().getTime() - (new Date().getTimezoneOffset() * 60000 ));
date.setDate(date.getDate() + index);
return {
temperature: {
min: Number(this.partOf(temperature[2], '>', '>', '°<')),
max: Number(this.partOf(temperature[3], '>', '>', '°<'))
},
rain: {
precipitation: Number(this.partOf(list[2], '<span', '>', 'mm')),
probability: Number(this.partOf(list[2], '<span', ' -', '%').trimStart())
},
wind: {
compass: this.partOf(list[3], '</span>', '>', '-').trim(),
velocity: Number(this.partOf(list[3], '</span>', '-', 'km/h').trimStart())
},
humidity: {
min: Number(this.partOf(humidity[1], '<span', '>', '%')),
max: Number(this.partOf(humidity[2], '<span', '>', '%'))
},
sun: {
morning: this.partOf(list[6], 'Sol', '</span>', ' - ').trim(),
afternoon: this.partOf(list[6], 'Sol', ' - ', '</p>').trim()
},
moon: moon.map(line => line.slice(0, line.indexOf('<')).replace('\n-', ' -').trim()),
rainbow: this.partOf(list[5], 'title', '"', '"'),
description: this.partOf(data, '<p', '>', '</p>').trim(),
date: date.toISOString().slice(0, 10),
idcity: code
} as DailyWeatherModel;
});
if(weathers.reduce((isValid: boolean, weather) => weather.description.length > 5 ? isValid : false, true))
return weathers;
return Promise.reject('Invalid response.');
}).then(data => {
if(!data || data.length != 15)
return Promise.reject('Invalid response.');
return data;
});
}
async weatherByCityId(id: number): Promise<InstantWeatherModel> {
return await fetch(`${ ClimaTempoEngine.baseUrl }/json/myclimatempo/user/weatherNow?idlocale=${id}`)
.then(res => res.json())
.then((data: any) => {
if(!data?.data?.getWeatherNow?.[0]?.data?.[0]?.weather)
return Promise.reject('Invalid response.');
return {
idlocale: id,
...data.data.getWeatherNow[0].data[0].weather
} as InstantWeatherModel;
});
}
async cityInfoById(id: number): Promise<LocaleModel> {
return await fetch(`${ ClimaTempoEngine.baseUrl }/json/myclimatempo/user/weatherNow?idlocale=${id}`)
.then(res => res.json())
.then((data: any) => {
if(!data?.data?.getWeatherNow?.[0]?.data?.[0]?.locale)
return Promise.reject('Invalid response.');
return data.data.getWeatherNow[0].data[0].locale as LocaleModel;
});
}
async citiesFromSearchByName(name: string): Promise<DetailedLocaleModel[]> {
const body = new URLSearchParams();
body.append('name', name);
return await fetch(`${ ClimaTempoEngine.baseUrl }/json/busca-cidades`, {
method: 'POST',
body
})
.then(res => res.json())
.then((data: any) => data[0].response.data as DetailedLocaleModel[])
.catch(err => {
console.log(err);
return [] as DetailedLocaleModel[];
});
}
private partOf(subject: string, before: string, start: string, end?: string, keepStart = false) {
let part = subject.slice(subject.indexOf(before));
part = part.slice(part.indexOf(start));
part = part.slice(keepStart ? 0 : start.length, end ? part.indexOf(end, start.length) : undefined);
return part;
}
}
export default ClimaTempoEngine;