-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding lesson10 - load media item implementation by Yemi (#333)
* feat: Adding homework10 - load media item implementation - Yemi * feat: updated homework10 - removed all comments by Yemi --------- Co-authored-by: Anthony D. Mays <[email protected]>
- Loading branch information
1 parent
903d387
commit 8d6467e
Showing
2 changed files
with
47 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,45 @@ | ||
import csv from 'csv-parser'; | ||
import fs from 'fs'; | ||
import { Credit, MediaItem } from '../models/index.js'; | ||
import { Loader } from './loader.js'; | ||
|
||
export class OyeyemiJimohLoader implements Loader { | ||
getLoaderName(): string { | ||
return 'oyeyemijimoh'; | ||
} | ||
|
||
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`, | ||
); | ||
|
||
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 credits = []; | ||
const readable = fs | ||
.createReadStream('data/credits.csv', 'utf-8') | ||
.pipe(csv()); | ||
for await (const row of readable) { | ||
const { media_item_id, role, name } = row; | ||
credits.push(new Credit(media_item_id, name, role)); | ||
} | ||
return credits; | ||
} | ||
} |