Browse Source

fix: search pageable

Ben 9 months ago
parent
commit
664b8b4d92
2 changed files with 47 additions and 38 deletions
  1. 17 17
      js/youtube/test.js
  2. 30 21
      js/youtube/youtube.js

+ 17 - 17
js/youtube/test.js

@@ -1,23 +1,23 @@
-detail(`https://www.youtube.com/watch?v=DYptgVvkVLQ`, 'WEB')
-    .then(res => {
-        console.log(res);
-    })
-    .catch(e => {
-        console.log(e);
-    })
-
-// search("trump gets mercilessly booed at speech", null, "WEB")
+// detail(`https://www.youtube.com/watch?v=DYptgVvkVLQ`, 'WEB')
 //     .then(res => {
 //         console.log(res);
-
-//         search("trump gets mercilessly booed at speech", res["data"]["next"], "WEB")
-//             .then(res => {
-//                 console.log(res);
-//             })
-//             .catch(e => {
-//                 console.log(e);
-//             })
 //     })
 //     .catch(e => {
 //         console.log(e);
 //     })
+
+search("billboard pop songs 2024", null, "WEB")
+    .then(res => {
+        console.log(res);
+
+        search("trump gets mercilessly booed at speech", res["data"]["next"], "WEB")
+            .then(res => {
+                console.log(res);
+            })
+            .catch(e => {
+                console.log(e);
+            })
+    })
+    .catch(e => {
+        console.log(e);
+    })

+ 30 - 21
js/youtube/youtube.js

@@ -459,7 +459,7 @@ search = async (keyword, next, platform) => {
                 if (printable(platform)) {
                     console.log(video);
                 }
-                if (video && video["videoId"]) {
+                if (video && video["videoId"] && video["lengthText"]) {
                     videos.push({
                         "type": "videoWithContextRenderer",
                         "data": {
@@ -503,27 +503,36 @@ search = async (keyword, next, platform) => {
             }
 
             const ytInitialDataResp = JSON.parse(match[1]);
+            if (printable(platform)) {
+                console.log(ytInitialDataResp);
+            }
             const videos = [];
-            for (const item of ytInitialDataResp["contents"]?.["twoColumnSearchResultsRenderer"]?.["primaryContents"]?.["sectionListRenderer"]?.["contents"]?.[0]?.["itemSectionRenderer"]?.["contents"]) {
-                if (item["videoRenderer"]) {
-                    const video = item["videoRenderer"];
-                    if (printable(platform)) {
-                        console.log(video);
-                    }
-                    if (video && video["videoId"]) {
-                        videos.push({
-                            "type": "videoWithContextRenderer",
-                            "data": {
-                                "videoId": video["videoId"],
-                                "title": video["title"]?.["runs"]?.[0]?.["text"],
-                                "thumbnails": video["thumbnail"]?.["thumbnails"],
-                                "channelName": video["longBylineText"]?.["runs"]?.[0]?.["text"],
-                                "publishedTimeText": video["publishedTimeText"]?.["simpleText"],
-                                "viewCountText": video["viewCountText"]?.["simpleText"],
-                                "shortViewCountText": video["shortViewCountText"]?.["simpleText"],
-                                "lengthText": video["lengthText"]?.["simpleText"]
+            const contents = ytInitialDataResp["contents"]?.["twoColumnSearchResultsRenderer"]?.["primaryContents"]?.["sectionListRenderer"]?.["contents"] || []
+            for (const content of contents) {
+                const currentContents = content["itemSectionRenderer"]?.["contents"]
+                if (Array.isArray(currentContents)) {
+                    for (const currentContent of currentContents) {
+                        if (currentContent["videoRenderer"]) {
+                            const video = currentContent["videoRenderer"];
+                            if (printable(platform)) {
+                                console.log(video);
                             }
-                        });
+                            if (video && video["videoId"] && video["lengthText"]) {
+                                videos.push({
+                                    "type": "videoWithContextRenderer",
+                                    "data": {
+                                        "videoId": video["videoId"],
+                                        "title": video["title"]?.["runs"]?.[0]?.["text"],
+                                        "thumbnails": video["thumbnail"]?.["thumbnails"],
+                                        "channelName": video["longBylineText"]?.["runs"]?.[0]?.["text"],
+                                        "publishedTimeText": video["publishedTimeText"]?.["simpleText"],
+                                        "viewCountText": video["viewCountText"]?.["simpleText"],
+                                        "shortViewCountText": video["shortViewCountText"]?.["simpleText"],
+                                        "lengthText": video["lengthText"]?.["simpleText"]
+                                    }
+                                });
+                            }
+                        }
                     }
                 }
             }
@@ -536,7 +545,7 @@ search = async (keyword, next, platform) => {
                     .split(",")[0]
                     .split('"')[2];
             }
-            next["continuation"] = ytInitialDataResp["contents"]?.["twoColumnSearchResultsRenderer"]?.["primaryContents"]?.["sectionListRenderer"]?.["contents"]?.[1]?.["continuationItemRenderer"]?.["continuationEndpoint"]?.["continuationCommand"]?.["token"]
+            next["continuation"] = contents[contents.length - 1]?.["continuationItemRenderer"]?.["continuationEndpoint"]?.["continuationCommand"]?.["token"]
 
             const ret = {
                 "code": 200,