summary refs log tree commit diff
path: root/api/src
diff options
context:
space:
mode:
authoruurgothat <cckhmck@gmail.com>2021-10-09 18:23:16 +0300
committeruurgothat <cckhmck@gmail.com>2021-10-09 18:23:16 +0300
commitcf7383763d408583b2ede98c2ea5700806fc4e15 (patch)
treea80c3c0105b1022e7a1d72405fe618062b92527b /api/src
parentMerge branch 'master' of http://github.com/fosscord/fosscord-server (diff)
downloadserver-cf7383763d408583b2ede98c2ea5700806fc4e15.tar.xz
Implement GIFs
Diffstat (limited to 'api/src')
-rw-r--r--api/src/routes/gifs/search.ts37
-rw-r--r--api/src/routes/gifs/trending-gifs.ts37
-rw-r--r--api/src/routes/gifs/trending.ts48
3 files changed, 122 insertions, 0 deletions
diff --git a/api/src/routes/gifs/search.ts b/api/src/routes/gifs/search.ts
new file mode 100644
index 00000000..3cbff64f
--- /dev/null
+++ b/api/src/routes/gifs/search.ts
@@ -0,0 +1,37 @@
+import { Router, Response, Request } from "express";
+import fetch from "node-fetch";
+import { route } from "@fosscord/api";
+
+const router = Router();
+
+router.get("/", route({}), async (req: Request, res: Response) => {
+	// TODO: Custom providers and code quality
+	const { q, media_format, locale, provider } = req.query;
+
+	const parseResult = (result: any) => {
+		return {
+			id: result.id,
+			title: result.title,
+			url: result.itemurl,
+			src: result.media[0].mp4.url,
+			gif_src: result.media[0].gif.url,
+			width: result.media[0].mp4.dims[0],
+			height: result.media[0].mp4.dims[1],
+			preview: result.media[0].mp4.preview
+		};
+	};
+
+	const response = await fetch(`https://g.tenor.com/v1/search?q=${q}&media_format=${media_format}&locale=${locale}&key=LIVDSRZULELA`, {
+		method: "get",
+		headers: { "Content-Type": "application/json" }
+	});
+
+	const { results } = await response.json();
+	let cache = new Array() as any[];
+	results.forEach((result: any) => {
+		cache.push(parseResult(result));
+	});
+	res.json(cache).status(200);
+});
+
+export default router;
diff --git a/api/src/routes/gifs/trending-gifs.ts b/api/src/routes/gifs/trending-gifs.ts
new file mode 100644
index 00000000..76e791fa
--- /dev/null
+++ b/api/src/routes/gifs/trending-gifs.ts
@@ -0,0 +1,37 @@
+import { Router, Response, Request } from "express";
+import fetch from "node-fetch";
+import { route } from "@fosscord/api";
+
+const router = Router();
+
+router.get("/", route({}), async (req: Request, res: Response) => {
+	// TODO: Custom providers and code quality
+	const { media_format, locale, provider } = req.query;
+
+	const parseResult = (result: any) => {
+		return {
+			id: result.id,
+			title: result.title,
+			url: result.itemurl,
+			src: result.media[0].mp4.url,
+			gif_src: result.media[0].gif.url,
+			width: result.media[0].mp4.dims[0],
+			height: result.media[0].mp4.dims[1],
+			preview: result.media[0].mp4.preview
+		};
+	};
+
+	const response = await fetch(`https://g.tenor.com/v1/trending?media_format=${media_format}&locale=${locale}&key=LIVDSRZULELA`, {
+		method: "get",
+		headers: { "Content-Type": "application/json" }
+	});
+
+	const { results } = await response.json();
+	let cache = new Array() as any[];
+	results.forEach((result: any) => {
+		cache.push(parseResult(result));
+	});
+	res.json(cache).status(200);
+});
+
+export default router;
diff --git a/api/src/routes/gifs/trending.ts b/api/src/routes/gifs/trending.ts
new file mode 100644
index 00000000..3b91eb12
--- /dev/null
+++ b/api/src/routes/gifs/trending.ts
@@ -0,0 +1,48 @@
+import { Router, Response, Request } from "express";
+import fetch from "node-fetch";
+import { route } from "@fosscord/api";
+
+const router = Router();
+
+router.get("/", route({}), async (req: Request, res: Response) => {
+	// TODO: Custom providers and code quality
+	const { media_format, locale, provider } = req.query;
+
+	const parseResult = (result: any) => {
+		return {
+			id: result.id,
+			title: result.title,
+			url: result.itemurl,
+			src: result.media[0].mp4.url,
+			gif_src: result.media[0].gif.url,
+			width: result.media[0].mp4.dims[0],
+			height: result.media[0].mp4.dims[1],
+			preview: result.media[0].mp4.preview
+		};
+	};
+
+	const responseSource = await fetch(`https://g.tenor.com/v1/categories?media_format=${media_format}&locale=${locale}&key=LIVDSRZULELA`, {
+		method: "get",
+		headers: { "Content-Type": "application/json" }
+	});
+
+	const trendGifSource = await fetch(`https://g.tenor.com/v1/trending?media_format=${media_format}&locale=${locale}&key=LIVDSRZULELA`, {
+		method: "get",
+		headers: { "Content-Type": "application/json" }
+	});
+
+	const { tags } = await responseSource.json();
+	const { results } = await trendGifSource.json();
+	let cache = new Array() as any[];
+
+	tags.forEach((result: any) => {
+		cache.push({
+			name: result.searchterm,
+			src: result.image
+		});
+	});
+
+	res.json({ categories: [cache], gifs: [parseResult(results[0])] }).status(200);
+});
+
+export default router;