forked from code-differently/code-differently-24-q4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added Xavier's CSV loader (code-differently#331)
* feat: started work on Xavier's lesson_10 parse CSV files/finished extra credit 1 * feat: manually parsed CSV for credits * chore: removed extra variable * chore: removed hardcoded 100 and replaced with half of media items length * chore: removed out of context/resolved comments * feat: updated credits to be a class, parsed with new class * fix: fixed credit class * feat: fixed connecting of credits to mediaItems * Revert "feat: updated credits to be a class, parsed with new class" This reverts commit 9abb8d6. * feat: credit.ts matches credit.ts in main * feat: reworked credit adding to each media item * chore: formatting/organizing imports loaders.modules
- Loading branch information
1 parent
bbf5a7b
commit 6a45e46
Showing
2 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import csv from 'csv-parser'; | ||
import fs from 'fs'; | ||
import { Credit, MediaItem, Role } from '../models/index.js'; | ||
import { Loader } from './loader.js'; | ||
|
||
export class XavierCruzLoader implements Loader { | ||
getLoaderName(): string { | ||
return 'xaviercruz'; | ||
} | ||
|
||
async loadData(): Promise<MediaItem[]> { | ||
const credits = await this.loadCredits(); | ||
const mediaItems = await this.loadMediaItems(); | ||
|
||
console.log( | ||
`Loaded ${credits.length} credits and ${mediaItems.length} media items`, | ||
); | ||
|
||
credits.forEach((credit) => { | ||
const mediaItem = mediaItems.find( | ||
(media) => media.getId() === credit.getMediaItemId(), | ||
); | ||
|
||
if (mediaItem) { | ||
mediaItem.addCredit(credit); | ||
} | ||
}); | ||
|
||
return [...mediaItems.values()]; | ||
} | ||
|
||
async loadMediaItems(): Promise<MediaItem[]> { | ||
const mediaItems = []; | ||
const readable = fs | ||
.createReadStream('data/media_items.csv', 'utf-8') | ||
.pipe(csv()); | ||
|
||
for await (const row of readable) { | ||
const { id, type, title, year } = row; | ||
mediaItems.push(new MediaItem(id, title, type, year, [])); | ||
} | ||
|
||
return mediaItems; | ||
} | ||
|
||
async loadCredits(): Promise<Credit[]> { | ||
const filePath = 'data/credits.csv'; | ||
const fileContents = fs.readFileSync(filePath, 'utf-8'); | ||
|
||
const lines = fileContents.split('\n'); | ||
const contentByLine = lines.slice(1); | ||
|
||
for (let i = 0; i < contentByLine.length; i++) { | ||
contentByLine[i] = contentByLine[i].substring( | ||
contentByLine[i].indexOf(',') + 1, | ||
); | ||
} | ||
|
||
// help from ChatGPT - Fixing the roleStr as Role issue | ||
const credits: Credit[] = contentByLine.map((credit) => { | ||
const [mediaItemId, roleStr, name] = credit.split(','); | ||
|
||
// cast roleStr to Role type | ||
const role: Role = roleStr as Role; | ||
|
||
return new Credit(mediaItemId, name, role); | ||
}); | ||
|
||
return credits; | ||
} | ||
} |