diff --git a/index.ts b/index.ts index 8accb28..f4d222c 100644 --- a/index.ts +++ b/index.ts @@ -17,6 +17,7 @@ import * as http from "http"; import * as google from "googlethis"; +import { Innertube, UniversalCache, Utils } from "youtubei.js"; const corsWhitelist = ["https://mrepol742.github.io"]; @@ -32,12 +33,44 @@ function getRoutes() { let url = ress.split("?")[0]; console.log(req.method + " " + req.headers.origin + " " + url); if (req.method != "GET" || !(corsWhitelist.indexOf(req.headers.origin) !== -1)) { - res.writeHead(301, { Location: "https://mrepol742.github.io/unauthorized" }); + res.writeHead(301, { Location: "https://mrepol742.github.io/unauthorized?utm_source=mrepol742.repl.co" }); res.end(); } else { - if (url == "/img" || url == "/img/index.html") { - let data = ress.split("?")[1]; - let results = []; + let data = ress.split("?")[1]; + let results = []; + if (url == "/vid" || url == "/vid/index.html") { + try { + const yt = await Innertube.create({ cache: new UniversalCache(false), generate_session_locally: true }); + const search = await yt.search(data, { type: "video" }); + if (search.results) { + res.setHeader("Access-Control-Allow-Origin", req.headers.origin); + res.setHeader("Content-Type", "application/json"); + res.writeHead(200); + res.end(JSON.stringify(search.results)); + } + } catch (err) { + res.setHeader("Access-Control-Allow-Origin", req.headers.origin); + res.setHeader("Content-Type", "application/json"); + res.writeHead(500); + res.end('{error: "Internal Server Error", code: 500}'); + } + } else if (url == "/mus" || url == "/mus/index.html") { + try { + const yt = await Innertube.create({ cache: new UniversalCache(false), generate_session_locally: true }); + const search = await yt.music.search(data, { type: "song" }); + if (search.results) { + res.setHeader("Access-Control-Allow-Origin", req.headers.origin); + res.setHeader("Content-Type", "application/json"); + res.writeHead(200); + res.end(JSON.stringify(search.results)); + } + } catch (err) { + res.setHeader("Access-Control-Allow-Origin", req.headers.origin); + res.setHeader("Content-Type", "application/json"); + res.writeHead(500); + res.end('{error: "Internal Server Error", code: 500}'); + } + } else if (url == "/img" || url == "/img/index.html") { try { const images = await google.image(data, { safe: true }); let i; @@ -49,11 +82,12 @@ function getRoutes() { res.writeHead(200); res.end(JSON.stringify(results)); } catch (err) { - console.log(err); + res.setHeader("Access-Control-Allow-Origin", req.headers.origin); + res.setHeader("Content-Type", "application/json"); + res.writeHead(500); + res.end('{error: "Internal Server Error", code: 500}'); } } else if (url == "/" || url == "/index.html") { - let data = ress.split("?")[1]; - let results = []; try { let response1 = await google.search(data, { page: 0, @@ -78,11 +112,23 @@ function getRoutes() { res.setHeader("Content-Type", "application/json"); res.writeHead(200); res.end(JSON.stringify(results)); - } catch (err) {} - } catch (err) {} + } catch (err) { + res.setHeader("Access-Control-Allow-Origin", req.headers.origin); + res.setHeader("Content-Type", "application/json"); + res.writeHead(500); + res.end('{error: "Internal Server Error", code: 500}'); + } + } catch (err) { + res.setHeader("Access-Control-Allow-Origin", req.headers.origin); + res.setHeader("Content-Type", "application/json"); + res.writeHead(500); + res.end('{error: "Internal Server Error", code: 500}'); + } } else { - res.writeHead(301, { Location: "https://mrepol742.github.io/404.html" }); - res.end(); + res.setHeader("Access-Control-Allow-Origin", req.headers.origin); + res.setHeader("Content-Type", "application/json"); + res.writeHead(400); + res.end('{error: "Bad Request", code: 400}'); } } }; diff --git a/package-lock.json b/package-lock.json index 0874a56..49ca9f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,17 @@ { - "name": "search", + "name": "webvium-search", "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "search", + "name": "webvium-search", "version": "1.0.1", "license": "SEE LICENSE IN https://github.com/mrepol742/search/blob/master/LICENSE", "dependencies": { "@types/node": "^20.3.1", - "googlethis": "^1.7.1" + "googlethis": "^1.8.0", + "youtubei.js": "^5.1.0" }, "devDependencies": { "prettier": "^2.8.8" @@ -19,11 +20,30 @@ "node": ">=18.x" } }, + "node_modules/@fastify/busboy": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@fastify/busboy/-/busboy-2.0.0.tgz", + "integrity": "sha512-JUFJad5lv7jxj926GPgymrWQxxjPYuJNiNjNMzqT+HiuP6Vl3dk5xzG+8sTX96np0ZAluvaMzPsjhHZ5rNuNQQ==", + "engines": { + "node": ">=14" + } + }, "node_modules/@types/node": { "version": "20.3.1", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.3.1.tgz", "integrity": "sha512-EhcH/wvidPy1WeML3TtYFGR83UzjxeWRen9V402T8aUGYsCHOmfoisV3ZSg03gAFIbLq8TnWOJ0f4cALtnSEUg==" }, + "node_modules/acorn": { + "version": "8.10.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.10.0.tgz", + "integrity": "sha512-F0SAmZ8iUtS//m8DmCTA0jlh6TDKkHQyK6xc6V4KDTyZKA9dnvX9/3sRTVQrWm79glUAZbnmmNcdYwUIHWVybw==", + "bin": { + "acorn": "bin/acorn" + }, + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -114,6 +134,11 @@ "url": "https://github.com/sponsors/fb55" } }, + "node_modules/cssom": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/cssom/-/cssom-0.5.0.tgz", + "integrity": "sha512-iKuQcq+NdHqlAcwUY0o/HL69XQrUaQdMjmStJ8JFmUaiiQErlhrmuigkg/CU4E2J0IyUKUrMAgl36TvN67MqTw==" + }, "node_modules/delayed-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", @@ -214,9 +239,9 @@ } }, "node_modules/googlethis": { - "version": "1.7.1", - "resolved": "https://registry.npmjs.org/googlethis/-/googlethis-1.7.1.tgz", - "integrity": "sha512-2rJWqcNQeY5/D3HUQn4aM9piru5LjPFG6TTumww5hRMm6tmdnHu/WwSXji6ueUbKVZUHsHQO8mICV7mJK9I7ug==", + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/googlethis/-/googlethis-1.8.0.tgz", + "integrity": "sha512-sMS9kXr9ZiQ41ij8Hxum2NuNLyx+I3WC9dv/mEGsEvsqcovC9reeswRETOh0Toez9Nx2ri6Nwr+k4Q2YkKavRg==", "dependencies": { "axios": "^0.21.1", "cheerio": "1.0.0-rc.10", @@ -230,6 +255,11 @@ "url": "https://github.com/sponsors/LuanRT" } }, + "node_modules/html-escaper": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-3.0.3.tgz", + "integrity": "sha512-RuMffC89BOWQoY0WKGpIhn5gX3iI54O6nRA0yC124NYVtzjmFWBIiFd8M0x+ZdX0P9R4lADg1mgP8C7PxGOWuQ==" + }, "node_modules/htmlparser2": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-6.1.0.tgz", @@ -248,6 +278,113 @@ "entities": "^2.0.0" } }, + "node_modules/jintr": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/jintr/-/jintr-1.1.0.tgz", + "integrity": "sha512-Tu9wk3BpN2v+kb8yT6YBtue+/nbjeLFv4vvVC4PJ7oCidHKbifWhvORrAbQfxVIQZG+67am/mDagpiGSVtvrZg==", + "funding": [ + "https://github.com/sponsors/LuanRT" + ], + "dependencies": { + "acorn": "^8.8.0" + } + }, + "node_modules/linkedom": { + "version": "0.14.26", + "resolved": "https://registry.npmjs.org/linkedom/-/linkedom-0.14.26.tgz", + "integrity": "sha512-mK6TrydfFA7phrnp+1j57ycBwFI5bGSW6YXlw9acHoqF+mP/y+FooEYYyniOt5Ot57FSKB3iwmnuQ1UUyNLm5A==", + "dependencies": { + "css-select": "^5.1.0", + "cssom": "^0.5.0", + "html-escaper": "^3.0.3", + "htmlparser2": "^8.0.1", + "uhyphen": "^0.2.0" + } + }, + "node_modules/linkedom/node_modules/css-select": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/css-select/-/css-select-5.1.0.tgz", + "integrity": "sha512-nwoRF1rvRRnnCqqY7updORDsuqKzqYJ28+oSMaJMMgOauh3fvwHqMS7EZpIPqK8GL+g9mKxF1vP/ZjSeNjEVHg==", + "dependencies": { + "boolbase": "^1.0.0", + "css-what": "^6.1.0", + "domhandler": "^5.0.2", + "domutils": "^3.0.1", + "nth-check": "^2.0.1" + }, + "funding": { + "url": "https://github.com/sponsors/fb55" + } + }, + "node_modules/linkedom/node_modules/dom-serializer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-2.0.0.tgz", + "integrity": "sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg==", + "dependencies": { + "domelementtype": "^2.3.0", + "domhandler": "^5.0.2", + "entities": "^4.2.0" + }, + "funding": { + "url": "https://github.com/cheeriojs/dom-serializer?sponsor=1" + } + }, + "node_modules/linkedom/node_modules/domhandler": { + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-5.0.3.tgz", + "integrity": "sha512-cgwlv/1iFQiFnU96XXgROh8xTeetsnJiDsTc7TYCLFd9+/WNkIqPTxiM/8pSd8VIrhXGTf1Ny1q1hquVqDJB5w==", + "dependencies": { + "domelementtype": "^2.3.0" + }, + "engines": { + "node": ">= 4" + }, + "funding": { + "url": "https://github.com/fb55/domhandler?sponsor=1" + } + }, + "node_modules/linkedom/node_modules/domutils": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/domutils/-/domutils-3.1.0.tgz", + "integrity": "sha512-H78uMmQtI2AhgDJjWeQmHwJJ2bLPD3GMmO7Zja/ZZh84wkm+4ut+IUnUdRa8uCGX88DiVx1j6FRe1XfxEgjEZA==", + "dependencies": { + "dom-serializer": "^2.0.0", + "domelementtype": "^2.3.0", + "domhandler": "^5.0.3" + }, + "funding": { + "url": "https://github.com/fb55/domutils?sponsor=1" + } + }, + "node_modules/linkedom/node_modules/entities": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz", + "integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==", + "engines": { + "node": ">=0.12" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, + "node_modules/linkedom/node_modules/htmlparser2": { + "version": "8.0.2", + "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-8.0.2.tgz", + "integrity": "sha512-GYdjWKDkbRLkZ5geuHs5NY1puJ+PXwP7+fHPRz06Eirsb9ugf6d8kkXav6ADhcODhFFPMIXyxkxSuMf3D6NCFA==", + "funding": [ + "https://github.com/fb55/htmlparser2?sponsor=1", + { + "type": "github", + "url": "https://github.com/sponsors/fb55" + } + ], + "dependencies": { + "domelementtype": "^2.3.0", + "domhandler": "^5.0.3", + "domutils": "^3.0.1", + "entities": "^4.4.0" + } + }, "node_modules/mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", @@ -311,10 +448,40 @@ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.2.tgz", "integrity": "sha512-5svOrSA2w3iGFDs1HibEVBGbDrAY82bFQ3HZ3ixB+88nsbsWQoKqDRb5UBYAUPEzbBn6dAp5gRNXglySbx1MlA==" }, + "node_modules/uhyphen": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/uhyphen/-/uhyphen-0.2.0.tgz", + "integrity": "sha512-qz3o9CHXmJJPGBdqzab7qAYuW8kQGKNEuoHFYrBwV6hWIMcpAmxDLXojcHfFr9US1Pe6zUswEIJIbLI610fuqA==" + }, + "node_modules/undici": { + "version": "5.26.4", + "resolved": "https://registry.npmjs.org/undici/-/undici-5.26.4.tgz", + "integrity": "sha512-OG+QOf0fTLtazL9P9X7yqWxQ+Z0395Wk6DSkyTxtaq3wQEjIroVe7Y4asCX/vcCxYpNGMnwz8F0qbRYUoaQVMw==", + "dependencies": { + "@fastify/busboy": "^2.0.0" + }, + "engines": { + "node": ">=14.0" + } + }, "node_modules/unraw": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/unraw/-/unraw-2.0.1.tgz", "integrity": "sha512-tdOvLfRzHolwYcHS6HIX860MkK9LQ4+oLuNwFYL7bpgTEO64PZrcQxkisgwJYCfF8sKiWLwwu1c83DvMkbefIQ==" + }, + "node_modules/youtubei.js": { + "version": "5.8.0", + "resolved": "https://registry.npmjs.org/youtubei.js/-/youtubei.js-5.8.0.tgz", + "integrity": "sha512-xMQxbhy0TMpLvJnGbLnqAE9RhiLCwBuVrsTyVd0ZFX6o98fmJmddcE1qeRIagNGNr16BmofqtsbGXIfpZprFhA==", + "funding": [ + "https://github.com/sponsors/LuanRT" + ], + "dependencies": { + "jintr": "^1.1.0", + "linkedom": "^0.14.12", + "tslib": "^2.5.0", + "undici": "^5.19.1" + } } } } diff --git a/package.json b/package.json index 518cefb..12f6604 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "webvium-search", "version": "1.0.1", - "description": "Backend for Webvium Search Results using googlethis npm package.", + "description": "Backend for Webvium Search Results using googlethis & youtubei.js npm package.", "main": "index.js", "scripts": { "start": "clear && while [ true ]; do node index.js ; done", @@ -20,7 +20,8 @@ "homepage": "https://github.com/mrepol742/search#readme", "dependencies": { "@types/node": "^20.3.1", - "googlethis": "^1.8.0" + "googlethis": "^1.8.0", + "youtubei.js": "^5.1.0" }, "devDependencies": { "prettier": "^2.8.8"