Looping on selector text (node.js)

I have a code snippet that once goes to a youtube link, it gets videos links and then opening them in a new tab.
I’m trying to make it to do so that it only gets the video link if text in the selector is present and has the text of it say a number less than 5. (example: uploaded 4 minutes ago), if there aren’t any videos that suitable, then the page will reload after 10seconds, and try again until videos have been found.

I tried using catch but it doesn’t work as I don’t exactly understand how to do this very well in my situation.

here is the selector of the time uploaded ago (as defined):

uploadedago : "#metadata-line > span:nth-child(4)",

(that’s in another file called “constants>selector>index.js”.)

here is the code that I’m working with:

   spinners.add('first-spinner',
   {
      text: 'Searching for videos..',
      color: 'yellow',
   });
   for (let i = 0; i < keyword.length; i++)
   {
      if (config.cuser == true)
      {
         await page.goto('https://www.youtube.com/c/username');
      }
      else
      {
         await page.goto(`https://www.youtube.com/results?search_query=${keyword[i]}&sp=CAISBAgBEAE%253D&sp=EgIQAQ%253D%253D`);

         const element = await page.$(selector.shortvideos,
         );
         if (element)
         {
            await page.evaluate(() =>
            {
               document
                  .querySelector(selector.shortvideos)
                  .remove();
            });
         }
      }

      await page.waitForTimeout(3000);
      spinners.succeed('first-spinner',
      {
         text: 'done..',
         color: 'yellow'
      });
      await page.waitForTimeout(7000);
      spinners.add('hasil',
      {
         text: 'Collecting videos..',
         color: 'yellow'
      });

      //collecting links
      let linked = await Promise.all((await page.$$(selector.videoTitleinSearch)).map(async a =>
      {
         return {
            url :await (await a.getProperty('href')).jsonValue(),
            title : await (await a.getProperty('title')).jsonValue()
         };
      }));

      let uploaded = await Promise.all((await page.$$(selector.uploadedago)).map(async a =>
         {
            return {
               url :await (await a.getProperty('href')).jsonValue()
            }
         }));

      let hrefs = await Promise.all((await page.$$(selector.shortsTitleinSearch)).map(async a =>
      {
         return await (await a.getProperty('href')).jsonValue();
      }));

      if (hrefs.length === 0)
      {
         linked.push(hrefs);
      }

      const link = linked.filter((el) => el.url != null);

      spinners.succeed('hasil',
      {
         text: `FOUND ${link.length}LINKS`,
         color: 'yellow',
      });

Aim of new code: It should log the number of videos uploaded under 5 minutes ago (including seconds as they’re less than minutes) to the console.

Disclaimer: this is a script strictly used to scrape data on youtube only for my own channels. (it’s a link of keywords that only my videos are under in the tags section.)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.