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
@@ -7,6 +7,7 @@ const Genre = require('../models/Genre');
|
|||||||
const EntrySeen = require('../models/EntrySeen');
|
const EntrySeen = require('../models/EntrySeen');
|
||||||
const { saveEntry, updateEntry } = require('../services/entryService');
|
const { saveEntry, updateEntry } = require('../services/entryService');
|
||||||
const Season = require('../models/Season');
|
const Season = require('../models/Season');
|
||||||
|
const { bulkImportMovies } = require('../services/importerService');
|
||||||
const entryRouter = express.Router();
|
const entryRouter = express.Router();
|
||||||
|
|
||||||
// ! /entries
|
// ! /entries
|
||||||
@@ -65,7 +66,7 @@ entryRouter.get('/:id', async (req, res) => {
|
|||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// ! POST
|
||||||
// * POST - CREATE ENTRY
|
// * POST - CREATE ENTRY
|
||||||
entryRouter.post('/', async (req, res) => {
|
entryRouter.post('/', async (req, res) => {
|
||||||
const { tmdb_id, is_tvserie } = req.body;
|
const { tmdb_id, is_tvserie } = req.body;
|
||||||
@@ -76,6 +77,20 @@ entryRouter.post('/', async (req, res) => {
|
|||||||
res.status(201).json(entry);
|
res.status(201).json(entry);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// * POST - BULK IMPORT MOVIES
|
||||||
|
entryRouter.post('/bulk-import-movies', async (req, res) => {
|
||||||
|
const { movies } = req.body;
|
||||||
|
|
||||||
|
let entries = [];
|
||||||
|
try {
|
||||||
|
entries = await bulkImportMovies(movies);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(201).json(entries);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// * PUT - ENTRY UPDATE
|
// * PUT - ENTRY UPDATE
|
||||||
entryRouter.put('/:id', async (req, res) => {
|
entryRouter.put('/:id', async (req, res) => {
|
||||||
|
|||||||
@@ -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,
|
||||||
|
};
|
||||||
@@ -15,7 +15,7 @@ async function fetchEntryData(tmdb_id, is_tvserie) {
|
|||||||
|
|
||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('Error fetching the data.')
|
console.log('Error fetching the entry data.', error)
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { MovieDateSeen } from '../../shared/models/dtos/movieDateSeen.model';
|
||||||
|
import { HttpClient } from '@angular/common/http';
|
||||||
|
import { Entry } from '../../shared/models/entry.model';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class ImporterService {
|
||||||
|
private backend_apiUrl = 'http://localhost:4000/api/v1';
|
||||||
|
private apiAction = 'entries';
|
||||||
|
|
||||||
|
constructor(private http: HttpClient) {}
|
||||||
|
bulkAddWithDateSeen(values: string) {
|
||||||
|
const separator = ',';
|
||||||
|
|
||||||
|
// ? converting each line into a MovieDateSeen object
|
||||||
|
let valuesSplited = values.split('\n');
|
||||||
|
let movies = [] as MovieDateSeen[];
|
||||||
|
valuesSplited.forEach((m) => {
|
||||||
|
let movie = {} as MovieDateSeen;
|
||||||
|
|
||||||
|
movie.tmdbId = Number.parseInt(m.split(separator)[0]);
|
||||||
|
movie.dateSeen = new Date(m.split(separator)[1]);
|
||||||
|
movie.userRating = Number.parseInt(m.split(separator)[2]);
|
||||||
|
|
||||||
|
movies.push(movie);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ? consuming the backend API
|
||||||
|
return this.http.post<Entry[]>(
|
||||||
|
`${this.backend_apiUrl}/${this.apiAction}/bulk-import-movies`,
|
||||||
|
{
|
||||||
|
movies: movies,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
export interface MovieDateSeen {
|
||||||
|
tmdbId: number;
|
||||||
|
dateSeen: Date;
|
||||||
|
userRating: number;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user