rebuilding some things
This commit is contained in:
BIN
database.sqlite
Normal file
BIN
database.sqlite
Normal file
Binary file not shown.
261
index.js
261
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 +=
|
||||
"<a href='/showDetails?id=" +
|
||||
row.id +
|
||||
"'><div style='width: 150px; float: left; padding: 5px;'><img src='" +
|
||||
posterLink +
|
||||
'\' style="width: 100%;"><br><center><b>' +
|
||||
row.movieTitel +
|
||||
"</b></center></div></a>";
|
||||
|
||||
});*/
|
||||
|
||||
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("<script>", "").replace("<html>", "").replace("\n", "").replace("\r", "").replace("\t", "")
|
||||
//htmlBlock = htmlParser.parse(temp)
|
||||
//temp2 = temp2.split(/\r?\n/)
|
||||
//temp = temp.split('img src="https://ssl.ofdb.de/images/shim.gif" width="1" height="10" border="0" alt=""></td>')[0]
|
||||
//console.log(temp2.find(checkForString))
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Movie DB app listening at http://localhost:${port}`);
|
||||
});
|
||||
|
2545
package-lock.json
generated
2545
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -24,9 +24,14 @@
|
||||
"imdb-api": "^4.4.1",
|
||||
"node-fetch": "^2.6.1",
|
||||
"node-html-parser": "^3.1.2",
|
||||
"prisma": "^3.12.0",
|
||||
"quagga": "^0.12.1",
|
||||
"request": "^2.88.2",
|
||||
"sqlite3": "^5.0.2",
|
||||
"sync-request": "^6.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^17.0.23",
|
||||
"@prisma/client": "^3.12.0"
|
||||
}
|
||||
}
|
||||
|
@ -7,59 +7,91 @@ serverPort = 9999
|
||||
|
||||
class MyServer(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
|
||||
|
||||
print(self.path)
|
||||
if(self.path.startswith("/?ean=")):
|
||||
print("EAN request")
|
||||
|
||||
eanCode = self.path.split("=")[1]
|
||||
print(eanCode)
|
||||
url = 'https://ssl.ofdb.de/view.php?page=suchergebnis'
|
||||
url = 'https://www.ofdb.de/view.php?page=suchergebnis'
|
||||
myobj = {'SText': eanCode, 'Kat':'EAN'} #'4030521376748'
|
||||
|
||||
x = requests.post(url, data = myobj)
|
||||
tme = x.text
|
||||
try:
|
||||
tme = tme.split('<div data-nx-container="inread"><!-- INREAD --></div>')[1]
|
||||
tme = tme.split('img src="https://ssl.ofdb.de/images/shim.gif" width="1" height="10" border="0" alt=""></td>')[0]
|
||||
#tme = tme.split('<div data-nx-container="inread"><!-- INREAD --></div>')[1]
|
||||
#print(tme)
|
||||
tme = tme.split('img src="https://www.ofdb.de/images/shim.gif" width="1" height="10" border="0" alt=""></td>')[0]
|
||||
print(tme)
|
||||
tme = tme.split('<i>Vorab-Einträge:</i>')[1]
|
||||
|
||||
if("Keine Ergebnisse" in tme):
|
||||
print("Non vorab")
|
||||
tme = x.text
|
||||
tme = tme.split('<div data-nx-container="inread"><!-- INREAD --></div>')[1]
|
||||
tme = tme.split('img src="https://ssl.ofdb.de/images/shim.gif" width="1" height="10" border="0" alt=""></td>')[0]
|
||||
print("Alive")
|
||||
tme = tme.split('img src="https://www.ofdb.de/images/shim.gif" width="1" height="10" border="0" alt=""></td>')[0]
|
||||
print("Alive 1")
|
||||
tme = tme.split(",SHADOW,true)\"><b>")[1]
|
||||
print("Alive 2")
|
||||
tme = tme.split("</b></a><br>")[0]
|
||||
print(tme)
|
||||
# print(tme)
|
||||
else:
|
||||
print("Here")
|
||||
tme = tme.split(",SHADOW,true)\"><b>")[1]
|
||||
tme = tme.split("</b></a><br>")[0]
|
||||
#tme = tme.split('</b></a><br>')[0]
|
||||
|
||||
|
||||
tme = tme[:len(tme)-7]
|
||||
if(tme[-5:] in [", Der", ", Die", ", Das"]):
|
||||
tme = tme[-3:] + " " + tme
|
||||
tme = tme[:len(tme)-5]
|
||||
|
||||
except IndexError as ex:
|
||||
self.send_response(500)
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
self.wfile.write(bytes("Invalid EAN", "utf-8"))
|
||||
print(ex)
|
||||
try:
|
||||
tme = tme.split('img src="https://www.ofdb.de/images/shim.gif" width="1" height="10" border="0" alt=""></td>')[0]
|
||||
tme = tme.split('<i>Vorab-Einträge:</i>')[0]
|
||||
tme = tme.split(",SHADOW,true)\"><b>")[1]
|
||||
tme = tme.split("</b></a><br>")[0]
|
||||
tme = tme.split(" (20")[0]
|
||||
print(tme)
|
||||
print("OKAY")
|
||||
except Exception as ex:
|
||||
print(ex)
|
||||
self.send_response(500)
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
self.wfile.write(bytes("Invalid EAN", "utf-8"))
|
||||
print(ex)
|
||||
else:
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "text/plain")
|
||||
self.end_headers()
|
||||
self.wfile.write(bytes(tme, "utf-8"))
|
||||
else:
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "text/plain")
|
||||
self.end_headers()
|
||||
self.wfile.write(bytes(tme, "utf-8"))
|
||||
|
||||
|
||||
|
||||
|
||||
elif(self.path.startswith("/?eanMulti=")):
|
||||
print("EAN request")
|
||||
|
||||
eanCode = self.path.split("=")[1]
|
||||
print(eanCode)
|
||||
url = 'https://ssl.ofdb.de/view.php?page=suchergebnis'
|
||||
url = 'https://www.ofdb.de/view.php?page=suchergebnis'
|
||||
myobj = {'SText': eanCode, 'Kat':'EAN'} #'4030521376748'
|
||||
|
||||
x = requests.post(url, data = myobj)
|
||||
tme = x.text
|
||||
try:
|
||||
tme = tme.split('<div data-nx-container="inread"><!-- INREAD --></div>')[1]
|
||||
tme = tme.split('img src="https://ssl.ofdb.de/images/shim.gif" width="1" height="10" border="0" alt=""></td>')[0]
|
||||
tme = tme.split('img src="https://www.ofdb.de/images/shim.gif" width="1" height="10" border="0" alt=""></td>')[0]
|
||||
#tme = tme.split('width="120" height="168">\',SHADOW,true)"><b>')[1]
|
||||
print(tme)
|
||||
#tme = tme.split(',SHADOW,true)"><b>')[1]
|
||||
@ -79,11 +111,11 @@ class MyServer(BaseHTTPRequestHandler):
|
||||
self.send_response(404)
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
webServer = HTTPServer((hostName, serverPort), MyServer)
|
||||
print("Server started http://%s:%s" % (hostName, serverPort))
|
||||
|
||||
|
24
schema.prisma
Normal file
24
schema.prisma
Normal file
@ -0,0 +1,24 @@
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
url = "file:./database.sqlite"
|
||||
}
|
||||
|
||||
|
||||
model movieInfo {
|
||||
id Int @id @default(autoincrement())
|
||||
movieID String?
|
||||
movieTitel String?
|
||||
place Int
|
||||
type Int
|
||||
posterUrl String?
|
||||
}
|
||||
|
||||
model apiChache {
|
||||
id Int @id @default(autoincrement())
|
||||
imdbID String?
|
||||
url String?
|
||||
}
|
@ -88,6 +88,14 @@
|
||||
<img src="<%= it.cover %>" alt="Cover image" style="width: 100%;"></img><br>
|
||||
<center><%= it.titleByAPI %><br><i><%= it.id %></i></center>
|
||||
</div>
|
||||
|
||||
<select id="type" name="type">
|
||||
<option value="0">Physical</option>
|
||||
<option value="1">Digital - Maxdome</option>
|
||||
<option value="2">Digital - Amazon Prime Video</option>
|
||||
<option value="3">Digital - DVR</option>
|
||||
</select>
|
||||
|
||||
<button onclick="saveToLibary()" class="button-primary" id="addToBtn"> Add to libary</button>
|
||||
<button onclick="window.location.href = '/'"><i class="fas fa-home"></i> Back to home</button>
|
||||
<button
|
||||
@ -115,7 +123,7 @@
|
||||
}
|
||||
|
||||
function saveToLibary(){
|
||||
url = "/save?reponseID=<%= it.ean %>";
|
||||
url = "/save?reponseID=<%= it.ean %>&type=" + document.getElementById("type").value;
|
||||
document.getElementById("addToBtn").remove();
|
||||
$.get(url,function(data,status) {
|
||||
console.log(JSON.parse(data))
|
||||
|
@ -92,7 +92,8 @@
|
||||
<h4>OpenMovieDB - Moviedetails</h4>
|
||||
<div style="width: 300px;">
|
||||
<img src="<%= it.cover %>" alt="Cover image" style="width: 100%;"></img><br>
|
||||
<center><%= it.titleByAPI %><br><i><%= it.id %></i></center>
|
||||
<center><%= it.titleByAPI %><br><i><%= it.id %></i><br>
|
||||
<b>Art: </b> <%= it.type %></center>
|
||||
</div>
|
||||
<button onclick="handelDelete()" id="delBtn"><i class="fas fa-trash"></i> Delete</button>
|
||||
<button onclick="window.location.href = '/'"><i class="fas fa-home"></i> Back to home</button>
|
||||
|
Reference in New Issue
Block a user