Compare commits

...

7 commits

Author SHA1 Message Date
Nikhil Nawgiri 40a5088513 Refactor 2024-10-27 01:26:47 +02:00
Nikhil Nawgiri 01291a74fa Cleanup 2024-10-27 01:24:57 +02:00
Nikhil Nawgiri 75d046c934 Add /check endpoint to check single imdb against jf and wishlist 2024-10-27 01:23:47 +02:00
Nikhil Nawgiri f4d7afc468 Improve fsHelper for reading wishlist 2024-10-27 01:19:21 +02:00
Nikhil Nawgiri 45fb46efa5 Add func to read wishlist from disk 2024-10-27 00:55:21 +02:00
Nikhil Nawgiri 48314d97b0 Update gitignore 2024-10-27 00:43:23 +02:00
Nikhil Nawgiri ca880154bf Refactor fsHelper 2024-10-27 00:43:04 +02:00
3 changed files with 42 additions and 17 deletions

3
.gitignore vendored
View file

@ -1,5 +1,6 @@
be/config.ts
database.json
jellyfin_content.json
wishlist.json
# Logs
logs

View file

@ -1,6 +1,23 @@
import * as fs from "fs";
export const saveJsonToDisk = (content: {}) => {
const outputPath = `../database.json`;
fs.writeFileSync(outputPath, JSON.stringify(content));
const JELLYFIN_CONTENT_PATH = "../jellyfin_content.json";
const WISHLIST_PATH = "../wishlist.json";
const saveToDisk = (path: string, content: any) => fs.writeFileSync(path, JSON.stringify(content));
export const saveJfItemsToDisk = (content: {}) => saveToDisk(JELLYFIN_CONTENT_PATH, content);
export const saveWishlistToDisk = (content: {}) => saveToDisk(WISHLIST_PATH, content);
export const readWishlistFromDisk = async () => {
let wishlistItems: string[] = [];
if (await fs.existsSync(WISHLIST_PATH)) {
const fileContent = JSON.parse(await fs.readFileSync(WISHLIST_PATH, "utf8"));
wishlistItems = fileContent.wishlistItems;
} else {
// create empty wishlist file
saveWishlistToDisk({ wishlistItems });
}
return wishlistItems;
}

View file

@ -1,5 +1,5 @@
import { fetchFromJellyfinApi } from "./jellyfinApiHelper.ts";
import { saveJsonToDisk } from "./fsHelper.ts";
import { readWishlistFromDisk, saveJfItemsToDisk } from "./fsHelper.ts";
import { transformItems } from "./itemHelper.ts";
import { app } from "./expressHelper.ts";
@ -16,22 +16,31 @@ const run = async () => {
}: JellyfinApiResponse = await fetchFromJellyfinApi();
const transformedItems = transformItems(items);
console.log("transformedItems:", transformedItems);
const arrayOfExistingImdbIds = transformedItems.map(item => item.imdbId);
console.log("arrayOfExistingImdbIds:", arrayOfExistingImdbIds);
const jellyfinExistingItems = transformedItems.map(item => item.imdbId);
// saveJsonToDisk({ totalItemCount, jellyfinItems: transformedItems });
// saveJfItemsToDisk({ totalItemCount, jellyfinItems: transformedItems });
app.get("/dingle", (req, res) => {
res.json(transformedItems)
});
// app.get("/items", (req, res) => {
// res.json(transformedItems)
// });
// to do: endpoint to check single items against arrayOfExistingImdbIds
// improve this endpoint to handle array of imdbIds
app.get("/check/:imdbId", async (req, res) => {
const { imdbId } = req.params;
const wishlistItems = await readWishlistFromDisk();
if (jellyfinExistingItems.includes(imdbId)) {
res.json({ imdbId, status: "ON_JF" });
} else if (wishlistItems.includes(imdbId)) {
res.json({ imdbId, status: "ON_WISHLIST" });
} else {
res.json({ imdbId, status: "NO_MATCH" });
}
})
app.post("/bob", (req, res) => {
app.post("/add", (req, res) => {
const { imdbId: requestedImdbId } = req.body;
if (arrayOfExistingImdbIds.includes(requestedImdbId)) {
if (jellyfinExistingItems.includes(requestedImdbId)) {
res.send("DUP\n");
} else {
// to do: push to wishlist array
@ -41,8 +50,6 @@ const run = async () => {
})
app.listen(1312);
// Add logic to endpoints
}
run();