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 : Lesson-10 adding Yafiah Loader provider (code-differently#334)
* Feat : Lesson-10 adding Yafiah Loader provider * fix-up: remove * Fix-Up: removed --------- Co-authored-by: Yafiaha <[email protected]> Co-authored-by: Anthony D. Mays <[email protected]>
- Loading branch information
1 parent
748fdef
commit 903d387
Showing
2 changed files
with
49 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,47 @@ | ||
import csv from 'csv-parser'; | ||
import fs from 'fs'; | ||
import { Credit, MediaItem } from '../models/index.js'; | ||
import { Loader } from './loader.js'; | ||
|
||
export class YafiahAbdullahLoader implements Loader { | ||
getLoaderName(): string { | ||
return 'yafiahabdullah'; | ||
} | ||
|
||
async loadData(): Promise<MediaItem[]> { | ||
const [credits, mediaItems] = await Promise.all([ | ||
this.loadCredits(), | ||
this.loadMediaItems(), | ||
]); | ||
|
||
console.log( | ||
`Loaded ${credits.length} credits and ${mediaItems.length} media items`, | ||
); | ||
|
||
return [...mediaItems.values()]; | ||
} | ||
|
||
async loadMediaItems(): Promise<MediaItem[]> { | ||
const media = []; | ||
const readable = fs | ||
.createReadStream('data/media_items.csv', 'utf-8') | ||
.pipe(csv()); | ||
for await (const row of readable) { | ||
const { id, type, title, year } = row; | ||
media.push(new MediaItem(id, title, type, year, [])); | ||
} | ||
return media; | ||
} | ||
|
||
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; | ||
} | ||
} |