From 5cfc0ebc3067c1202e5c9f855dea5782d646f933 Mon Sep 17 00:00:00 2001 From: redbrain <42494361+redbrain@users.noreply.github.com> Date: Tue, 24 Nov 2020 00:39:11 -0500 Subject: [PATCH] feat: warn when there is a ytdl-core update (#779) * Alert if ytdl-core is old * Add variables and pacify the linter ...hopefully. why is readability so important in a line that serves no more than utility? * Cause the linter to not be angry, again I'd really like to see all the checks pass for once. This code is absolutely readable, I dare you to say otherwise. * Change console.log to console.error for the linter * Get version from package.json As suggested by fent Co-authored-by: fent * Allow console.warn * Move the warn code to getInfo() * Add env variable support env var UPDATE if not set: check twice daily settings: 'always', 'never' * Make requested changes Specifically: change env var to YTDL_NO_UPDATE which takes true or false Drop code outside of functions (that's what I think you meant by that request. will it still run during long-uptime?) Clearer message, calc ms number. gonna drop the env definition into nock, hold on * Add env definition as requested * Fix the eslintrc * Indent properly I'm migrating to a new computer so I'm doing this all from GH web, it's a nightmare * Appease the linter once more I fixed this already, huh whuh? Useful commit comment goes here * Maybe this will be correct indentation? If not, please fix it for me. * Made changes you requested, and some others: Fixed the indents for eslint (hopefully, it seems to dislike me) Implemented request of not checking until the first 12 hrs are over Made it a function, and called it inside the getInfo cache thing directly above it. Also exported it so it could be used programatically (seems like a good idea?) Please lmk if there's anything else you'd like to see. * Fixing silly mistakes subtitle: my brain is big and smart coming to theaters near y'all * so that's why it wanted a user-agent * Make line shorter for the linter Co-authored-by: fent --- .eslintrc.json | 2 +- lib/info.js | 17 +++++++++++++++++ test/nock.js | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index 0b0a68bf..5da230ae 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -41,7 +41,7 @@ "dot-location": ["error", "property"], "dot-notation": "error", "eqeqeq": "error", - "no-console": "error", + "no-console": ["error", { "allow": ["warn"] }], "no-empty-function": "error", "no-floating-decimal": "error", "no-implied-eval": "error", diff --git a/lib/info.js b/lib/info.js index cbd2827e..986d115c 100644 --- a/lib/info.js +++ b/lib/info.js @@ -429,6 +429,21 @@ const getM3U8 = async(url, options) => { return formats; }; +// Check for updates. +const { version } = require('../package.json'); +let lastUpdateCheck = Date.now(); +const checkForUpdates = () => { + if (!process.env.YTDL_NO_UPDATE && Date.now() - lastUpdateCheck >= 1000 * 60 * 60 * 12) { + miniget('https://api.github.com/repos/fent/node-ytdl-core/releases/latest', { + headers: { 'User-Agent': 'ytdl-core' } + }).text().then(response => { + if (JSON.parse(response).tag_name !== version) { + console.warn('\x1b[33mWARNING:\x1B[0m ytdl-core is out of date! Update with "npm install ytdl-core@latest".'); + } + }); + } +}; + // Cache get info functions. // In case a user wants to get a video's info before downloading. @@ -440,6 +455,7 @@ for (let funcName of ['getBasicInfo', 'getInfo']) { */ const func = exports[funcName]; exports[funcName] = (link, options = {}) => { + checkForUpdates(); let id = urlUtils.getVideoID(link); const key = [funcName, id, options.lang].join('-'); return exports.cache.getOrSet(key, () => func(id, options)); @@ -452,3 +468,4 @@ exports.validateID = urlUtils.validateID; exports.validateURL = urlUtils.validateURL; exports.getURLVideoID = urlUtils.getURLVideoID; exports.getVideoID = urlUtils.getVideoID; +exports.checkForUpdates = checkForUpdates; diff --git a/test/nock.js b/test/nock.js index 790b3318..92ac32aa 100644 --- a/test/nock.js +++ b/test/nock.js @@ -9,6 +9,7 @@ const MANIFEST_HOST = 'https://manifest.googlevideo.com'; const M3U8_HOST = 'https://manifest.googlevideo.com'; const EMBED_PATH = '/embed/'; const INFO_PATH = '/get_video_info?'; +process.env.YTDL_NO_UPDATE = true; if (global.it) {