Skip to content

Commit

Permalink
refactor!: finish parser migration
Browse files Browse the repository at this point in the history
Finally! :)

This removes all code related to the old parser.

#65
  • Loading branch information
LuanRT committed Jul 11, 2022
1 parent 947fd78 commit 68cb841
Show file tree
Hide file tree
Showing 285 changed files with 443 additions and 1,631 deletions.
36 changes: 18 additions & 18 deletions build/browser.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions build/browser.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions build/node.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions build/node.js.map

Large diffs are not rendered by default.

33 changes: 6 additions & 27 deletions lib/Innertube.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const NotificationsMenu = require('./parser/youtube/NotificationsMenu');
const YTMusic = require('./core/Music');
const FilterableFeed = require('./core/FilterableFeed');
const TabbedFeed = require('./core/TabbedFeed');
const Feed = require('./core/Feed');

const EventEmitter = require('events');
const { PassThrough } = BROWSER ? require('stream-browserify') : require('stream');
Expand All @@ -32,7 +33,6 @@ const {
generateRandomString
} = require('./utils/Utils');

const OldParser = require('./parser');
const Proto = require('./proto');

/** @namespace */
Expand All @@ -51,7 +51,7 @@ class Innertube {
* @param {string} [config.cookie]
* @param {boolean} [config.debug]
* @param {object} [config.proxy]
* @param {object} [config.http_ent]
* @param {object} [config.http_agent]
* @param {object} [config.https_agent]
*/
constructor(config) {
Expand Down Expand Up @@ -107,7 +107,6 @@ class Innertube {

/**
* Signs in to a google account.
*
* @param {object} credentials
* @param {string} credentials.access_token - Token used to sign in.
* @param {string} credentials.refresh_token - Token used to get a new access token.
Expand All @@ -133,7 +132,6 @@ class Innertube {

/**
* Signs out of an account.
*
* @returns {Promise.<{ success: boolean, status_code: number }>}
*/
async signOut() {
Expand All @@ -148,7 +146,6 @@ class Innertube {

/**
* Retrieves video info.
*
* @param {string} video_id
* @returns {Promise.<VideoInfo>}
*/
Expand All @@ -165,7 +162,6 @@ class Innertube {

/**
* Retrieves basic video info.
*
* @param {string} video_id
* @returns {Promise.<VideoInfo>}
*/
Expand All @@ -179,7 +175,6 @@ class Innertube {

/**
* Searches a given query.
*
* @param {string} query - search query.
* @param {object} [filters] - search filters.
* @param {string} [filters.upload_date] - filter videos by upload date, can be: any | last_hour | today | this_week | this_month | this_year
Expand Down Expand Up @@ -224,7 +219,6 @@ class Innertube {

/**
* Retrieves comments for a video.
*
* @param {string} video_id - the video id.
* @param {string} [sort_by] - can be: `TOP_COMMENTS` or `NEWEST_FIRST`.
* @returns {Promise.<Comments>}
Expand All @@ -243,7 +237,6 @@ class Innertube {

/**
* Retrieves YouTube's home feed (aka recommendations).
*
* @returns {Promise<FilterableFeed>}
*/
async getHomeFeed() {
Expand All @@ -253,7 +246,6 @@ class Innertube {

/**
* Returns the account's library.
*
* @returns {Promise.<Library>}
*/
async getLibrary() {
Expand All @@ -263,8 +255,7 @@ class Innertube {

/**
* Retrieves watch history.
* Which can also be achieved through {@link getLibrary()}.
*
* Which can also be achieved with {@link getLibrary()}.
* @returns {Promise.<History>}
*/
async getHistory() {
Expand All @@ -274,7 +265,6 @@ class Innertube {

/**
* Retrieves trending content.
*
* @returns {Promise<TabbedFeed>}
*/
async getTrending() {
Expand All @@ -284,23 +274,15 @@ class Innertube {

/**
* Retrieves subscriptions feed.
*
* @returns {Promise.<{ items: Array.<{ date: string, videos: object[] }>}>}
* @returns {Promise.<Feed>}
*/
async getSubscriptionsFeed() {
const response = await this.actions.browse('FEsubscriptions');

const subsfeed = new OldParser(this, response, {
client: 'YOUTUBE',
data_type: 'SUBSFEED'
}).parse();

return subsfeed;
return new Feed(this.actions, response.data);
}

/**
* Retrieves contents for a given channel. (WIP)
*
* Retrieves contents for a given channel.
* @param {string} id - channel id
* @returns {Promise<Channel>}
*/
Expand All @@ -312,7 +294,6 @@ class Innertube {

/**
* Retrieves notifications.
*
* @returns {Promise.<NotificationsMenu>}
*/
async getNotifications() {
Expand All @@ -322,7 +303,6 @@ class Innertube {

/**
* Retrieves unseen notifications count.
*
* @returns {Promise.<number>}
*/
async getUnseenNotificationsCount() {
Expand All @@ -332,7 +312,6 @@ class Innertube {

/**
* Retrieves the contents of a given playlist.
*
* @param {string} playlist_id - the id of the playlist.
* @returns {Promise.<Playlist>}
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/core/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Uuid = require('uuid');
const Proto = require('../proto');
const Utils = require('../utils/Utils');
const Constants = require('../utils/Constants');
const Parser = require('../parser/contents');
const Parser = require('../parser');

/** @namespace */
class Actions {
Expand Down
23 changes: 10 additions & 13 deletions lib/core/Feed.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

const ResultsParser = require('../parser/contents');
const Parser = require('../parser');
const { InnertubeError } = require('../utils/Utils');

// TODO: add a way subdivide into sections and return subfeeds?

class Feed {
#page;

/** @type {import('../parser/contents/classes/ContinuationItem')[]} */
/** @type {import('../parser/classes/ContinuationItem')[]} */
#continuation;

/** @type {import('../core/Actions')} */
Expand All @@ -20,20 +20,17 @@ class Feed {
if (data.on_response_received_actions || data.on_response_received_endpoints || already_parsed) {
this.#page = data;
} else {
this.#page = ResultsParser.parseResponse(data);
this.#page = Parser.parseResponse(data);
}

this.#memo =

this.#page.on_response_received_commands ?
this.#page.on_response_received_commands_memo :

this.#page.on_response_received_endpoints ?
this.#page.on_response_received_endpoints_memo :

this.#page.contents ?
this.#page.contents_memo :

this.#page.on_response_received_actions ?
this.#page.on_response_received_actions_memo : [];

Expand All @@ -44,7 +41,7 @@ class Feed {
* Get all videos on a given page via memo
*
* @param {Map<string, any[]>} memo
* @returns {Array<import('../parser/contents/classes/Video') | import('../parser/contents/classes/GridVideo') | import('../parser/contents/classes/CompactVideo') | import('../parser/contents/classes/PlaylistVideo') | import('../parser/contents/classes/PlaylistPanelVideo') | import('../parser/contents/classes/WatchCardCompactVideo')>}
* @returns {Array<import('../parser/classes/Video') | import('../parser/classes/GridVideo') | import('../parser/classes/CompactVideo') | import('../parser/classes/PlaylistVideo') | import('../parser/classes/PlaylistPanelVideo') | import('../parser/classes/WatchCardCompactVideo')>}
*/
static getVideosFromMemo(memo) {
const videos = memo.get('Video') || [];
Expand All @@ -68,7 +65,7 @@ class Feed {
* Get all playlists on a given page via memo
*
* @param {Map<string, any[]>} memo
* @returns {Array<import('../parser/contents/classes/Playlist') | import('../parser/contents/classes/GridPlaylist')>}
* @returns {Array<import('../parser/classes/Playlist') | import('../parser/classes/GridPlaylist')>}
*/
static getPlaylistsFromMemo(memo) {
const playlists = memo.get('Playlist') || [];
Expand All @@ -86,7 +83,7 @@ class Feed {
/**
* Get all the community posts in the feed
*
* @returns {import('../parser/contents/classes/BackstagePost')[] | import('../parser/contents/classes/Post')[]}
* @returns {import('../parser/classes/BackstagePost')[] | import('../parser/classes/Post')[]}
*/
get posts() {
return this.#memo.get('BackstagePost') || this.#memo.get('Post') || [];
Expand All @@ -106,7 +103,7 @@ class Feed {
/**
* Get all playlists in the feed
*
* @returns {Array<import('../parser/contents/classes/Playlist') | import('../parser/contents/classes/GridPlaylist')>}
* @returns {Array<import('../parser/classes/Playlist') | import('../parser/classes/GridPlaylist')>}
*/
get playlists() {
return Feed.getPlaylistsFromMemo(this.#memo);
Expand Down Expand Up @@ -135,9 +132,9 @@ class Feed {
* @returns {import('../parser/contents/Shelf')[] | import('../parser/contents/RichShelf')[] | import('../parser/contents/ReelShelf')[]}
*/
get shelves() {
const shelf = this.#page.contents_memo.get('Shelf') || [];
const rich_shelf = this.#page.contents_memo.get('RichShelf') || [];
const reel_shelf = this.#page.contents_memo.get('ReelShelf') || [];
const shelf = this.#memo.get('Shelf') || [];
const rich_shelf = this.#memo.get('RichShelf') || [];
const reel_shelf = this.#memo.get('ReelShelf') || [];

return [ ...shelf, ...rich_shelf, ...reel_shelf ];
}
Expand Down
6 changes: 3 additions & 3 deletions lib/core/FilterableFeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Feed = require('./Feed');

class FilterableFeed extends Feed {
/**
* @type {import('../parser/contents/ChipCloudChip')[]}
* @type {import('../parser/classes/ChipCloudChip')[]}
*/
#chips;

Expand All @@ -16,7 +16,7 @@ class FilterableFeed extends Feed {
/**
* Get filters for the feed
*
* @returns {import('../parser/contents/ChipCloudChip')[]}
* @returns {import('../parser/classes/ChipCloudChip')[]}
*/
get filter_chips() {
if (this.#chips) return this.#chips || [];
Expand All @@ -38,7 +38,7 @@ class FilterableFeed extends Feed {
/**
* Applies given filter and returns a new {@link Feed} object.
*
* @param {string | import('../parser/contents/classes/ChipCloudChip')} filter
* @param {string | import('../parser/classes/ChipCloudChip')} filter
* @returns {Promise.<Feed>}
*/
async getFilteredFeed(filter) {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/Music.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Parser = require('../parser/contents');
const Parser = require('../parser');
const Search = require('../parser/ytmusic/Search');
const HomeFeed = require('../parser/ytmusic/HomeFeed');
const Explore = require('../parser/ytmusic/Explore');
Expand Down
5 changes: 1 addition & 4 deletions lib/parser/contents/README.md → lib/parser/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Parser

Sanitizes and standardizes InnerTube responses while maintaining the integrity of the data. Also [drastically improves](https://github.com/LuanRT/YouTube.js/blob/main/lib/parser/youtube/Library.js#L41) how API calls are made and handled.

**Note:**
This will eventually replace the old parser.
Sanitizes and standardizes InnerTube responses while maintaining the integrity of the data. Also [drastically improves](https://github.com/LuanRT/YouTube.js/blob/main/lib/parser/youtube/Library.js#L44) how API calls are made and handled.

## API

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const Parser = require('..');
const NavigatableText = require('./NavigatableText');
const Thumbnail = require('./Thumbnail');
const Constants = require('../../../utils/Constants');
const Constants = require('../../utils/Constants');

class Author {
#nav_text;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const NavigationEndpoint = require('./NavigationEndpoint');
const Utils = require('../../../utils/Utils');
const Utils = require('../../utils/Utils');
const Text = require('./Text');

class ChildVideo {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const Parser = require('..');
const Text = require('./Text');
const Author = require('./Author');
const Utils = require('../../../utils/Utils');
const Utils = require('../../utils/Utils');
const Thumbnail = require('./Thumbnail');
const NavigationEndpoint = require('./NavigationEndpoint');

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Parser = require('..');
const Author = require('./Author');
const Thumbnail = require('./Thumbnail');
const NavigationEndpoint = require('./NavigationEndpoint');
const Utils = require('../../../utils/Utils');
const Utils = require('../../utils/Utils');
const Text = require('./Text');

class Movie {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const Parser = require('..');
const Text = require('./Text');
const Utils = require('../../../utils/Utils');
const Utils = require('../../utils/Utils');
const Thumbnail = require('./Thumbnail');
const NavigationEndpoint = require('./NavigationEndpoint');

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const NavigationEndpoint = require('./NavigationEndpoint');

class Notification {
type = 'Notification';

constructor(data) {
this.thumbnails = Thumbnail.fromResponse(data.thumbnail);
this.video_thumbnails = Thumbnail.fromResponse(data.videoThumbnail);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions lib/parser/classes/PlaylistInfoCardContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const Text = require('./Text');
const Thumbnail = require('./Thumbnail');
const NavigationEndpoint = require('./NavigationEndpoint');

class PlaylistInfoCardContent {
type = 'PlaylistInfoCardContent';

constructor(data) {
this.title = new Text(data.playlistTitle);
this.thumbnails = Thumbnail.fromResponse(data.thumbnail);
this.video_count = new Text(data.playlistVideoCount);
this.channel_name = new Text(data.channelName);
this.endpoint = new NavigationEndpoint(data.action);
}
}

module.exports = PlaylistInfoCardContent;
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Parser = require('..');
const Text = require('./Text');
const Thumbnail = require('./Thumbnail');
const NavigationEndpoint = require('./NavigationEndpoint');
const Utils = require('../../../utils/Utils');
const Utils = require('../../utils/Utils');

class PlaylistPanelVideo {
type = 'PlaylistPanelVideo';
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 68cb841

Please sign in to comment.