mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-07-03 09:35:05 +00:00
refactor: rewrite Innertube#getNotifications() to use the new parser
This commit is contained in:
@@ -14,6 +14,7 @@ const Playlist = require('./parser/youtube/Playlist');
|
||||
const Library = require('./parser/youtube/Library');
|
||||
const History = require('./parser/youtube/History');
|
||||
const Comments = require('./parser/youtube/Comments');
|
||||
const NotificationsMenu = require('./parser/youtube/NotificationsMenu');
|
||||
|
||||
const YTMusic = require('./core/Music');
|
||||
const FilterableFeed = require('./core/FilterableFeed');
|
||||
@@ -312,17 +313,11 @@ class Innertube {
|
||||
/**
|
||||
* Retrieves notifications.
|
||||
*
|
||||
* @returns {Promise.<{ items: Array.<{ title: string, sent_time: string, channel_name: string, channel_thumbnail: object, video_thumbnail: object, video_url: string, read: boolean, notification_id: string }>}>}
|
||||
* @returns {Promise.<NotificationsMenu>}
|
||||
*/
|
||||
async getNotifications() {
|
||||
const response = await this.actions.notifications('get_notification_menu');
|
||||
|
||||
const notifications = new OldParser(this, response.data, {
|
||||
client: 'YOUTUBE',
|
||||
data_type: 'NOTIFICATIONS'
|
||||
}).parse();
|
||||
|
||||
return notifications;
|
||||
return new NotificationsMenu(this.actions, response.data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
24
lib/parser/contents/classes/Notification.js
Normal file
24
lib/parser/contents/classes/Notification.js
Normal file
@@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
|
||||
const Parser = require('..');
|
||||
const Text = require('./Text');
|
||||
const Thumbnail = require('./Thumbnail');
|
||||
const NavigationEndpoint = require('./NavigationEndpoint');
|
||||
|
||||
class Notification {
|
||||
type = 'Notification';
|
||||
|
||||
constructor(data) {
|
||||
this.thumbnails = Thumbnail.fromResponse(data.thumbnail);
|
||||
this.video_thumbnails = Thumbnail.fromResponse(data.videoThumbnail);
|
||||
this.short_message = new Text(data.shortMessage);
|
||||
this.sent_time = new Text(data.sentTimeText);
|
||||
this.notification_id = data.notificationId;
|
||||
this.endpoint = new NavigationEndpoint(data.navigationEndpoint);
|
||||
this.record_click_endpoint = new NavigationEndpoint(data.recordClickEndpoint);
|
||||
this.menu = Parser.parse(data.contextualMenu);
|
||||
this.read = data.read;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Notification;
|
||||
@@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
const Parser = require('../..');
|
||||
|
||||
class AppendContinuationItemsAction {
|
||||
type = 'AppendContinuationItemsAction';
|
||||
|
||||
constructor(data) {
|
||||
this.items = Parser.parse(data.continuationItems);
|
||||
this.target = data.target;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AppendContinuationItemsAction;
|
||||
14
lib/parser/contents/classes/actions/OpenPopupAction.js
Normal file
14
lib/parser/contents/classes/actions/OpenPopupAction.js
Normal file
@@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
const Parser = require('../..');
|
||||
|
||||
class OpenPopupAction {
|
||||
type = 'OpenPopupAction';
|
||||
|
||||
constructor(data) {
|
||||
this.popup = Parser.parse(data.popup);
|
||||
this.popup_type = data.popupType;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = OpenPopupAction;
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const Parser = require('..');
|
||||
const Parser = require('../..');
|
||||
|
||||
class Menu {
|
||||
type = 'Menu';
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const Button = require('./Button');
|
||||
const Button = require('../Button');
|
||||
|
||||
class MenuNavigationItem extends Button {
|
||||
type = 'MenuNavigationItem';
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const Button = require('./Button');
|
||||
const Button = require('../Button');
|
||||
|
||||
class MenuServiceItem extends Button {
|
||||
type = 'MenuServiceItem';
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const NavigationEndpoint = require('./NavigationEndpoint');
|
||||
const NavigationEndpoint = require('../NavigationEndpoint');
|
||||
|
||||
class MenuServiceItemDownload {
|
||||
type = 'MenuServiceItemDownload';
|
||||
15
lib/parser/contents/classes/menus/MultiPageMenu.js
Normal file
15
lib/parser/contents/classes/menus/MultiPageMenu.js
Normal file
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
const Parser = require('../..');
|
||||
|
||||
class MultiPageMenu {
|
||||
type = 'MultiPageMenu';
|
||||
|
||||
constructor(data) {
|
||||
this.header = Parser.parse(data.header);
|
||||
this.sections = Parser.parse(data.sections);
|
||||
this.style = data.style;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MultiPageMenu;
|
||||
@@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
const Parser = require('../..');
|
||||
|
||||
class MultiPageMenuNotificationSection {
|
||||
type = 'MultiPageMenuNotificationSection';
|
||||
|
||||
constructor(data) {
|
||||
this.items = Parser.parse(data.items);
|
||||
}
|
||||
|
||||
get contents() {
|
||||
return this.items;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MultiPageMenuNotificationSection;
|
||||
15
lib/parser/contents/classes/menus/SimpleMenuHeader.js
Normal file
15
lib/parser/contents/classes/menus/SimpleMenuHeader.js
Normal file
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
const Parser = require('../..');
|
||||
const Text = require('../Text');
|
||||
|
||||
class SimpleMenuHeader {
|
||||
type = 'SimpleMenuHeader';
|
||||
|
||||
constructor(data) {
|
||||
this.title = new Text(data.title);
|
||||
this.buttons = Parser.parse(data.buttons);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SimpleMenuHeader;
|
||||
@@ -121,8 +121,15 @@ class Parser {
|
||||
const on_response_received_commands = data.onResponseReceivedCommands ? Parser.parseRR(data.onResponseReceivedCommands) : null;
|
||||
const on_response_received_commands_memo = Parser.#memo;
|
||||
this.#clearMemo();
|
||||
|
||||
|
||||
this.#createMemo();
|
||||
const actions = data.actions ? Parser.parseActions(data.actions) : null;
|
||||
const actions_memo = Parser.#memo;
|
||||
this.#clearMemo();
|
||||
|
||||
return {
|
||||
actions,
|
||||
actions_memo,
|
||||
contents,
|
||||
contents_memo,
|
||||
on_response_received_actions,
|
||||
@@ -135,7 +142,6 @@ class Parser {
|
||||
continuation: data.continuation ? Parser.parseC(data.continuation) : null,
|
||||
/** @type {*} */
|
||||
continuation_contents: data.continuationContents ? Parser.parseLC(data.continuationContents) : null,
|
||||
actions: data.actions && Parser.parseActions(data.actions),
|
||||
metadata: Parser.parse(data.metadata),
|
||||
header: Parser.parse(data.header),
|
||||
/** @type {import('./classes/PlayerMicroformat')} */
|
||||
|
||||
File diff suppressed because one or more lines are too long
46
lib/parser/youtube/NotificationsMenu.js
Normal file
46
lib/parser/youtube/NotificationsMenu.js
Normal file
@@ -0,0 +1,46 @@
|
||||
'use strict';
|
||||
|
||||
const Parser = require('../contents');
|
||||
const { InnertubeError } = require('../../utils/Utils');
|
||||
|
||||
class NotificationsMenu {
|
||||
#page;
|
||||
#actions;
|
||||
#continuation;
|
||||
|
||||
/**
|
||||
* @param {import('../../core/Actions')} actions
|
||||
* @param {object} response - API response.
|
||||
*/
|
||||
constructor(actions, response) {
|
||||
this.#actions = actions;
|
||||
this.#page = Parser.parseResponse(response);
|
||||
|
||||
/** @type {import('./contents/classes/menus/SimpleMenuHeader')} */
|
||||
this.header = this.#page.actions_memo.get('SimpleMenuHeader')?.[0] || null;
|
||||
|
||||
/** @type {import('./contents/classes/Notification')} */
|
||||
this.contents = this.#page.actions_memo.get('Notification');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves next batch of notifications.
|
||||
* @returns {Promise.<NotificationsMenu>}
|
||||
*/
|
||||
async getContinuation() {
|
||||
const continuation = this.#page.actions_memo.get('ContinuationItem')?.[0];
|
||||
|
||||
if (!continuation)
|
||||
throw new InnertubeError('Continuation not found');
|
||||
|
||||
const response = await continuation.endpoint.callTest(this.#actions, { parse: false });
|
||||
|
||||
return new NotificationsMenu(this.#actions, response.data);
|
||||
}
|
||||
|
||||
get page() {
|
||||
return this.#page;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = NotificationsMenu;
|
||||
Reference in New Issue
Block a user