Speedtest.net servers in JSON

parent b49110f7
......@@ -97,37 +97,72 @@ router.get("/ip", (req: Request, res: Response, next: NextFunction) => {
});
});
enum DataReturnFormat {
Html = "html",
Json = "json",
Jsonp = "jsonp",
}
/**
* Browse Speedtest.net servers
*/
router.get("/speedtest-servers/", (req: Request, res: Response, next: NextFunction) => {
Axios.get(`https://www.speedtest.net/speedtest-servers.php`)
.then(x => x.data)
.then(rawXmlData => {
const parser = new xml2js.Parser({});
parser.parseStringPromise(rawXmlData)
.then(data => data.settings.servers)
.then(data => data[0].server)
.then(servers => {
const finalData = [];
servers.forEach(server => {
finalData.push(server['$']);
});
return finalData;
})
.then(servers => {
res.render("explore/speedtest-servers.twig", {
title: `Browse Speedtest.net servers`,
canonical_url: `/explore/speedtest-servers/`,
servers,
let format: DataReturnFormat;
switch (req.query.format) {
case DataReturnFormat.Json:
format = DataReturnFormat.Json;
break;
case DataReturnFormat.Jsonp:
format = DataReturnFormat.Jsonp;
break;
case "" || undefined:
format = DataReturnFormat.Html;
break;
default:
break;
}
if(format) {
Axios.get(`https://www.speedtest.net/speedtest-servers.php`)
.then(x => x.data)
.then(rawXmlData => {
const parser = new xml2js.Parser({});
parser.parseStringPromise(rawXmlData)
.then(data => data.settings.servers)
.then(data => data[0].server)
.then(servers => {
const finalData = [];
servers.forEach(server => {
finalData.push(server['$']);
});
return finalData;
})
})
.catch(next);
})
.catch(next)
.then(servers => {
if(format === DataReturnFormat.Json) {
res.json(servers);
} else if(format === DataReturnFormat.Jsonp) {
res.jsonp(servers);
} else {
res.render("explore/speedtest-servers.twig", {
title: `Browse Speedtest.net servers`,
canonical_url: `/explore/speedtest-servers/`,
servers,
});
}
})
.catch(next);
})
.catch(next)
} else {
next();
}
});
export const ExploreController: Router = router;
......@@ -35,6 +35,8 @@
{% endfor %}
</tbody>
</table>
<p>Also available in <a href="/explore/speedtest-servers?format=json">JSON</a> and <a href="https://www.speedtest.net/speedtest-servers.php">XML</a>.</p>
{% endblock %}
{% block javascripts %}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment