Skip to content

Commit

Permalink
fix; production build regex parse issue (#27)
Browse files Browse the repository at this point in the history
* fix: try @babel/plugin-transform-unicode-regex

* fix: update live id parsing

* fix: debug title

* fix: parsing title

* chore: add changelog

* fix: format
  • Loading branch information
sawaYch authored Sep 3, 2024
1 parent 9e8bad0 commit 01886bb
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-pans-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"next-youtube-livechat": patch
---

fix API parsing issue
201 changes: 194 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/next-youtube-livechat/.babelrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["@babel/plugin-transform-unicode-regex"]
}
1 change: 1 addition & 0 deletions packages/next-youtube-livechat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"react-dom": "^18"
},
"devDependencies": {
"@babel/plugin-transform-unicode-regex": "^7.24.7",
"@next/eslint-plugin-next": "^14.1.1",
"@repo/eslint-config": "*",
"@repo/typescript-config": "*",
Expand Down
17 changes: 12 additions & 5 deletions packages/next-youtube-livechat/src/libs/youtubeApiParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ export function getOptionsFromLivePage(
): FetchOptions & { liveId: string } {
let liveId: string;
const idResult = data.match(
/{"webCommandMetadata":{"url":"\/watch\?v=(.+?)","webPageType":/
/"originalUrl":"https:\/\/www.youtube.com\/watch\?v\\u003d(.+?)"/
);
console.log(idResult);
if (idResult) {
liveId = idResult[2];
liveId = idResult[0]
.replace('"originalUrl":"https://www.youtube.com/watch?v\\u003d', '')
.replace('"', '');
} else {
throw new Error('Live Stream was not found');
}
Expand Down Expand Up @@ -87,12 +90,16 @@ export function getOptionsFromLivePage(

let liveTitle: string;
const liveTitleResult = data.match(
/<meta name="title" content="[\s\S]*"><meta name="description"/
/{"playerOverlayVideoDetailsRenderer":{"title":{"simpleText":"[\s\S]*"},"subtitle":{/
);
console.log('liveTitleResult', liveTitleResult);
if (liveTitleResult) {
liveTitle = liveTitleResult[0]
.replace('<meta name="title" content="', '')
.replace('"><meta name="description"', '');
.replace(
'{"playerOverlayVideoDetailsRenderer":{"title":{"simpleText":"',
''
)
.replace('"},"subtitle":{', '');
} else {
throw new Error('Live Title was not found');
}
Expand Down

0 comments on commit 01886bb

Please sign in to comment.