Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
Fix non-json outputs
Browse files Browse the repository at this point in the history
Sometimes youtube-dl won't return JSON. It can return a simple string (when using the "-g" or "--get-url" flags for instance). This commit, with the minimum amount of modifications possible, tries to handle this case.

Comes with a test
  • Loading branch information
Coriou committed May 22, 2019
1 parent 3092226 commit cfff104
Show file tree
Hide file tree
Showing 3 changed files with 550 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ ytdl.exec = function exec(url, args, options, callback) {
* @returns {Object}
*/
function parseInfo(data) {
// Youtube-dl might return just an url as a string when using the "-g" or "--get-url" flag
if (typeof data === "string")
if (data.startsWith("http"))
data = JSON.stringify({url: data})

const info = JSON.parse(data);

// Add and process some entries to keep backwards compatibility
Expand Down Expand Up @@ -316,6 +321,13 @@ ytdl.getInfo = function getInfo(url, args, options, callback) {
if (err) return callback(err);
let info;

// If using the "-g" or "--get-url" flag youtube-dl will return just a string (the URL to the video) which messes up the parsing
// This fixes this behaviour
if (args.indexOf('-g') > -1 || args.indexOf('--get-url') > -1 )
if (Array.isArray(data))
if(data.length >= 2)
data.splice(0, 1)

try {
info = data.map(parseInfo);
} catch (err) {
Expand Down
Loading

0 comments on commit cfff104

Please sign in to comment.