From 8c2f571cccc4f01572a920fa8fefc38deecb5ac0 Mon Sep 17 00:00:00 2001 From: Raditya Harya Date: Sun, 29 Oct 2023 14:41:26 +0000 Subject: [PATCH] compatibility layer for trakt_ica; --- app/api/backward_compat/movies/route.ts | 60 +++++++++++++++++++++++++ app/api/backward_compat/shows/route.ts | 58 ++++++++++++++++++++++++ lib/trakt/utils/Movies.ts | 2 - 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 app/api/backward_compat/movies/route.ts create mode 100644 app/api/backward_compat/shows/route.ts diff --git a/app/api/backward_compat/movies/route.ts b/app/api/backward_compat/movies/route.ts new file mode 100644 index 0000000..cdb6c1f --- /dev/null +++ b/app/api/backward_compat/movies/route.ts @@ -0,0 +1,60 @@ +// This endpoint is used as a compatibility layer for https://github.com/radityaharya/trakt_ical +// This will be removed in the future + +import { TraktAPI } from "@/lib/trakt/Trakt"; +import { NextResponse, NextRequest } from "next/server"; +import { headers } from "next/headers"; +import clientPromise from "@/lib/mongo/mongoPromise"; + +export async function GET(request: NextRequest) { + try { + const days_ago = request.url?.includes("days_ago") + ? parseInt(request.url.split("days_ago=")[1].split("&")[0]) + : 30; + const period = request.url?.includes("period") + ? parseInt(request.url.split("period=")[1].split("&")[0]) + : 30; + const key = request.url?.includes("key") + ? request.url.split("key=")[1].split("&")[0] + : undefined; + const userAgent = headers().get("user-agent") || ""; + if (/Mozilla|Chrome|Safari|Firefox|Edge/.test(userAgent)) { + throw new Error( + "Browser not supported for this route, use this link to Import the calendar", + ); + } + + console.log(`days_ago: ${days_ago} | period: ${period}`); + if (![days_ago, period].every(Number.isInteger)) { + throw new Error("days_ago and period must be integers"); + } + + const client = await clientPromise; + const db = client.db(process.env.NEXTAUTH_DB); + const collection = db.collection("nextauth_users"); + + const user = await collection.findOne({ user_id: key }); + if (!user) { + throw new Error("User not found"); + } + + const trakt = new TraktAPI(undefined, user.name); + const cal = ( + await trakt.Movies.getMoviesCalendar(days_ago, period) + ).toBlob(); + return new NextResponse(cal, { + headers: { + "Content-Type": "text/calendar", + "Content-Disposition": `attachment; filename="trakt-${ + user.name + }-${new Date().toISOString()}.ics"`, + }, + }); + } catch (error) { + console.error(error instanceof Error ? error.message : error); + return NextResponse.json({ + error: error instanceof Error ? error.message : error, + status: "error", + }); + } +} diff --git a/app/api/backward_compat/shows/route.ts b/app/api/backward_compat/shows/route.ts new file mode 100644 index 0000000..3a05f28 --- /dev/null +++ b/app/api/backward_compat/shows/route.ts @@ -0,0 +1,58 @@ +// This endpoint is used as a compatibility layer for https://github.com/radityaharya/trakt_ical +// This will be removed in the future + +import { TraktAPI } from "@/lib/trakt/Trakt"; +import { NextResponse, NextRequest } from "next/server"; +import { headers } from "next/headers"; +import clientPromise from "@/lib/mongo/mongoPromise"; + +export async function GET(request: NextRequest) { + try { + const days_ago = request.url?.includes("days_ago") + ? parseInt(request.url.split("days_ago=")[1].split("&")[0]) + : 30; + const period = request.url?.includes("period") + ? parseInt(request.url.split("period=")[1].split("&")[0]) + : 30; + const key = request.url?.includes("key") + ? request.url.split("key=")[1].split("&")[0] + : undefined; + const userAgent = headers().get("user-agent") || ""; + if (/Mozilla|Chrome|Safari|Firefox|Edge/.test(userAgent)) { + throw new Error( + "Browser not supported for this route, use this link to Import the calendar", + ); + } + + console.log(`days_ago: ${days_ago} | period: ${period}`); + if (![days_ago, period].every(Number.isInteger)) { + throw new Error("days_ago and period must be integers"); + } + + const client = await clientPromise; + const db = client.db(process.env.NEXTAUTH_DB); + const collection = db.collection("nextauth_users"); + + const user = await collection.findOne({ user_id: key }); + if (!user) { + throw new Error("User not found"); + } + + const trakt = new TraktAPI(undefined, user.name); + const cal = (await trakt.Shows.getShowsCalendar(days_ago, period)).toBlob(); + return new NextResponse(cal, { + headers: { + "Content-Type": "text/calendar", + "Content-Disposition": `attachment; filename="trakt-${ + user.name + }-${new Date().toISOString()}.ics"`, + }, + }); + } catch (error) { + console.error(error instanceof Error ? error.message : error); + return NextResponse.json({ + error: error instanceof Error ? error.message : error, + status: "error", + }); + } +} diff --git a/lib/trakt/utils/Movies.ts b/lib/trakt/utils/Movies.ts index 1310e8b..d6fcec3 100644 --- a/lib/trakt/utils/Movies.ts +++ b/lib/trakt/utils/Movies.ts @@ -220,8 +220,6 @@ export class MoviesUtil extends BaseUtil { if (entry.runtime === null || entry.runtime === 0) { entry.runtime = 120; } - console.log(entry ? entry.ids : "no movie"); - const summary = `${entry.title}`; const description = entry.overview