mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 19:53:36 +00:00
working version, use corsUrl in weather providers
This commit is contained in:
parent
7cfc7b9d74
commit
c622db918b
40
js/server.js
40
js/server.js
@ -78,23 +78,31 @@ function Server(config, callback) {
|
|||||||
app.use(directory, express.static(path.resolve(global.root_path + directory)));
|
app.use(directory, express.static(path.resolve(global.root_path + directory)));
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get("/cors", function (req, res) {
|
app.get("/cors", async function (req, res) {
|
||||||
// http://localhost:8080/cors?url=https://google.de
|
// example: http://localhost:8080/cors?url=https://google.de
|
||||||
console.dir(req.query.url);
|
|
||||||
|
|
||||||
fetch(req.query.url, { headers: { "User-Agent": "Mozilla/5.0 MagicMirror/" + global.version } })
|
try {
|
||||||
.then((response) => {
|
const reg = "^/cors.+url=(.*)";
|
||||||
global.mmCorsHeader = response.headers.get("Content-Type");
|
let url = "";
|
||||||
if (! global.mmCorsHeader) {global.mmCorsHeader = "application/text"}
|
|
||||||
return response.text();
|
let match = new RegExp(reg, "g").exec(req.url);
|
||||||
})
|
if (!match) {
|
||||||
.then((responseData) => {
|
url = "invalid url: " + req.url;
|
||||||
res.set("Content-Type", global.mmCorsHeader);
|
Log.error(url);
|
||||||
res.send(responseData);
|
res.send(url);
|
||||||
})
|
} else {
|
||||||
.catch((error) => {
|
url = match[1];
|
||||||
Log.error(error);
|
Log.log("cors url: " + url);
|
||||||
});
|
const response = await fetch(url, { headers: { "User-Agent": "Mozilla/5.0 MagicMirror/" + global.version } });
|
||||||
|
const header = response.headers.get("Content-Type");
|
||||||
|
const data = await response.text();
|
||||||
|
if (header) res.set("Content-Type", header);
|
||||||
|
res.send(data);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
Log.error(error);
|
||||||
|
res.send(error);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/version", function (req, res) {
|
app.get("/version", function (req, res) {
|
||||||
|
@ -18,7 +18,7 @@ WeatherProvider.register("darksky", {
|
|||||||
|
|
||||||
// Set the default config properties that is specific to this provider
|
// Set the default config properties that is specific to this provider
|
||||||
defaults: {
|
defaults: {
|
||||||
apiBase: "https://cors-anywhere.herokuapp.com/https://api.darksky.net",
|
apiBase: "https://api.darksky.net",
|
||||||
weatherEndpoint: "/forecast",
|
weatherEndpoint: "/forecast",
|
||||||
apiKey: "",
|
apiKey: "",
|
||||||
lat: 0,
|
lat: 0,
|
||||||
@ -67,7 +67,7 @@ WeatherProvider.register("darksky", {
|
|||||||
// Create a URL from the config and base URL.
|
// Create a URL from the config and base URL.
|
||||||
getUrl() {
|
getUrl() {
|
||||||
const units = this.units[this.config.units] || "auto";
|
const units = this.units[this.config.units] || "auto";
|
||||||
return `${this.config.apiBase}${this.config.weatherEndpoint}/${this.config.apiKey}/${this.config.lat},${this.config.lon}?units=${units}&lang=${this.config.lang}`;
|
return this.getCorsUrl() + `${this.config.apiBase}${this.config.weatherEndpoint}/${this.config.apiKey}/${this.config.lat},${this.config.lon}?units=${units}&lang=${this.config.lang}`;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Implement WeatherDay generator.
|
// Implement WeatherDay generator.
|
||||||
|
@ -164,7 +164,7 @@ WeatherProvider.register("envcanada", {
|
|||||||
// CORS errors when accessing EC
|
// CORS errors when accessing EC
|
||||||
//
|
//
|
||||||
getUrl() {
|
getUrl() {
|
||||||
return "http://localhost:8080/cors?url=https://dd.weather.gc.ca/citypage_weather/xml/" + this.config.provCode + "/" + this.config.siteCode + "_e.xml";
|
return this.getCorsUrl() + "https://dd.weather.gc.ca/citypage_weather/xml/" + this.config.provCode + "/" + this.config.siteCode + "_e.xml";
|
||||||
},
|
},
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -116,7 +116,7 @@ WeatherProvider.register("openweathermap", {
|
|||||||
* Gets the complete url for the request
|
* Gets the complete url for the request
|
||||||
*/
|
*/
|
||||||
getUrl() {
|
getUrl() {
|
||||||
return this.config.apiBase + this.config.apiVersion + this.config.weatherEndpoint + this.getParams();
|
return this.getCorsUrl() + this.config.apiBase + this.config.apiVersion + this.config.weatherEndpoint + this.getParams();
|
||||||
},
|
},
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -91,7 +91,7 @@ WeatherProvider.register("smhi", {
|
|||||||
getURL() {
|
getURL() {
|
||||||
let lon = this.config.lon;
|
let lon = this.config.lon;
|
||||||
let lat = this.config.lat;
|
let lat = this.config.lat;
|
||||||
return `https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/${lon}/lat/${lat}/data.json`;
|
return this.getCorsUrl() + `https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/${lon}/lat/${lat}/data.json`;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -73,7 +73,7 @@ WeatherProvider.register("ukmetoffice", {
|
|||||||
* Gets the complete url for the request
|
* Gets the complete url for the request
|
||||||
*/
|
*/
|
||||||
getUrl(forecastType) {
|
getUrl(forecastType) {
|
||||||
return this.config.apiBase + this.config.locationID + this.getParams(forecastType);
|
return this.getCorsUrl() + this.config.apiBase + this.config.locationID + this.getParams(forecastType);
|
||||||
},
|
},
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -63,7 +63,7 @@ WeatherProvider.register("ukmetofficedatahub", {
|
|||||||
queryStrings += "&includeLocationName=" + true;
|
queryStrings += "&includeLocationName=" + true;
|
||||||
|
|
||||||
// Return URL, making sure there is a trailing "/" in the base URL.
|
// Return URL, making sure there is a trailing "/" in the base URL.
|
||||||
return this.config.apiBase + (this.config.apiBase.endsWith("/") ? "" : "/") + forecastType + queryStrings;
|
return this.getCorsUrl() + this.config.apiBase + (this.config.apiBase.endsWith("/") ? "" : "/") + forecastType + queryStrings;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Build the list of headers for the request
|
// Build the list of headers for the request
|
||||||
|
@ -72,7 +72,7 @@ WeatherProvider.register("weatherbit", {
|
|||||||
// Create a URL from the config and base URL.
|
// Create a URL from the config and base URL.
|
||||||
getUrl() {
|
getUrl() {
|
||||||
const units = this.units[this.config.units] || "auto";
|
const units = this.units[this.config.units] || "auto";
|
||||||
return `${this.config.apiBase}${this.config.weatherEndpoint}?lat=${this.config.lat}&lon=${this.config.lon}&units=${units}&key=${this.config.apiKey}`;
|
return this.getCorsUrl() + `${this.config.apiBase}${this.config.weatherEndpoint}?lat=${this.config.lat}&lon=${this.config.lon}&units=${units}&key=${this.config.apiKey}`;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Implement WeatherDay generator.
|
// Implement WeatherDay generator.
|
||||||
|
@ -40,7 +40,7 @@ WeatherProvider.register("weathergov", {
|
|||||||
// Called to set the config, this config is the same as the weather module's config.
|
// Called to set the config, this config is the same as the weather module's config.
|
||||||
setConfig: function (config) {
|
setConfig: function (config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
(this.config.apiBase = "https://api.weather.gov"), this.fetchWxGovURLs(this.config);
|
(this.config.apiBase = this.getCorsUrl() + "https://api.weather.gov"), this.fetchWxGovURLs(this.config);
|
||||||
},
|
},
|
||||||
|
|
||||||
// Called when the weather provider is about to start.
|
// Called when the weather provider is about to start.
|
||||||
|
@ -111,6 +111,15 @@ const WeatherProvider = Class.extend({
|
|||||||
this.delegate.updateAvailable(this);
|
this.delegate.updateAvailable(this);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getCorsUrl: function () {
|
||||||
|
const url = window.config.address + ":" + window.config.port + "/cors?url=";
|
||||||
|
if (window.config.useHttps) {
|
||||||
|
return "https://" + url;
|
||||||
|
} else {
|
||||||
|
return "http://" + url;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// A convenience function to make requests. It returns a promise.
|
// A convenience function to make requests. It returns a promise.
|
||||||
fetchData: function (url, method = "GET", data = null) {
|
fetchData: function (url, method = "GET", data = null) {
|
||||||
const getData = function (mockData) {
|
const getData = function (mockData) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user