From 28e42a3f11688735a6a33840aed07a4e1f7fa285 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Tue, 5 Oct 2021 15:12:18 -0300 Subject: [PATCH] feat: add possibility to fetch video notifications --- lib/Actions.js | 27 +++++++++++++++++++++++++-- lib/Innertube.js | 23 +++++++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/Actions.js b/lib/Actions.js index b9693c6c..cb3dd3cf 100644 --- a/lib/Actions.js +++ b/lib/Actions.js @@ -130,7 +130,7 @@ async function commentVideo(session, video_id, text) { createCommentParams: Utils.encodeId(video_id) }; - const response = await axios.post(Constants.urls.YT_BASE_URL + '/youtubei/v1/comment/create_comment?key=' + session.key, JSON.stringify(data), Constants.innertube_request_opts({ session, video_id, data })).catch((error) => error); + const response = await axios.post(Constants.urls.YT_BASE_URL + '/youtubei/v1/comment/create_comment?key=' + session.key, JSON.stringify(data), Constants.innertube_request_opts({ session, id: video_id, data })).catch((error) => error); if (response instanceof Error) { return { success: false, @@ -145,4 +145,27 @@ async function commentVideo(session, video_id, text) { } } -module.exports = { subscribe, unsubscribe, likeVideo, dislikeVideo, removeLike, commentVideo }; \ No newline at end of file +async function getNotifications(session) { + if (!session.logged_in) throw new Error('You must be logged in to fetch notifications'); + let data = { + context: session.context, + notificationsMenuRequestType: 'NOTIFICATIONS_MENU_REQUEST_TYPE_INBOX' + }; + + const response = await axios.post(Constants.urls.YT_BASE_URL + '/youtubei/v1/notification/get_notification_menu?key=' + session.key, JSON.stringify(data), Constants.innertube_request_opts({ session, data })).catch((error) => error); + if (response instanceof Error) { + return { + success: false, + status_code: response.response.status, + message: response.message + }; + } else { + return { + success: true, + status_code: response.status, + data: response.data + }; + } +} + +module.exports = { subscribe, unsubscribe, likeVideo, dislikeVideo, removeLike, commentVideo, getNotifications }; \ No newline at end of file diff --git a/lib/Innertube.js b/lib/Innertube.js index def25073..99888ec7 100644 --- a/lib/Innertube.js +++ b/lib/Innertube.js @@ -103,7 +103,26 @@ class Innertube { return video_data; } - + + async getNotifications() { + const response = await Actions.getNotifications(this); + const contents = response.data.actions[0].openPopupAction.popup.multiPageMenuRenderer.sections[0].multiPageMenuNotificationSectionRenderer.items; + return contents.map((notification) => { + if (!notification.notificationRenderer) return; + notification = notification.notificationRenderer; + return { + title: notification.shortMessage.simpleText, + sent_time: notification.sentTimeText.simpleText, + channel_name: notification.contextualMenu.menuRenderer.items[1].menuServiceItemRenderer.text.runs[1].text, + channel_thumbnail: notification.thumbnail.thumbnails[0], + video_thumbnail: notification.videoThumbnail.thumbnails[0], + video_url: 'https://youtu.be/' + notification.navigationEndpoint.watchEndpoint.videoId, + read: notification.read, + notification_id: notification.notificationId, + }; + }).filter((notification_block) => notification_block); + } + async requestVideoInfo(id, desktop) { let response; if (!desktop) { @@ -114,7 +133,7 @@ class Innertube { if (response instanceof Error) throw new Error('Could not retrieve watch page info: ' + response.message); return response.data; } - + download(id, options = {}) { options.quality = options.quality || '360p'; options.type = options.type || 'both';