Skip to content

Commit

Permalink
feat: extract channel error alert
Browse files Browse the repository at this point in the history
  • Loading branch information
LuanRT committed Jan 27, 2023
1 parent 50ef712 commit 0b99180
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/parser/classes/Alert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Text from './misc/Text';
import { YTNode } from '../helpers';

class Alert extends YTNode {
static type = 'Alert';

text: Text;
alert_type: string;

constructor(data: any) {
super();
this.text = new Text(data.text);
this.alert_type = data.type;
}
}

export default Alert;
2 changes: 2 additions & 0 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type Message from './classes/Message';
import type LiveChatParticipantsList from './classes/LiveChatParticipantsList';
import type LiveChatHeader from './classes/LiveChatHeader';
import type LiveChatItemList from './classes/LiveChatItemList';
import type Alert from './classes/Alert';

import MusicMultiSelectMenuItem from './classes/menus/MusicMultiSelectMenuItem';
import Format from './classes/misc/Format';
Expand Down Expand Up @@ -137,6 +138,7 @@ export default class Parser {
metadata: Parser.parse(data.metadata),
microformat: data.microformat ? Parser.parseItem(data.microformat) : null,
overlay: Parser.parseItem(data.overlay),
alerts: Parser.parseArray<Alert>(data.alerts),
refinements: data.refinements || null,
estimated_results: data.estimatedResults ? parseInt(data.estimatedResults) : null,
player_overlays: Parser.parse(data.playerOverlays),
Expand Down
2 changes: 2 additions & 0 deletions src/parser/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { default as AccountItemSectionHeader } from './classes/AccountItemSectio
import { default as AccountSectionList } from './classes/AccountSectionList';
import { default as AppendContinuationItemsAction } from './classes/actions/AppendContinuationItemsAction';
import { default as OpenPopupAction } from './classes/actions/OpenPopupAction';
import { default as Alert } from './classes/Alert';
import { default as AnalyticsMainAppKeyMetrics } from './classes/analytics/AnalyticsMainAppKeyMetrics';
import { default as AnalyticsRoot } from './classes/analytics/AnalyticsRoot';
import { default as AnalyticsShortsCarouselCard } from './classes/analytics/AnalyticsShortsCarouselCard';
Expand Down Expand Up @@ -326,6 +327,7 @@ export const YTNodes = {
AccountSectionList,
AppendContinuationItemsAction,
OpenPopupAction,
Alert,
AnalyticsMainAppKeyMetrics,
AnalyticsRoot,
AnalyticsShortsCarouselCard,
Expand Down
9 changes: 8 additions & 1 deletion src/parser/youtube/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import FeedFilterChipBar from '../classes/FeedFilterChipBar';
import ChannelSubMenu from '../classes/ChannelSubMenu';
import SortFilterSubMenu from '../classes/SortFilterSubMenu';

import { InnertubeError } from '../../utils/Utils';
import { ChannelError, InnertubeError } from '../../utils/Utils';

import type { AppendContinuationItemsAction, ReloadContinuationItemsCommand } from '..';

Expand All @@ -36,6 +36,13 @@ export default class Channel extends TabbedFeed {
const metadata = this.page.metadata?.item().as(ChannelMetadata);
const microformat = this.page.microformat?.as(MicroformatData);

if (this.page.alerts) {
const alert = this.page.alerts.first();
if (alert?.alert_type === 'ERROR') {
throw new ChannelError(alert.text.toString());
}
}

if (!metadata && !this.page.contents)
throw new InnertubeError('Invalid channel', this);

Expand Down
1 change: 1 addition & 0 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class MissingParamError extends InnertubeError { }
export class OAuthError extends InnertubeError { }
export class PlayerError extends Error { }
export class SessionError extends Error { }
export class ChannelError extends Error { }

/**
* Compares given objects. May not work correctly for
Expand Down

0 comments on commit 0b99180

Please sign in to comment.