Browse Source

youtube: use fetch

master
vampirefrog 8 months ago
parent
commit
fe236ed147
  1. 99
      youtube.js

99
youtube.js

@ -1,5 +1,5 @@
const SongProvider = require('./songprovider');
const https = require('https');
const fetch = require('node-fetch');
class YouTube extends SongProvider {
constructor() {
@ -26,66 +26,55 @@ class YouTube extends SongProvider {
}
getInfo(url, cb) {
url = this.getCanonicalUrl(url);
https.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}
}, (r) => {
if(r.statusCode !== 200) {
cb('Could not get YouTube info for '+url+'. Status code: ' + r.statusCode);
} else {
var html = '';
r.on('data', chunk => html += chunk);
r.on('end', () => {
let match;
if(match = html.match(/var ytInitialPlayerResponse = (\{(.*?)\});/)) {
try {
var ytPlayerResponse = JSON.parse(match[1]);
} catch(e) {
cb('Could not parse JSON YouTube data for'+url);
return;
}
let canonicalUrl = this.getCanonicalUrl(url);
fetch(canonicalUrl)
.then((response) => { return response.text(); })
.then((html) => {
let match;
if(match = html.match(/var ytInitialPlayerResponse = (\{(.*?)\});/)) {
try {
var ytPlayerResponse = JSON.parse(match[1]);
} catch(e) {
cb('Could not parse JSON YouTube data for'+url);
return;
}
if(!ytPlayerResponse) {
cb('Unrecognized YouTube data for '+url);
return;
}
if(!ytPlayerResponse) {
cb('Unrecognized YouTube data for '+url);
return;
}
let width = null, height = null;
if(ytPlayerResponse.streamingData && ytPlayerResponse.streamingData.formats) {
for(let format of ytPlayerResponse.streamingData.formats) {
if(format.width > width || width === null) {
width = format.width;
height = format.height;
}
let width = null, height = null;
if(ytPlayerResponse.streamingData && ytPlayerResponse.streamingData.formats) {
for(let format of ytPlayerResponse.streamingData.formats) {
if(format.width > width || width === null) {
width = format.width;
height = format.height;
}
}
}
if(ytPlayerResponse.videoDetails) {
var ret = {
platform: 'youtube',
url: url,
title: ytPlayerResponse.videoDetails.title,
thumbnail: ytPlayerResponse.videoDetails.thumbnail && ytPlayerResponse.videoDetails.thumbnail.thumbnails && ytPlayerResponse.videoDetails.thumbnail.thumbnails[0].url || null,
duration: parseInt(ytPlayerResponse.videoDetails.lengthSeconds),
author: ytPlayerResponse.videoDetails.author,
author_url: 'https://www.youtube.com/channel/'+ytPlayerResponse.videoDetails.channelId,
description: ytPlayerResponse.videoDetails.shortDescription,
width: width,
height: height,
views: parseInt(ytPlayerResponse.videoDetails.viewCount),
};
cb(null, ret);
}
} else {
cb('Could not parse YouTube data for '+url);
if(ytPlayerResponse.videoDetails) {
var ret = {
platform: 'youtube',
url: url,
title: ytPlayerResponse.videoDetails.title,
thumbnail: ytPlayerResponse.videoDetails.thumbnail && ytPlayerResponse.videoDetails.thumbnail.thumbnails && ytPlayerResponse.videoDetails.thumbnail.thumbnails[0].url || null,
duration: parseInt(ytPlayerResponse.videoDetails.lengthSeconds),
author: ytPlayerResponse.videoDetails.author,
author_url: 'https://www.youtube.com/channel/'+ytPlayerResponse.videoDetails.channelId,
description: ytPlayerResponse.videoDetails.shortDescription,
width: width,
height: height,
views: parseInt(ytPlayerResponse.videoDetails.viewCount),
};
cb(null, ret);
}
});
}
}).on('error', (e) => {
cb('Error reading YouTube '+url+'. ')
});
} else {
cb('Could not parse YouTube data for '+url);
}
})
.catch((e) => { cb('Error reading YouTube Info: '+e); });
}
}

Loading…
Cancel
Save