mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-19 20:41:17 +00:00
refactor!: rewrite Analytics to TypeScript (#122)
* refactor: migrate all analytics’ classes to TypeScript Also, add AnalyticsShortsCarouselCard and AnalyticsRoot.
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
import Video from './AnalyticsVideo';
|
||||
import { YTNode } from '../helpers';
|
||||
|
||||
class AnalyticsVodCarouselCard extends YTNode {
|
||||
static type = 'AnalyticsVodCarouselCard';
|
||||
|
||||
constructor(data) {
|
||||
super();
|
||||
this.title = data.title;
|
||||
this.videos = data.videoCarouselData.videos.map((video) => new Video(video));
|
||||
}
|
||||
}
|
||||
|
||||
export default AnalyticsVodCarouselCard;
|
||||
@@ -1,23 +0,0 @@
|
||||
import { YTNode } from '../helpers';
|
||||
|
||||
class DataModelSection extends YTNode {
|
||||
static type = 'DataModelSection';
|
||||
|
||||
constructor(data) {
|
||||
super();
|
||||
this.title = data.title;
|
||||
this.subtitle = data.subtitle;
|
||||
this.metric_value = data.metricValue;
|
||||
this.comparison_indicator = data.comparisonIndicator;
|
||||
|
||||
this.series_configuration = {
|
||||
line_series: {
|
||||
lines_data: data.seriesConfiguration.lineSeries.linesData,
|
||||
domain_axis: data.seriesConfiguration.lineSeries.domainAxis,
|
||||
measure_axis: data.seriesConfiguration.lineSeries.measureAxis
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default DataModelSection;
|
||||
@@ -3,11 +3,12 @@ import { YTNode } from '../helpers';
|
||||
|
||||
class Element extends YTNode {
|
||||
static type = 'Element';
|
||||
model;
|
||||
|
||||
constructor(data) {
|
||||
super();
|
||||
const type = data.newElement.type.componentType;
|
||||
return Parser.parse(type.model);
|
||||
this.model = Parser.parse(type.model);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,11 @@ class NavigationEndpoint extends YTNode {
|
||||
|
||||
constructor(data: any) {
|
||||
super();
|
||||
|
||||
// This is only present in Android nav endpoints
|
||||
if (Reflect.has(data || {}, 'innertubeCommand'))
|
||||
data = data.innertubeCommand;
|
||||
|
||||
const name = Object.keys(data || {})
|
||||
.find((item) =>
|
||||
item.endsWith('Endpoint') ||
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
import DataModelSection from './DataModelSection';
|
||||
import { YTNode } from '../helpers';
|
||||
import { YTNode } from '../../helpers';
|
||||
|
||||
class AnalyticsMainAppKeyMetrics extends YTNode {
|
||||
static type = 'AnalyticsMainAppKeyMetrics';
|
||||
|
||||
constructor(data) {
|
||||
period: string;
|
||||
sections: DataModelSection[];
|
||||
|
||||
constructor(data: any) {
|
||||
super();
|
||||
this.period = data.cardData.periodLabel;
|
||||
const metrics_data = data.cardData.sections[0].analyticsKeyMetricsData;
|
||||
this.sections = metrics_data.dataModel.sections.map((section) => new DataModelSection(section));
|
||||
this.sections = metrics_data.dataModel.sections.map((section: any) => new DataModelSection(section));
|
||||
}
|
||||
}
|
||||
|
||||
45
src/parser/classes/analytics/AnalyticsRoot.ts
Normal file
45
src/parser/classes/analytics/AnalyticsRoot.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { YTNode } from '../../helpers';
|
||||
|
||||
class AnalyticsRoot extends YTNode {
|
||||
static type = 'AnalyticsRoot';
|
||||
|
||||
title: string;
|
||||
selected_card_index_key: string;
|
||||
use_main_app_specs: boolean;
|
||||
|
||||
table_cards: {
|
||||
title: string;
|
||||
rows: {
|
||||
label: string;
|
||||
display_value: string;
|
||||
display_value_a11y: string;
|
||||
bar_ratio: number;
|
||||
bar_color: number;
|
||||
bar_opacity: number;
|
||||
}[];
|
||||
}[];
|
||||
|
||||
constructor(data: any) {
|
||||
super();
|
||||
const cards = data.analyticsTableCarouselData.data.tableCards;
|
||||
|
||||
this.title = data.analyticsTableCarouselData.carouselTitle;
|
||||
this.selected_card_index_key = data.analyticsTableCarouselData.selectedCardIndexKey;
|
||||
|
||||
this.table_cards = cards.map((card: any) => ({
|
||||
title: card.cardData.title,
|
||||
rows: card.cardData.rows.map((row: any) => ({
|
||||
label: row.label,
|
||||
display_value: row.displayValue,
|
||||
display_value_a11y: row.displayValueA11y,
|
||||
bar_ratio: row.barRatio,
|
||||
bar_color: row.barColor,
|
||||
bar_opacity: row.barOpacity
|
||||
}))
|
||||
}));
|
||||
|
||||
this.use_main_app_specs = data.analyticsTableCarouselData.useMainAppSpecs;
|
||||
}
|
||||
}
|
||||
|
||||
export default AnalyticsRoot;
|
||||
25
src/parser/classes/analytics/AnalyticsShortsCarouselCard.ts
Normal file
25
src/parser/classes/analytics/AnalyticsShortsCarouselCard.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { YTNode } from '../../helpers';
|
||||
import NavigationEndpoint from '../NavigationEndpoint';
|
||||
|
||||
class AnalyticsShortsCarouselCard extends YTNode {
|
||||
static type = 'AnalyticsShortsCarouselCard';
|
||||
|
||||
title: string;
|
||||
shorts: {
|
||||
description: string;
|
||||
thumbnail_url: string;
|
||||
endpoint: NavigationEndpoint;
|
||||
}[];
|
||||
|
||||
constructor(data: any) {
|
||||
super();
|
||||
this.title = data.title;
|
||||
this.shorts = data.shortsCarouselData.shorts.map((short: any) => ({
|
||||
description: short.shortsDescription,
|
||||
thumbnail_url: short.thumbnailUrl,
|
||||
endpoint: new NavigationEndpoint(short.videoEndpoint)
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
export default AnalyticsShortsCarouselCard;
|
||||
@@ -1,10 +1,19 @@
|
||||
import Thumbnail from './misc/Thumbnail';
|
||||
import { YTNode } from '../helpers';
|
||||
import Thumbnail from '../misc/Thumbnail';
|
||||
import { YTNode } from '../../helpers';
|
||||
|
||||
class AnalyticsVideo extends YTNode {
|
||||
static type = 'AnalyticsVideo';
|
||||
|
||||
constructor(data) {
|
||||
title: string;
|
||||
metadata: {
|
||||
views: string;
|
||||
published: string;
|
||||
thumbnails: Thumbnail[];
|
||||
duration: string;
|
||||
is_short: boolean;
|
||||
};
|
||||
|
||||
constructor(data: any) {
|
||||
super();
|
||||
this.title = data.videoTitle;
|
||||
|
||||
17
src/parser/classes/analytics/AnalyticsVodCarouselCard.ts
Normal file
17
src/parser/classes/analytics/AnalyticsVodCarouselCard.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import Video from './AnalyticsVideo';
|
||||
import { YTNode } from '../../helpers';
|
||||
|
||||
class AnalyticsVodCarouselCard extends YTNode {
|
||||
static type = 'AnalyticsVodCarouselCard';
|
||||
|
||||
title: string;
|
||||
videos: Video[];
|
||||
|
||||
constructor(data: any) {
|
||||
super();
|
||||
this.title = data.title;
|
||||
this.videos = data.videoCarouselData.videos.map((video: any) => new Video(video));
|
||||
}
|
||||
}
|
||||
|
||||
export default AnalyticsVodCarouselCard;
|
||||
@@ -1,9 +1,12 @@
|
||||
import { YTNode } from '../helpers';
|
||||
import { YTNode } from '../../helpers';
|
||||
|
||||
class CtaGoToCreatorStudio extends YTNode {
|
||||
static type = 'CtaGoToCreatorStudio';
|
||||
|
||||
constructor(data) {
|
||||
title: string;
|
||||
use_new_specs: boolean;
|
||||
|
||||
constructor(data: any) {
|
||||
super();
|
||||
this.title = data.buttonLabel;
|
||||
this.use_new_specs = data.useNewSpecs;
|
||||
72
src/parser/classes/analytics/DataModelSection.ts
Normal file
72
src/parser/classes/analytics/DataModelSection.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { YTNode } from '../../helpers';
|
||||
|
||||
class DataModelSection extends YTNode {
|
||||
static type = 'DataModelSection';
|
||||
|
||||
title: string;
|
||||
subtitle: string;
|
||||
metric_value: string;
|
||||
|
||||
comparison_indicator: {
|
||||
trend: string;
|
||||
};
|
||||
|
||||
series_configuration: {
|
||||
line_series: {
|
||||
lines_data: {
|
||||
x: number[];
|
||||
y: number[];
|
||||
style: {
|
||||
line_width: number;
|
||||
line_color: number;
|
||||
}
|
||||
}
|
||||
domain_axis: {
|
||||
tick_values: number[];
|
||||
custom_formatter: {
|
||||
labels: string[];
|
||||
}
|
||||
}
|
||||
measure_axis: {
|
||||
tick_values: number[];
|
||||
custom_formatter: {
|
||||
labels: string[];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
constructor(data: any) {
|
||||
super();
|
||||
|
||||
this.title = data.title;
|
||||
this.subtitle = data.subtitle;
|
||||
this.metric_value = data.metricValue;
|
||||
this.comparison_indicator = data.comparisonIndicator;
|
||||
|
||||
const line_series = data.seriesConfiguration.lineSeries;
|
||||
|
||||
this.series_configuration = {
|
||||
line_series: {
|
||||
lines_data: {
|
||||
x: line_series.linesData[0].x,
|
||||
y: line_series.linesData[0].y,
|
||||
style: {
|
||||
line_width: line_series.linesData[0].style.lineWidth,
|
||||
line_color: line_series.linesData[0].style.lineColor
|
||||
}
|
||||
},
|
||||
domain_axis: {
|
||||
tick_values: line_series.domainAxis.tickValues,
|
||||
custom_formatter: line_series.domainAxis.customFormatter
|
||||
},
|
||||
measure_axis: {
|
||||
tick_values: line_series.measureAxis.tickValues,
|
||||
custom_formatter: line_series.measureAxis.customFormatter
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default DataModelSection;
|
||||
Reference in New Issue
Block a user