refactor: migrate parsers to TS (#133)

* dev: finish top-level parsers TS migration

* dev: migrate menu renderers to TS

* chore: fix ts errors

* dev: finish ts migration 🎉
This commit is contained in:
LuanRT
2022-08-20 03:18:17 -03:00
committed by GitHub
parent b101a39d30
commit 34281e2445
206 changed files with 1747 additions and 608 deletions

106
src/parser/classes/Video.ts Normal file
View File

@@ -0,0 +1,106 @@
import Parser from '../index';
import Text from './misc/Text';
import Author from './misc/Author';
import Thumbnail from './misc/Thumbnail';
import NavigationEndpoint from './NavigationEndpoint';
import { timeToSeconds } from '../../utils/Utils';
import { YTNode } from '../helpers';
class Video extends YTNode {
static type = 'Video';
id: string;
title: Text;
description_snippet: Text | null;
snippets: {
text: Text;
hover_text: Text;
}[];
thumbnails: Thumbnail[];
thumbnail_overlays;
rich_thumbnail;
author: Author;
endpoint: NavigationEndpoint;
published: Text;
view_count: Text;
short_view_count: Text;
upcoming;
duration: {
text: string;
seconds: number;
};
show_action_menu: boolean;
is_watched: boolean;
menu;
constructor(data: any) {
super();
const overlay_time_status = data.thumbnailOverlays
.find((overlay: any) => overlay.thumbnailOverlayTimeStatusRenderer)
?.thumbnailOverlayTimeStatusRenderer.text || 'N/A';
this.id = data.videoId;
this.title = new Text(data.title);
this.description_snippet = data.descriptionSnippet ? new Text(data.descriptionSnippet) : null;
this.snippets = data.detailedMetadataSnippets?.map((snippet: any) => ({
text: new Text(snippet.snippetText),
hover_text: new Text(snippet.snippetHoverText)
})) || [];
this.thumbnails = Thumbnail.fromResponse(data.thumbnail);
this.thumbnail_overlays = Parser.parse(data.thumbnailOverlays);
this.rich_thumbnail = data.richThumbnail && Parser.parse(data.richThumbnail);
this.author = new Author(data.ownerText, data.ownerBadges, data.channelThumbnailSupportedRenderers?.channelThumbnailWithLinkRenderer?.thumbnail);
this.endpoint = new NavigationEndpoint(data.navigationEndpoint);
this.published = new Text(data.publishedTimeText);
this.view_count = new Text(data.viewCountText);
this.short_view_count = new Text(data.shortViewCountText);
const upcoming = data.upcomingEventData && Number(`${data.upcomingEventData.startTime}000`);
if (upcoming) {
this.upcoming = new Date(upcoming);
}
this.duration = {
text: data.lengthText ? new Text(data.lengthText).text : new Text(overlay_time_status).text,
seconds: timeToSeconds(data.lengthText ? new Text(data.lengthText).text : new Text(overlay_time_status).text)
};
this.show_action_menu = data.showActionMenu;
this.is_watched = data.isWatched || false;
this.menu = Parser.parse(data.menu);
}
get description(): string {
if (this.snippets.length > 0) {
return this.snippets.map((snip) => snip.text.toString()).join('');
}
return this.description_snippet?.toString() || '';
}
/*
Get is_live() {
return this.badges.some((badge) => badge.style === 'BADGE_STYLE_TYPE_LIVE_NOW');
}
*/
get is_upcoming(): boolean | undefined {
return this.upcoming && this.upcoming > new Date();
}
/*
Get has_captions() {
return this.badges.some((badge) => badge.label === 'CC');
}*/
get best_thumbnail(): Thumbnail | undefined{
return this.thumbnails[0];
}
}
export default Video;