Skip to content

Commit

Permalink
feat: warn when there is a ytdl-core update (#779)
Browse files Browse the repository at this point in the history
* 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 <fentbox@gmail.com>

* 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 <fentbox@gmail.com>
  • Loading branch information
redbrain and fent committed Nov 24, 2020
1 parent 4012a15 commit 5cfc0eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .eslintrc.json
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions lib/info.js
Expand Up @@ -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.
Expand All @@ -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));
Expand All @@ -452,3 +468,4 @@ exports.validateID = urlUtils.validateID;
exports.validateURL = urlUtils.validateURL;
exports.getURLVideoID = urlUtils.getURLVideoID;
exports.getVideoID = urlUtils.getVideoID;
exports.checkForUpdates = checkForUpdates;
1 change: 1 addition & 0 deletions test/nock.js
Expand Up @@ -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) {
Expand Down

0 comments on commit 5cfc0eb

Please sign in to comment.