-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapiCrap.js
146 lines (135 loc) · 4.29 KB
/
apiCrap.js
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
//to do
// api for
// api for latitude and longitude - done
// finding gyms and parks in the local area - done
// api for air pollution information - done
// food hygiene
import fetch from 'node-fetch';
const mapsApiKey = process.env.NEXT_PUBLIC_MAPS_API_KEY; //parthivs key
const environApiKey = process.env.NEXT_PUBLIC_ENVIRON_API_KEY; //parthivs key
async function getTextSearchResponse(query,postcode,radiusVal) {
let highLowPair = await getHighLowPair(postcode,radiusVal);
const res = await fetch('https://places.googleapis.com/v1/places:searchText',{
method:'POST',
headers:{
'X-Goog-Api-Key':mapsApiKey,
'X-Goog-FieldMask':'places.displayName,places.shortFormattedAddress,places.websiteUri',
},
body: JSON.stringify({
textQuery:query,
locationRestriction:{
rectangle: {
low:{
latitude: highLowPair["low"]["latitude"] ,
longitude: highLowPair["low"]["longitude"]
},
high:{
latitude: highLowPair["high"]["latitude"],
longitude:highLowPair["high"]["longitude"]
},
}
}
})
})
const data = await res.json()
return data; //this is a json object
}
async function getAirQualityIndex(postcode){
const latLongPair = await getLatLongFromPostcode(postcode);
const res = await fetch(`https://airquality.googleapis.com/v1/currentConditions:lookup?key=${environApiKey}`,{
method: 'POST',
body: JSON.stringify({
location:{
latitude: latLongPair["latitude"],
longitude: latLongPair["longitude"]
},
extraComputations:["LOCAL_AQI"],
})
});
const data = await res.json();
console.log(data);
}
/* SAMPLE RESPONSE FOR AIR QUALITY INDEX DATA
{
dateTime: '2024-02-26T01:00:00Z',
regionCode: 'gb',
indexes: [
{
code: 'uaqi',
displayName: 'Universal AQI',
aqi: 74,
aqiDisplay: '74',
color: [Object],
category: 'Good air quality',
dominantPollutant: 'o3'
},
{
code: 'gbr_defra',
displayName: 'DAQI (UK)',
aqi: 2,
aqiDisplay: '2',
color: [Object],
category: 'Low air pollution',
dominantPollutant: 'o3'
}
]
}
*/
async function fetchNames(query,postcode,radius){
//imagine you are in google maps, 'query' is what you would type into the search bar
//data is a json object, postcode is a string, radius is a number
//console.log(data);
let data = await getTextSearchResponse(query,postcode,radius);
if(Object.keys(data).length==0){
console.log("nothing");
return;
}
return data["places"];
}
async function printNames(data){
console.log(data);
if(data=="nothing"){
console.log("nothing");
}
else{
for(const place of data){
console.log(place["displayName"]["text"]);
}
}
}
async function getLatLongFromPostcode(p){
const res = await fetch(`https://api.postcodes.io/postcodes/${p}`)
const data = await res.json();
const latLong = {
latitude: data["result"]["latitude"],
longitude: data["result"]["longitude"]
};
return latLong;
}
async function getHighLowPair(postcode, radius){
let postcodeCoords = await getLatLongFromPostcode(postcode);
let coordChanges = convertDistanceToCoordChange(radius/1000);
let highLowPair = {
high: {
latitude: postcodeCoords["latitude"]+coordChanges["latDeviance"],
longitude: postcodeCoords["longitude"]+coordChanges["longDeviance"]
},
low: {
latitude: postcodeCoords["latitude"]-coordChanges["latDeviance"],
longitude: postcodeCoords["longitude"]-coordChanges["longDeviance"]
}
}
return highLowPair;
}
function degreesToRadians(degrees){
return degrees*(Math.PI/180);
}
function convertDistanceToCoordChange(radiusInKm){
const latDeviance = radiusInKm/110.574;
return {
latDeviance : latDeviance,
longDeviance : 1/(111.320*Math.cos(degreesToRadians(latDeviance)))
}
}
//printNames(await fetchNames("gym", "BS14HJ", 5000));
getAirQualityIndex("BS14HJ");