diff --git a/database.sqlite b/database.sqlite new file mode 100644 index 0000000..f474932 Binary files /dev/null and b/database.sqlite differ diff --git a/index.js b/index.js index 3f8abf7..515cc8c 100644 --- a/index.js +++ b/index.js @@ -7,30 +7,20 @@ const sqlite3 = require("sqlite3"); const requestSync = require("sync-request"); var Config = require('config-js'); const app = express(); +const PrismaClient = require('@prisma/client').PrismaClient; const port = 4000; +const eanConvAdress = "http://192.168.178.51:9999/" -const initalTableSQL = - "CREATE TABLE `movieInfo` ( \ - `id` INT NOT NULL, \ - `movieID` VARCHAR(128), \ - `movieTitel` VARCHAR(255), \ - `place` INT zerofill, \ - `type` INT zerofill, \ - `posterUrl` VARCHAR(255) \ -);"; +const typeIndex = {0: "Physical", 1: "Digital - Maxdome", 2: "Digital - Amazon Prime Video", 3: "Digital - DVR"} -const initalTableSQL2 = - "CREATE TABLE `apiChache` ( \ - `imdbID` VARCHAR(128), \ - `url` VARCHAR(255) \ -);"; +const prisma = new PrismaClient(); var responseBuffer = { allBufferd: [] }; responseBuffer = JSON.parse(fs.readFileSync("chache.bin", "utf8")); -var db = new sqlite3.Database("database.sqllite"); -if (!fs.existsSync("database.sqllite")) { +var db = new sqlite3.Database("database.sqlite"); +if (!fs.existsSync("database.sqlite")) { console.log("Creating table."); db.serialize(() => { db.run(initalTableSQL); @@ -38,122 +28,25 @@ if (!fs.existsSync("database.sqllite")) { }); } db.close(); -var db = new sqlite3.Database("database.sqllite"); +var db = new sqlite3.Database("database.sqlite"); var config = new Config('key.cfg'); -async function doesMovieExistInDatabase(imdbID) { - console.warn(imdbID); - db.all( - "SELECT * FROM movieInfo WHERE movieID='" + imdbID + "'", - function (err, rows) { - console.log("!!!" + rows); - if (rows == undefined) { - console.log(false); - return false; - } else { - console.log(true); - return true; - } - } - ); -} - -function getBufferdResponse(type, imdbID) { - // Cover image - if (type == 1) { - db.all( - "SELECT * FROM apiChache WHERE imdbID=" + imdbID, - function (err, rows) { - // Okay, there is no result, lets ask the real api - if (rows == undefined) { - console.log("No chache"); - var res = requestSync( - "GET", - "https://imdb-api.com/en/API/Posters//" + imdbID, - {} - ); - const jsonParsed = JSON.parse(res.body); - // Well frick, api limit reached - if (jsonParsed.errorMessage.includes("Maximum usage")) { - console.warn("API limit reached!"); - - return "https://tse2.mm.bing.net/th?id=OIP.SW4X5yWbxOMofUFxMwSTbAHaJQ&pid=Api"; // Return "No cover image" - console.log("unreachable"); - } else { - // Cool, we got an response - posterUrl = - "https://tse2.mm.bing.net/th?id=OIP.SW4X5yWbxOMofUFxMwSTbAHaJQ&pid=Api"; // Just a fallback - if (jsonParsed.posters[0] != undefined) { - posterUrl = jsonParsed.posters[0].link; - // Lets save the response - db.run( - "INSERT INTO apiChache(imdbID, url) VALUES(?, ?)", - [imdbID, posterUrl], - (err) => { - if (err) { - return console.log(err.message); - } - console.log("Saved url to chache"); - } - ); - } - // Return the url - return posterUrl; - } - } else { - // There is a result, lets return the result - return rows[0].url; - } - } - ); - } -} - app.use(express.static("static")); -app.get("/", function (req, res) { +app.get("/", async function handleIndex(req, res) { // https://imdb-api.com/de/API/Posters//[ID HERE] - var buildString = ""; - db.serialize(function () { - db.all("SELECT * FROM movieInfo ORDER BY movieTitel ASC;", function (err, rows) { - /* rows.forEach((row) => { - console.log(row.id + ": " + row.movieTitel); - //var posterLink = getBufferdResponse(1, row.movieID); - posterLink = row.posterUrl; - console.log(posterLink); - - /*var res = requestSync( - "GET", - "https://imdb-api.com/en/API/Posters//" + row.movieID, - {} - ); - const jsonParsed = JSON.parse(res.body); - console.log(jsonParsed); - posterLink = ""; - if (jsonParsed.posters[0] == undefined) { - posterLink = - "https://tse2.mm.bing.net/th?id=OIP.SW4X5yWbxOMofUFxMwSTbAHaJQ&pid=Api"; - } else { - posterLink = jsonParsed.posters[0].link; - }*/ - /*buildString += - "

' + - row.movieTitel + - "
"; - - });*/ - - console.log(rows) - const data = fs.readFileSync("template/bookshelf.html", "utf8"); - res.send(Eta.render(data, {jsonRespo: JSON.stringify(rows)})); - }); - }); + const rows = await prisma.movieInfo.findMany({ + orderBy: [ + { + movieTitel: 'asc', + } + ] + }) + // console.log(rows) + const data = fs.readFileSync("template/bookshelf.html", "utf8"); + res.send(Eta.render(data, { jsonRespo: JSON.stringify(rows) })); }); app.get("/add", function (req, res) { @@ -162,13 +55,13 @@ app.get("/add", function (req, res) { }); app.get("/delete", function (req, res) { - db.run(`DELETE FROM movieInfo WHERE id=?`, req.query["id"], function (err) { - if (err) { - return console.error(err.message); + prisma.movieInfo.delete({ + where: { + movieID: req.query.id } - console.log(`Row(s) deleted ${this.changes}`); - res.send("Done."); - }); + }) + console.log("Deleted entries") + res.send("Done."); }); app.get("/returnSaveResult", function (req, res) { @@ -187,48 +80,32 @@ app.get("/returnSaveResult", function (req, res) { ); }); -app.get("/showDetails", function (req, res) { +app.get("/showDetails", async function (req, res) { const data = fs.readFileSync("template/movieDetails.html", "utf8"); - console.log( req.query["id"]) - db.serialize(function () { - db.all( - "SELECT * FROM movieInfo WHERE movieID='" + req.query["id"] + "'", - function (err, rows) { - //+ req.params["id"] - row = rows[0]; - console.log(row); - - res.send( - Eta.render(data, { - internalID: row.id, - id: row.movieID, - cover: row.posterUrl, - titleByAPI: row.movieTitel, - }) - ); - } - ); - }); - /*responseJson = responseBuffer[req.query["reponseID"]]; - console.log(responseBuffer); + console.log(req.query["id"]) + const movie = await prisma.movieInfo.findFirst({ + where: { + movieID: req.query["id"] + } + }) res.send( Eta.render(data, { - id: responseJson.id, - cover: responseJson.coverImage, - titleByAPI: responseJson.titleByAPI, - ean: responseJson.ean, - isDisplay: true, - responseID: req.query["reponseID"] + internalID: movie.id, + id: movie.movieID, + cover: movie.posterUrl, + titleByAPI: movie.movieTitel, + type: typeIndex[movie.type], }) - );*/ + ) }); app.get("/apiCallBack", function (req, res) { //console.log(req.query); if (!responseBuffer.allBufferd.includes(req.query["ean"])) { request( - "http://localhost:9999/?ean=" + req.query["ean"], + eanConvAdress + "?ean=" + req.query["ean"], function (error, response, body) { + console.log(response, error, body) if (response.statusCode != 200) { console.log("OH NO"); res.send({ state: "ERR_EAN_CONV_NO_200" }); @@ -240,7 +117,7 @@ app.get("/apiCallBack", function (req, res) { searchTerm; request(reqString, function (error, response2, body) { jsonBody = JSON.parse(response2.body); - + console.log(jsonBody.length); if (jsonBody.errorMessage.includes("Maximum usage")) { res.send({ state: "ERR_IMDB_LIMIT_REACHED" }); @@ -289,49 +166,31 @@ app.get("/apiCallBack", function (req, res) { } }); -app.get("/save", function (req, res) { +app.get("/save", async function handleSave(req, res) { responseJson = responseBuffer[req.query["reponseID"]]; + const type = parseInt(req.query["type"]) || 0; - db.all( - "SELECT * FROM movieInfo WHERE movieID='" + responseJson.id + "'", - function (err, rows) { - console.log(rows); - if (rows == undefined || rows.length == 0) { - db.run( - "INSERT INTO movieInfo(id, movieID, movieTitel, place, type, posterUrl) VALUES(?, ?, ?, ?, ?, ?)", - [ - new Date().valueOf(), - responseJson.id, - responseJson.titleByAPI, - 0, - 0, - responseJson.coverImage, - ], - (err) => { - if (err) { - return console.log(err.message); - } - } - ); - res.send({ status: "OK" }); - } else { - res.send({ status: "ERR_ALREADY_EXIST" }); - } + const moveiTest = await prisma.movieInfo.findMany({ + where: { + movieID: responseJson.id } - ); + }) + if(moveiTest.length == 0){ + prisma.movieInfo.create({ + data: { + movieID: responseJson.id, + movieTitel: responseJson.titleByAPI, + place: 0, + type: type, + posterUrl: responseJson.coverImage + } + }) + res.send({ status: "OK" }); + }else{ + res.send({ status: "ERR_ALREADY_EXIST" }); + } }); -//var res = request('POST', 'https://ssl.ofdb.de/view.php?page=suchergebnis', { -// json: {'SText': '4030521376748', 'Kat':'EAN'}, -//}); - -//var temp2 = String(res.getBody('utf8')) -//temp = //temp.replace("