Skip to content

Commit

Permalink
Merge pull request #49 from gatecrasher777/debug
Browse files Browse the repository at this point in the history
Fixed logged in status for video info requests
  • Loading branch information
gatecrasher777 authored Oct 18, 2022
2 parents 96b92fa + a9b7b53 commit e1c6d68
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 28 deletions.
9 changes: 7 additions & 2 deletions lib/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ class Channel extends Model {
order: 'new',
quantity: 60,
query: '',
period: 0,
};
this.updateOptions(options);
this.page = 0;
this.more = '';
this.periodExceeded = false;
}

// convert options into innertube parameters
Expand Down Expand Up @@ -96,6 +98,7 @@ class Channel extends Model {
}
if (w.viewCountText) video.views = ut.viewQ(w.viewCountText.simpleText);
if (ts > this.latest) this.latest = ts;
if (this.options.period && (now - ts) > this.options.period) this.periodExceeded = true;
video.author = this.author;
video.channelId = this.id;
video.channelThumb = this.thumbnail;
Expand Down Expand Up @@ -152,6 +155,7 @@ class Channel extends Model {
video.channelThumb = this.thumbnail;
video.country = this.country;
if (ts > this.latest) this.latest = ts;
if (this.options.period && (now - ts) > this.options.period) this.periodExceeded = true;
found.push(video);
} catch (e) { this.debug(e); }
} else if (m) {
Expand All @@ -173,6 +177,7 @@ class Channel extends Model {
this.page = 0;
this.data = [];
this.quantity = 0;
this.periodExceeded = false;
this.results = [];
let postData = {
context: this.session.context,
Expand Down Expand Up @@ -274,7 +279,7 @@ class Channel extends Model {
if (gi && gi.length) {
try {
this.process(gi, this.updated);
if (this.quantity < this.options.quantity) {
if (!this.periodExceeded && this.quantity < this.options.quantity) {
await this.continued();
} else {
this.status = 'OK';
Expand Down Expand Up @@ -321,7 +326,7 @@ class Channel extends Model {
try {
let gi = this.data[this.page].onResponseReceivedActions[0].appendContinuationItemsAction.continuationItems;
this.process(gi, this.updated);
if ((this.quantity < this.options.quantity) && (last < this.quantity)) {
if (!this.periodExceeded && this.quantity < this.options.quantity && last < this.quantity) {
await this.continued();
}
} catch (e) { this.debug(e); }
Expand Down
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Model {
this.sapisid = '';
if (this.cookie.length) {
this.common.headers.cookie = cookie;
this.sapisid = ut.pinch(this.cookie, 'SAPISID=', ';', 0, 0, '', false);
this.sapisid = ut.pinch(this.cookie, '__Secure-3PAPISID=', ';', 0, 0, '', false);
}
this.debugOn = false;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class Session extends Model {
this.context.client && !this.context.client.visitorData) {
this.context.client.visitorData = j.SBOX_SETTINGS.VISITOR_DATA;
}
this.context.user = {};
this.context.user = {lockedSafetyMode: false};
this.context.request = {
useSsl: true,
};
if (this.context && this.context.client) this.context.client.hl = 'en';
this.player = new Player(this);
if (this.playerUrl.length) await this.player.fetch();
Expand Down
65 changes: 42 additions & 23 deletions lib/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Comment = require('./comment');

// video object retrieves video information and media, allows smart or direct downloads
class Video extends Model {

// construct the video, requires an active session object and
// options: {id[,published,path,filename,container,videoQuality,audioQuality,mediaBitrate]}
constructor(session, options) {
Expand Down Expand Up @@ -328,6 +329,7 @@ class Video extends Model {
// fetch request
async fetch(options) {
if (options) this.updateOptions(options);
this.session.context.client.originalUrl = `https://www.youtube.com/watch?v=${this.options.id}`;
let postData = ut.js({
playbackContext: {
contentPlaybackContext: {
Expand All @@ -344,6 +346,8 @@ class Video extends Model {
},
context: this.session.context,
videoId: this.options.id,
racyCheckOk: false,
contentCheckOk: false
});
let hdrs = {
'content-type': 'application/json',
Expand All @@ -357,9 +361,9 @@ class Video extends Model {
};
if (this.session.loggedIn) hdrs.authorization = this.session.player.idhashFn(this.sapisid);
let body = await this.httpsPost(
`https://www.youtube.com/youtubei/v1/player?key=${this.session.key}`,
`https://www.youtube.com/youtubei/v1/player?key=${this.session.key}&prettyPrint=false`,
{
path: `/youtubei/v1/player?key=${this.session.key}`,
path: `/youtubei/v1/player?key=${this.session.key}&prettyPrint=false`,
headers: hdrs,
},
postData,
Expand Down Expand Up @@ -614,7 +618,6 @@ class Video extends Model {
this.child.send({ msg: 'retry' });
} else {
this.child.send({ msg: 'done' });
this.cancelled = false;
resolve({
success: false,
fail: msg.fail,
Expand Down Expand Up @@ -682,7 +685,8 @@ class Video extends Model {
if (this.options.container === 'mkv') okay = true;
}
}
if (okay && !this.cancelled) {
if (this.cancelled) return false;
if (okay) {
let result = await this.downloadInstance(
dl.v,
dl.a,
Expand Down Expand Up @@ -808,6 +812,7 @@ class Video extends Model {
}

if (dl.mv || dl.ma) {

dl.vchk = () => {
if (vformat >= 0) {
return false;
Expand Down Expand Up @@ -836,6 +841,7 @@ class Video extends Model {
}
}
};

let result = await this.trydl(dl, vonly, aonly);
return result;
} else {
Expand Down Expand Up @@ -864,14 +870,16 @@ class Video extends Model {
}
}
if (this.options.videoQuality === 'none') aonly = true;
let success = await this.downloadAttempt(vonly, aonly, vformat, aformat);
if (success) {
this.downloaded = ut.now();
} else if (!this.cancelled) {
// try video onloy
if (aformat < 0 && !vonly && !aonly && vformat < 0) {
success = await this.downloadAttempt(true, false, vformat, aformat);
if (success) this.downloaded = ut.now();
if (!this.cancelled) {
let success = await this.downloadAttempt(vonly, aonly, vformat, aformat);
if (success) {
this.downloaded = ut.now();
} else if (!this.cancelled) {
// try video only
if (aformat < 0 && !vonly && !aonly && vformat < 0) {
success = await this.downloadAttempt(true, false, vformat, aformat);
if (success) this.downloaded = ut.now();
}
}
}
} catch (e) { this.debug(e); }
Expand Down Expand Up @@ -993,14 +1001,7 @@ class Video extends Model {
}
}

// initiate comments request
async fetchComments(options, proceed = true) {
this.commentOptions = { ...this.commentOptions, ...options };
this.commentData = [];
this.commentMore = '';
this.commentPage = 0;
this.quantity = 0;
this.results = [];
async fetchNext() {
let postData = ut.js({
playbackContext: {
contentPlaybackContext: {
Expand Down Expand Up @@ -1031,6 +1032,18 @@ class Video extends Model {
postData,
true,
);
return body;
}

// initiate comments request
async fetchComments(options, proceed = true) {
this.commentOptions = { ...this.commentOptions, ...options };
this.commentData = [];
this.commentMore = '';
this.commentPage = 0;
this.quantity = 0;
this.results = [];
let body = await this.fetchNext();
if (body) {
this.commentData.push(ut.jp(body));
try {
Expand Down Expand Up @@ -1231,6 +1244,7 @@ class Video extends Model {
this.debug(e);
}
}

// true if video has comments, false if no comments or comments are disabled
async hasComments() {
try {
Expand All @@ -1247,14 +1261,19 @@ class Video extends Model {
try {
this.downloaded = 0;
this.updateOptions(options);
if (!this.hasMedia) await this.fetch();
if (this.hasMedia) await this.dl(this.options.videoFormat, this.options.audioFormat);
if (this.hasCaptions && this.options.subtitles !== 'none') await this.subtitles();
if (!this.cancelled && !this.hasMedia) await this.fetch();
if (!this.cancelled && this.hasMedia) await this.dl(this.options.videoFormat, this.options.audioFormat);
if (!this.cancelled && this.hasCaptions && this.options.subtitles !== 'none') await this.subtitles();
} catch (e) {
this.debug(e);
}
}

async nextHas(text) {
let body = await this.fetchNext();
return body.includes(text);
}

updateOptions(newOptions) {
this.options = { ...this.options, ...newOptions };
if (['mp3', 'flac'].includes(this.options.container)) this.options.videoQuality = 'none';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ytcog",
"version": "2.4.2",
"version": "2.4.3",
"description": "YouTube innertube class library for node-js; session, player, searches, channels, playlists, videos and downloads.",
"main": "./lib/index.js",
"repository": {
Expand Down

0 comments on commit e1c6d68

Please sign in to comment.