Skip to content

Commit 2e376eb

Browse files
committed
improved parsing
1 parent 95b486b commit 2e376eb

File tree

2 files changed

+111
-21
lines changed

2 files changed

+111
-21
lines changed

src/main/main.ts

+109-19
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ const devpassword = 'diFay35WElL';
3333
const softname = 'EmuDeckROMLauncher';
3434

3535
app.commandLine.appendSwitch('disk-cache-size', '10737418240');
36-
if (os.platform() === 'linux') {
37-
app.commandLine.appendSwitch('--no-sandbox');
38-
}
36+
app.commandLine.appendSwitch('--no-sandbox');
3937

4038
let theme;
4139
ipcMain.on('get-theme', async (event, name) => {
@@ -157,6 +155,7 @@ if (!fs.existsSync(dbPathLibrary)) {
157155
"played" INTEGER DEFAULT 0,
158156
"path" TEXT,
159157
"databaseID" INTEGER,
158+
"parsed" INTEGER,
160159
"favourite" INTEGER DEFAULT 0,
161160
PRIMARY KEY("id" AUTOINCREMENT),
162161
UNIQUE("file_name")
@@ -905,13 +904,15 @@ const insertROM = (
905904
results.forEach((result) => {
906905
imageData.databaseID = result.DatabaseID;
907906
});
907+
imageData.parsed = 1;
908908
} else {
909909
imageData.databaseID = 0;
910+
imageData.parsed = 0;
910911
}
911912

912913
const insertQuery = `
913-
INSERT OR REPLACE INTO roms (file_name, name, system, platform, path, databaseID)
914-
VALUES (?, ?, ?, ?, ?, ?)
914+
INSERT OR REPLACE INTO roms (file_name, name, system, platform, path, databaseID, parsed)
915+
VALUES (?, ?, ?, ?, ?, ?, ?)
915916
`;
916917

917918
// console.log({ imageData });
@@ -924,6 +925,7 @@ const insertROM = (
924925
imageData.platform,
925926
imageData.gameFilePath,
926927
imageData.databaseID,
928+
imageData.parsed,
927929
],
928930
function (err) {
929931
if (err) {
@@ -1173,12 +1175,52 @@ async function getGameIdByName(gameName, system) {
11731175
jsonData.response.jeux.length > 0
11741176
) {
11751177
const gameId = jsonData.response.jeux[0].id;
1176-
console.log('ID del Juego encontrado:', gameId);
1177-
// Construimos la URL de la imagen y resolvemos la promesa con ella
1178+
// console.log('ID del Juego encontrado:', gameId);
11781179
if (gameId) {
1179-
resolve(
1180-
`https://www.screenscraper.fr/image.php?gameid=${gameId}&media=ss&hd=0&region=wor&num=&version=&maxwidth=338&maxheight=190`,
1181-
);
1180+
const urlSS = `https://www.screenscraper.fr/image.php?gameid=${gameId}&media=ss&hd=0&region=wor&num=&version=&maxwidth=338`;
1181+
// We check if the url exists
1182+
https
1183+
.get(urlSS, (res) => {
1184+
res.on('data', (chunk) => {
1185+
data += chunk;
1186+
});
1187+
1188+
res.on('end', () => {
1189+
if (data.length > 1000) {
1190+
resolve(
1191+
`https://www.screenscraper.fr/image.php?gameid=${gameId}&media=ss&hd=0&region=wor&num=&version=&maxwidth=338`,
1192+
);
1193+
} else {
1194+
// We retry with other region
1195+
urlSS = `https://www.screenscraper.fr/image.php?gameid=${gameId}&media=ss&hd=0&region=usa&num=&version=&maxwidth=338`;
1196+
// We check if the url exists
1197+
https
1198+
.get(urlSS, (res) => {
1199+
res.on('data', (chunk) => {
1200+
data += chunk;
1201+
});
1202+
res.on('end', () => {
1203+
if (data.length > 1000) {
1204+
resolve(
1205+
`https://www.screenscraper.fr/image.php?gameid=${gameId}&media=ss&hd=0&region=usa&num=&version=&maxwidth=338`,
1206+
);
1207+
} else {
1208+
console.log('IMG not found');
1209+
resolve(false);
1210+
}
1211+
});
1212+
})
1213+
.on('error', (err) => {
1214+
console.log(`Error: ${err.message}`);
1215+
});
1216+
1217+
resolve(false);
1218+
}
1219+
});
1220+
})
1221+
.on('error', (err) => {
1222+
console.log(`Error: ${err.message}`);
1223+
});
11821224
} else {
11831225
resolve(null);
11841226
}
@@ -1208,10 +1250,11 @@ ipcMain.on('ss-artwork', async (event, system) => {
12081250
SELECT DISTINCT
12091251
path,
12101252
name,
1253+
file_name,
12111254
system
12121255
FROM roms
12131256
WHERE system = ?
1214-
AND databaseID = 0
1257+
AND parsed = 0
12151258
GROUP BY name
12161259
`;
12171260

@@ -1226,25 +1269,72 @@ ipcMain.on('ss-artwork', async (event, system) => {
12261269
const resultsArray = rows.map((row) => ({ ...row }));
12271270

12281271
// We run through the array to get the missing pictures
1229-
async function main(game, system) {
1272+
async function lookForGame(game, system) {
12301273
try {
12311274
const imageUrl = await getGameIdByName(game, system);
1232-
return imageUrl;
1275+
if (imageUrl) {
1276+
return imageUrl;
1277+
}
1278+
return false;
12331279
} catch (error) {
12341280
console.error('Error obteniendo la URL de la imagen:', error);
12351281
}
12361282
}
12371283

12381284
if (resultsArray.length > 0) {
12391285
resultsArray.forEach((result) => {
1240-
main(result.name, result.system)
1286+
const romName = result.file_name;
1287+
1288+
let romNameTrimmed = romName
1289+
.replace(/\.nkit/g, '')
1290+
.replace(/!/g, '')
1291+
.replace(/Disc /g, '')
1292+
.replace(/Rev /g, '')
1293+
.replace(/\([^()]*\)/g, '')
1294+
.replace(/\[[A-z0-9!+]*\]/g, '')
1295+
.replace(/ \./g, '.');
1296+
1297+
romNameTrimmed = romNameTrimmed.replace(/\..*/, '');
1298+
console.log({ romNameTrimmed });
1299+
lookForGame(romNameTrimmed, result.system)
12411300
.then((imageUrl) => {
1242-
console.log('URL de la imagen:', imageUrl);
1243-
result.screenshot = imageUrl;
1301+
if (imageUrl) {
1302+
console.log('URL de la imagen:', imageUrl);
1303+
result.screenshot = imageUrl;
1304+
// We mark the game as scraped
1305+
1306+
// SQLITE UPDATE
1307+
dbLibrary.run(
1308+
`UPDATE roms
1309+
SET parsed = 1
1310+
WHERE name = ? and system = ?`,
1311+
[result.name, result.system],
1312+
function (err) {
1313+
if (err) {
1314+
return console.error(err.message);
1315+
}
1316+
console.log(`Filas actualizadas: ${this.changes}`);
1317+
},
1318+
);
12441319

1245-
// console.log({ resultsArray });
1246-
resultsJSON = JSON.stringify(resultsArray, null, 2);
1247-
event.reply('ss-artwork', resultsJSON);
1320+
// console.log({ resultsArray });
1321+
resultsJSON = JSON.stringify(resultsArray, null, 2);
1322+
event.reply('ss-artwork', resultsJSON);
1323+
} else {
1324+
// SQLITE UPDATE
1325+
dbLibrary.run(
1326+
`UPDATE roms
1327+
SET parsed = 2
1328+
WHERE name = ? and system = ?`,
1329+
[result.name, result.system],
1330+
function (err) {
1331+
if (err) {
1332+
return console.error(err.message);
1333+
}
1334+
console.log(`Filas actualizadas: ${this.changes}`);
1335+
},
1336+
);
1337+
}
12481338
})
12491339
.catch((error) => {
12501340
console.error('Error obteniendo la URL de la imagen:', error);

src/pages/GamesPage.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function GamesPage({ focusKey: focusKeyParam }) {
8282
const gamesArray = Object.values(json);
8383
setStatePage({ ...statePage, games: gamesArray });
8484
console.log('Cache restored');
85-
// askForArtwork(system, cache);
85+
askForArtwork(system, cache);
8686
} else {
8787
console.log('ask for games');
8888
ipcChannel.sendMessage(`get-games`, system);
@@ -95,7 +95,7 @@ function GamesPage({ focusKey: focusKeyParam }) {
9595
console.log('games to state');
9696
setStatePage({ ...statePage, games: gamesArray });
9797
console.log('games loaded');
98-
// askForArtwork(system, gamesTemp);
98+
askForArtwork(system, gamesTemp);
9999
});
100100
}
101101
}, []);

0 commit comments

Comments
 (0)