Skip to content

Commit

Permalink
compatibility layer for trakt_ica;
Browse files Browse the repository at this point in the history
  • Loading branch information
radityaharya authored Oct 29, 2023
1 parent 8a61685 commit 8c2f571
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
60 changes: 60 additions & 0 deletions app/api/backward_compat/movies/route.ts
Original file line number Diff line number Diff line change
@@ -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",
});
}
}
58 changes: 58 additions & 0 deletions app/api/backward_compat/shows/route.ts
Original file line number Diff line number Diff line change
@@ -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",
});
}
}
2 changes: 0 additions & 2 deletions lib/trakt/utils/Movies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8c2f571

Please sign in to comment.