started implementing bulk csv import
This commit is contained in:
1 parent
317ad928ea
commit
36866dabe6
5 files changed
+112
-2
No files matched your search
@@ -0,0 +1,52 @@
|
||||
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,
|
||||
};
|
||||
Reference in new issue
Block a user