From b43802655b0f9d42c42a90a9617b6d39247b9005 Mon Sep 17 00:00:00 2001 From: Stephen Parker Date: Sun, 5 May 2019 21:07:39 -0400 Subject: [PATCH] Add user-agent option, use current run cache setting for cache lookup expiry, remove metadata from cache files --- README.md | 1 + package.json | 2 +- src/withaspark.weather.cli.js | 3 ++ src/withaspark.weather.js | 59 ++++++++++++----------------------- 4 files changed, 25 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index cd9b9ea..4fd0671 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ path/to/src/withaspark.weather.cli.js --station=KJAX | `cache_dir` | "/tmp" | Directory to write cache files too. | | `cache_prefix` | "withaspark.weather." | Prefix for cache file names. | | `cache_lifetime` | 5 | Number of minutes to cache results before refetching. | +| `user_agent` | "@withaspark/weather User" | User agent to use when making requests to weather API. If experiencing rate-limiting issues, set to something unique. | | `unknown` | "?" | Symbol for unknown value. | diff --git a/package.json b/package.json index b68bff0..411cf60 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@withaspark/weather", - "version": "1.2.1", + "version": "1.3.0", "description": "Current weather and forecast fetching script", "main": "src/withaspark.weather.js", "scripts": { diff --git a/src/withaspark.weather.cli.js b/src/withaspark.weather.cli.js index 1ee6548..02fc3d5 100755 --- a/src/withaspark.weather.cli.js +++ b/src/withaspark.weather.cli.js @@ -13,6 +13,9 @@ var Weather = require("../src/withaspark.weather.js")({ cache_lifetime: args.hasOwnProperty("cache_lifetime") ? args.cache_lifetime : null, + user_agent: args.hasOwnProperty("user_agent") + ? args.user_agent + : null, unknown: args.hasOwnProperty("unknown") ? args.unknown : null diff --git a/src/withaspark.weather.js b/src/withaspark.weather.js index 6a8f4ea..a360707 100644 --- a/src/withaspark.weather.js +++ b/src/withaspark.weather.js @@ -22,6 +22,7 @@ function Weather(options) { this.config.station = options.station || "KJAX"; this.config.weather_url = "https://api.weather.gov/stations/{station}/observations/latest"; this.config.alert_url = "https://api.weather.gov/alerts/active?point={point}"; + this.config.user_agent = options.user_agent || "@withaspark/weather User"; this.config.cache_dir = (options.cache_dir || "/tmp").replace(new RegExp("/" + path.sep + "$/")); this.config.cache_prefix = options.cache_prefix || ("withaspark.weather." + this.config.station + "."); this.config.cache_lifetime = (options.hasOwnProperty("cache_lifetime")) @@ -74,13 +75,18 @@ function Weather(options) { : null, value = null; + // If value is cached, use it if (cached !== null) { value = cached; } - - if (saved !== null) { + // If value is saved, use it + else if (saved !== null) { value = saved; } + // If value is the name of a method, call it + else if (typeof that[key] === 'function') { + value = that[key](); + } // Round all numbers to integers if (that.isNumber(value)) { @@ -113,10 +119,9 @@ function Weather(options) { * * @param {string} key * @param {*} value - * @param {integer} expiry * @returns {*} Value of value */ - function cache(key, value, expiry) { + function cache(key, value) { if (typeof value === "undefined") { if (isCacheExpired(key)) { return null; @@ -125,11 +130,7 @@ function Weather(options) { return getFromCache(key); } - if (typeof expiry === "undefined") { - expiry = that.config.cache_lifetime; - } - - setInCache(key, value, expiry); + setInCache(key, value); return value; } @@ -141,14 +142,11 @@ function Weather(options) { * @returns {boolean} */ function isCacheExpired(key) { - var data = (getFromCache(key, true) || "").split("\n", 2); - - // Invalid cache file, just say it is expired - if (data.length !== 2) { + if (!fs.existsSync(getKeyCacheFile(key))) { return true; } - var expiry = data[0].split(":", 2)[1], + var expiry = that.config.cache_lifetime, age = getCacheAge(key); return (age >= expiry); @@ -175,23 +173,18 @@ function Weather(options) { * * @param {string} key * @param {*} value - * @param {*} expiry * @returns {void} */ - function setInCache(key, value, expiry) { + function setInCache(key, value, ) { if (typeof key !== "string" || typeof value === "undefined") { throw "Unable to set value in cache. String type key and any value is required."; } - if (typeof expiry === 'undefined') { - expiry = that.cache_lifetime; - } - that.data[key] = value; fs.writeFileSync( getKeyCacheFile(key), - "expires:" + expiry + "\n" + value, + value, function (err) { if (err) { return console.error(err); @@ -204,29 +197,18 @@ function Weather(options) { * Get the data for key from cache. * * @param {string} key - * @param {boolean} include_headers If true, will include metadata in return value * @returns {string} */ - function getFromCache(key, include_headers) { + function getFromCache(key) { if (typeof key !== "string") { throw "Unable to get value from cache. String type key is required."; } - if (typeof include_headers !== "boolean") { - include_headers = false; - } - if (!fs.existsSync(getKeyCacheFile(key))) { return null; } - var data = fs.readFileSync(getKeyCacheFile(key), { encoding: "utf8" }); - - if (include_headers) { - return data; - } - - return data.split("\n", 2)[1]; + return fs.readFileSync(getKeyCacheFile(key), { encoding: "utf8" }); } /** @@ -263,7 +245,6 @@ function Weather(options) { */ function fetchWeatherForStation() { if (!isCacheExpiredMany([ - 'station', 'timestamp', 'raw', 'coordinates', 'elevation', 'text', 'temperature', 'dewpoint', 'windDirection', 'windSpeed', 'pressure', 'visibility', 'precipitation', 'humidity', @@ -277,13 +258,13 @@ function Weather(options) { that.config.weather_url.replace('{station}', that.config.station), { headers: { - 'user-agent': '@withaspark/weather User', + 'user-agent': that.config.user_agent, }, } ); response = JSON.parse(response.getBody("UTF-8")); - cache('timestamp', response.properties.timestamp); + cache('timestamp', moment(response.properties.timestamp).toISOString(true)); cache('raw', response.properties.rawMessage); cache( 'coordinates', @@ -319,7 +300,7 @@ function Weather(options) { that.config.alert_url.replace('{point}', that.getCoordinates()), { headers: { - 'user-agent': '@withaspark/weather User', + 'user-agent': that.config.user_agent, }, } ); @@ -338,7 +319,7 @@ function Weather(options) { * @returns {void} */ function calcSecondaryProperties() { - var now = that.now, + var now = that.getNow(), coordinates = that.getCoordinates().split(","); if (coordinates && coordinates.length == 2) {