52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
const EntryRating = require("../models/EntryRating");
|
|
const EntrySeen = require("../models/EntrySeen");
|
|
const { saveEntry } = require("./entryService");
|
|
const { fetchEntryData } = require("./tmdbService");
|
|
|
|
async function bulkImportMovies(movies) {
|
|
const maxFetches = 35; // ! max fetch per 10 seconds
|
|
const fetchInterval = 500;
|
|
const waitTime = 10000;
|
|
|
|
let entries = [];
|
|
let overAllFetches = 0
|
|
let currenFetch = 0;
|
|
for (const movie of movies) {
|
|
const data = await fetchEntryData(movie.tmdbId, false);
|
|
if (data == null) {
|
|
console.log(`${currenFetch} data null for id: ${movie.id}`);
|
|
}
|
|
const entry = await saveEntry(data);
|
|
|
|
// ? adding date seen
|
|
await EntrySeen.create({ entry_id: entry.id, date_seen: movie.dateSeen, seen_with: "" });
|
|
|
|
// ? adding entry user rating
|
|
if (movie.userRating != 0) {
|
|
await EntryRating.update({ user_rating: movie.userRating }, {
|
|
where: {
|
|
entry_id: entry.id
|
|
}
|
|
});
|
|
}
|
|
|
|
entries.push(entry)
|
|
await new Promise(resolve => setTimeout(resolve, fetchInterval));
|
|
console.log(`${currenFetch} imported id: ${movie.id}`);
|
|
currenFetch++;
|
|
overAllFetches++;
|
|
if (overAllFetches == 50) break;
|
|
|
|
if (currenFetch == maxFetches) {
|
|
console.log(`Now waiting ${waitTime / 1000} seconds...`);
|
|
await new Promise(resolve => setTimeout(resolve, waitTime));
|
|
currentFetch = 0;
|
|
}
|
|
};
|
|
|
|
return entries;
|
|
}
|
|
|
|
module.exports = {
|
|
bulkImportMovies,
|
|
}; |