mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-24 15:21:54 +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:
18
src/parser/classes/analytics/AnalyticsMainAppKeyMetrics.ts
Normal file
18
src/parser/classes/analytics/AnalyticsMainAppKeyMetrics.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import DataModelSection from './DataModelSection';
|
||||
import { YTNode } from '../../helpers';
|
||||
|
||||
class AnalyticsMainAppKeyMetrics extends YTNode {
|
||||
static type = 'AnalyticsMainAppKeyMetrics';
|
||||
|
||||
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: any) => new DataModelSection(section));
|
||||
}
|
||||
}
|
||||
|
||||
export default AnalyticsMainAppKeyMetrics;
|
||||
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;
|
||||
30
src/parser/classes/analytics/AnalyticsVideo.ts
Normal file
30
src/parser/classes/analytics/AnalyticsVideo.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import Thumbnail from '../misc/Thumbnail';
|
||||
import { YTNode } from '../../helpers';
|
||||
|
||||
class AnalyticsVideo extends YTNode {
|
||||
static type = 'AnalyticsVideo';
|
||||
|
||||
title: string;
|
||||
metadata: {
|
||||
views: string;
|
||||
published: string;
|
||||
thumbnails: Thumbnail[];
|
||||
duration: string;
|
||||
is_short: boolean;
|
||||
};
|
||||
|
||||
constructor(data: any) {
|
||||
super();
|
||||
this.title = data.videoTitle;
|
||||
|
||||
this.metadata = {
|
||||
views: data.videoDescription.split('·')[0].trim(),
|
||||
published: data.videoDescription.split('·')[1].trim(),
|
||||
thumbnails: Thumbnail.fromResponse(data.thumbnailDetails),
|
||||
duration: data.formattedLength,
|
||||
is_short: data.isShort
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default AnalyticsVideo;
|
||||
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;
|
||||
17
src/parser/classes/analytics/CtaGoToCreatorStudio.ts
Normal file
17
src/parser/classes/analytics/CtaGoToCreatorStudio.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { YTNode } from '../../helpers';
|
||||
|
||||
class CtaGoToCreatorStudio extends YTNode {
|
||||
static type = 'CtaGoToCreatorStudio';
|
||||
|
||||
title: string;
|
||||
use_new_specs: boolean;
|
||||
|
||||
constructor(data: any) {
|
||||
super();
|
||||
this.title = data.buttonLabel;
|
||||
this.use_new_specs = data.useNewSpecs;
|
||||
// Is this even useful?
|
||||
}
|
||||
}
|
||||
|
||||
export default CtaGoToCreatorStudio;
|
||||
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