Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): live id, channel name and channel url parsing logic #22

Merged
merged 11 commits into from
Aug 24, 2024
5 changes: 5 additions & 0 deletions .changeset/angry-plants-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"next-youtube-livechat": patch
---

fix api due to youtube changes
38 changes: 27 additions & 11 deletions packages/next-youtube-livechat/src/libs/youtubeApiParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export function getOptionsFromLivePage(
): FetchOptions & { liveId: string } {
let liveId: string;
const idResult = data.match(
/<link rel="canonical" href="https:\/\/www.youtube.com\/watch\?v=(.+?)">/
/{"webCommandMetadata":{"url":"\/watch\?v=(.+?)","webPageType":/
);
if (idResult) {
liveId = idResult[1];
liveId = idResult[2];
} else {
throw new Error('Live Stream was not found');
}
Expand Down Expand Up @@ -63,10 +63,16 @@ export function getOptionsFromLivePage(

let liveThumbnail: string;
const liveThumbnailResult = data.match(
/<link rel="image_src" href="https:\/\/i.ytimg.com\/vi\/[^\s]+\/maxresdefault_live.jpg">/
/'https:\/\/i.ytimg.com\/vi\/[^\s]+\/hqdefault.jpg'\)/
);
if (liveThumbnailResult) {
liveThumbnail = liveThumbnailResult[0];
let lastIndex = liveThumbnail.lastIndexOf("')");
if (lastIndex !== -1) {
liveThumbnail =
liveThumbnail.substring(0, lastIndex) +
liveThumbnail.substring(lastIndex + 2);
}
const res = liveThumbnail.match(/https?:\/\/[^\s">]+/)?.[0];
if (res == null) {
throw new Error('Live Thumbnail image url parse error');
Expand All @@ -89,26 +95,36 @@ export function getOptionsFromLivePage(
}

let channelName: string;
const channelNameResult = data.match(/ownerChannelName":".*","liveBroadcast/);
const channelNameResult = data.match(
/"videoDescriptionInfocardsSectionRenderer":{"sectionTitle":{"simpleText":".*"},"creatorVideosButton"/
);
console.log('channelNameResult', channelNameResult);
if (channelNameResult) {
channelName = channelNameResult[0]
.replace('ownerChannelName":"', '')
.replace('","liveBroadcast', '');
.replace(
'"videoDescriptionInfocardsSectionRenderer":{"sectionTitle":{"simpleText":"',
''
)
.replace('"},"creatorVideosButton"', '');
} else {
throw new Error('Channel Name was not found');
// throw new Error('Channel Name was not found');
channelName = '???'; // FIXME
}

let channelUrl: string = 'https://www.youtube.com/';
const channelUrlResult = data.match(
/"ownerProfileUrl":"http:\/\/www.youtube.com\/.*","externalChannelId":/
/"canonicalBaseUrl":"\/@.*"}}}]},"subscriptionButton":{"type":"FREE"},/
);
console.log('channelUrlResult', channelUrlResult);

if (channelUrlResult) {
const channelAtId = channelUrlResult[0]
.replace('"ownerProfileUrl":"http://www.youtube.com/', '')
.replace('","externalChannelId":', '');
.replace('"canonicalBaseUrl":"/', '')
.replace('"}}}]},"subscriptionButton":{"type":"FREE"},', '');
channelUrl += channelAtId;
} else {
throw new Error('Channel Url was not found');
// throw new Error('Channel Url was not found');
channelUrl = '???'; // FIXME
}

return {
Expand Down