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

"Cannot read properties of undefined (reading 'videoId')" in nodejs lambda environment. #124

Open
jo-sip opened this issue Nov 15, 2024 · 9 comments
Labels
bug Something isn't working

Comments

@jo-sip
Copy link

jo-sip commented Nov 15, 2024

Describe the bug
I'm running into an issue where supplying a valid videoId throws a stack trace when running in a lambda environment. This happens:

TypeError: Cannot read properties of undefined (reading 'videoId')
at BaseVideoParser.loadBaseVideo (/var/task/index.js:79129:44)
at Video.load (/var/task/index.js:79289:43)
at Video.load (/var/task/index.js:80595:15)
at Client3. (/var/task/index.js:80753:113)
at Generator.next ()
at fulfilled (/var/task/index.js:80655:28)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

when the videoId is defined.

Could there be some async client issue? Static loadBaseVideo member? parseRawData returning null or no data? I'm thinking the latter could be likely, are there some options I need to set for different environments?

const youtube = new Client();

To Reproduce
Steps to reproduce the behavior:

Expected behavior
It not throw, as it doesn't locally.

@jo-sip jo-sip added the bug Something isn't working label Nov 15, 2024
@xiaohaoxing
Copy link
Contributor

Me too, my own crawler tool is also facing this problem, I'm working on it

@xiaohaoxing
Copy link
Contributor

Could you please provide the minimum code that reproduces this problem?

@jo-sip
Copy link
Author

jo-sip commented Nov 15, 2024

Could you please provide the minimum code that reproduces this problem?

Sure thing, here's my deployment code:

const apiHandler = new NodejsFunction(this, env("api-handler"), {
      architecture: Architecture.ARM_64,
      memorySize: 512,
      runtime: Runtime.NODEJS_20_X,
      timeout: Duration.seconds(20),
      entry: path.join(__dirname, "./lambda/api.handler.ts"),
      bundling: {
        minify: environment === "prod",
        target: "esnext",
      },
    });
export const handler = async (
  event: APIGatewayProxyEventV2,
): Promise<APIGatewayProxyResult> => {
  const youtube = new Client();
  console.log("v2");
  const video = await youtube.getVideo("dMvw7xx9HhQ");
  console.log(video);
  return {
    statusCode: 200,
    body: JSON.stringify({ message: "Hello from Lambda!" }),
  };
};
2024-11-15T18:13:11.638Z    ad0f6f85-9f76-4a06-ac79-b227475701d4    ERROR   Invoke Error    {"errorType":"TypeError","errorMessage":"Cannot read properties of undefined (reading 'videoId')","stack":["TypeError: Cannot read properties of undefined (reading 'videoId')","    at BaseVideoParser.loadBaseVideo (/var/task/index.js:79129:44)","    at Video.load (/var/task/index.js:79289:43)","    at Video.load (/var/task/index.js:80595:15)","    at Client4.<anonymous> (/var/task/index.js:80753:113)","    at Generator.next (<anonymous>)","    at fulfilled (/var/task/index.js:80655:28)","    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"]}

@jo-sip
Copy link
Author

jo-sip commented Nov 15, 2024

Is there a debugging method to log the http calls from the Client? I'm thinking that perhaps the call for the video is being blocked or erroring out, ending up being empty? (Or YT changed the structure of the response?)

const nextPromise = this.http.post(`${I_END_POINT}/next`, { data: { videoId } });
const playerPromise = this.http.post(`${I_END_POINT}/player`, { data: { videoId } });
const [nextResponse, playerResponse] = await Promise.all([nextPromise, playerPromise]);
const data = { response: nextResponse.data, playerResponse: playerResponse.data };
if (
!data.response?.contents?.twoColumnWatchNextResults.results.results.contents ||
data.playerResponse.playabilityStatus.status === "ERROR"
) {
return undefined as T;
}

@SuspiciousLookingOwl
Copy link
Owner

SuspiciousLookingOwl commented Nov 16, 2024

seems like YouTube made some changes to the response data, this changes rolled up on some user / IP (my local PC loads the data just fine, but I'm facing similar error on my server), checking it

Edit: I think it's caused by your IP got flagged by YouTube, so the client isn't able to retrieve the data properly, I'll try to look for a solution

@xiaohaoxing
Copy link
Contributor

Hi, I've working on this issue for a while and find the solution for getting video information #126
But i didn't confirm that is it necessary to add params to nextPromise above, can you please check it? @SuspiciousLookingOwl Thanks!

@shuntan
Copy link

shuntan commented Nov 24, 2024

I got same error and using proxy:

/www/wwwroot/youtubei/node_modules/youtubei/dist/cjs/youtube/BaseVideo/BaseVideoParser.js:14
target.id = videoInfo.videoDetails.videoId;
^

TypeError: Cannot read properties of undefined (reading 'videoId')
at Function.loadBaseVideo (/www/wwwroot/youtubei/node_modules/youtubei/dist/cjs/youtube/BaseVideo/BaseVideoParser.js:14:44)
at Video.load (/www/wwwroot/youtubei/node_modules/youtubei/dist/cjs/youtube/BaseVideo/BaseVideo.js:21:43)
at Video.load (/www/wwwroot/youtubei/node_modules/youtubei/dist/cjs/youtube/Video/Video.js:30:15)
at Client. (/www/wwwroot/youtubei/node_modules/youtubei/dist/cjs/youtube/Client/Client.js:94:55)
at Generator.next ()
at fulfilled (/www/wwwroot/youtubei/node_modules/youtubei/dist/cjs/youtube/Client/Client.js:5:58)
at processTicksAndRejections (node:internal/process/task_queues:96:5)

@fdciabdul
Copy link

as far i know, youtube always flag suspicious IP, make sure you have to slow down or make random interval request, so your server IP doesn't get flag 'spam' by Youtube

@DoxrGitHub
Copy link

DoxrGitHub commented Nov 29, 2024

I found what file was erroring, I edited the build (sorry) and made it log the "raw" thing it got before it tried to get values from it and saw that the response was beautiful and in my case I would actually be fine with just having that data and parsing it myself (but obviously its not that easy)

So I don't think its an error due to YouTube returning a bad response, they probably changed how the response looks and yeah. Unless I'm not recognizing it's not returning the actual info.

EDIT: Ended up monkey patching the build, it seems to work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants