Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mrepol742 committed Oct 20, 2023
1 parent bfda75b commit d3989a3
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 19 deletions.
68 changes: 57 additions & 11 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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}');
}
}
};
Expand Down
179 changes: 173 additions & 6 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down

0 comments on commit d3989a3

Please sign in to comment.