-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.mjs
147 lines (126 loc) · 3.69 KB
/
async.mjs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import fetch from 'node-fetch';
import express from 'express';
const rain_func = (list) => {
let prom = new Promise(resolve => {
let rain_bool = false
let rain_array = []
for (let i=0; i<list.length; i++){
if (list[i].hasOwnProperty('rain')){
rain_bool = true
rain_array[i] = list[i].rain["3h"]
}
else {
rain_array[i] = 0
}
}
resolve({rain_bool, rain_array})
})
return (prom)
}
const temp_func = (list) => {
return new Promise(resolve => {
var sum = 0
let temp = []
var clothes
for (let i=0; i<list.length; i++){
sum = sum + list[i].main.temp
temp[i] = list[i].main.temp
}
let avg_temp = sum / list.length - 273.15
if (avg_temp < 10){
clothes = "Cold"
}
else if (avg_temp >= 20){
clothes = "Hot"
}
else {
clothes = "Warm"
}
resolve({clothes, temp})
})
}
const wind_func = (list) => {
return new Promise(resolve => {
let wind = []
for(let i=0; i<list.length; i++){
if (list[i].hasOwnProperty('wind')){
wind[i] = list[i].wind.speed
}
else {
wind[i] = 0
}
}
resolve(wind)
})
}
const get_list = (location, key) => {
let url = 'https://api.openweathermap.org/data/2.5/forecast?q=' + location + '&appid=' + key
var requestOptions = {
method: 'GET',
redirect: 'follow'
};
return (fetch(url, requestOptions)
.then(response => response.json())
.then(result => {
let list = []
list = result.list
return(list)
})
.catch(error => console.log('error', error))
)
}
const coordinates_func = (location, key) => {
let url = 'https://api.openweathermap.org/data/2.5/weather?q=' + location + '&APPID=' + key
var requestOptions = {
method: 'GET',
redirect: 'follow'
};
return (fetch(url, requestOptions)
.then(response => response.json())
.then(result => {
return result.coord
})
.catch(error => console.log('error', error))
)
}
const mask_func = async(location, key) => {
let coord = await coordinates_func(location, key)
const url = 'http://api.openweathermap.org/data/2.5/air_pollution/forecast?lat=' + coord.lat + '&lon=' + coord.lon + '&appid=' + key
var requestOptions = {
method: 'GET',
redirect: 'follow'
};
return (fetch(url, requestOptions)
.then(response => response.json())
.then(result => {
let list = result.list
for (let i=0; i<list.length; i++){
if (list[i].components.pm2_5 > 10){
return true
}
}
return false
})
.catch(error => console.log('error', error))
)
}
const data_func = async(location, key) => {
let list, rain_return, temp_return, wind, umbrella, rain, clothes, temp, mask
list = await get_list(location, key)
rain_return = await rain_func(list)
temp_return = await temp_func(list)
wind = await wind_func(list)
mask = await mask_func(location, key)
umbrella = rain_return.rain_bool
rain = rain_return.rain_array
clothes = temp_return.clothes
temp = temp_return.temp
return({umbrella, clothes, rain, temp, wind, mask})
}
const create_object = async(location) => {
const key = "3e2d927d4f28b456c6bc662f34350957"
let data, mask
data = await data_func(location, key)
console.log(data)
}
create_object("Moscow,Russia")