Skip to content

Commit 5aa5796

Browse files
authored
Merge branch 'main' into primary/lesson_10
2 parents 86e5d47 + 6a45e46 commit 5aa5796

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import csv from 'csv-parser';
2+
import fs from 'fs';
3+
import { Credit, MediaItem, Role } from '../models/index.js';
4+
import { Loader } from './loader.js';
5+
6+
export class XavierCruzLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'xaviercruz';
9+
}
10+
11+
async loadData(): Promise<MediaItem[]> {
12+
const credits = await this.loadCredits();
13+
const mediaItems = await this.loadMediaItems();
14+
15+
console.log(
16+
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
17+
);
18+
19+
credits.forEach((credit) => {
20+
const mediaItem = mediaItems.find(
21+
(media) => media.getId() === credit.getMediaItemId(),
22+
);
23+
24+
if (mediaItem) {
25+
mediaItem.addCredit(credit);
26+
}
27+
});
28+
29+
return [...mediaItems.values()];
30+
}
31+
32+
async loadMediaItems(): Promise<MediaItem[]> {
33+
const mediaItems = [];
34+
const readable = fs
35+
.createReadStream('data/media_items.csv', 'utf-8')
36+
.pipe(csv());
37+
38+
for await (const row of readable) {
39+
const { id, type, title, year } = row;
40+
mediaItems.push(new MediaItem(id, title, type, year, []));
41+
}
42+
43+
return mediaItems;
44+
}
45+
46+
async loadCredits(): Promise<Credit[]> {
47+
const filePath = 'data/credits.csv';
48+
const fileContents = fs.readFileSync(filePath, 'utf-8');
49+
50+
const lines = fileContents.split('\n');
51+
const contentByLine = lines.slice(1);
52+
53+
for (let i = 0; i < contentByLine.length; i++) {
54+
contentByLine[i] = contentByLine[i].substring(
55+
contentByLine[i].indexOf(',') + 1,
56+
);
57+
}
58+
59+
// help from ChatGPT - Fixing the roleStr as Role issue
60+
const credits: Credit[] = contentByLine.map((credit) => {
61+
const [mediaItemId, roleStr, name] = credit.split(',');
62+
63+
// cast roleStr to Role type
64+
const role: Role = roleStr as Role;
65+
66+
return new Credit(mediaItemId, name, role);
67+
});
68+
69+
return credits;
70+
}
71+
}

0 commit comments

Comments
 (0)