-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCity.js
32 lines (28 loc) · 839 Bytes
/
getCity.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
import config from "./config.js";
export default async function (city) {
const corsProxy = config.corsProxy;
const coreURL = `https://weatherspark.com/search?q=${city}&ic=false&c=0&p=1`;
const url = corsProxy + coreURL;
const parser = new DOMParser();
return fetch(url, {
headers: {
accept: "application/json",
},
})
.then((response) => response.text())
.then((text) => parser.parseFromString(text, "text/html"))
.then((doc) => {
const results = [];
doc.querySelectorAll(".Search-Result-title").forEach((link) => {
results.push({
city: link.innerText,
url: "/" + link.href.split("/").slice(-3).join("/"),
});
});
return results;
})
.catch((error) => {
console.log(error);
});
}
// class Search-Result-title anchor tag