-
Notifications
You must be signed in to change notification settings - Fork 25
feat: added Xavier's loader file #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
anthonydmays
merged 14 commits into
code-differently:main
from
XavierCruz5106:feature/lesson_10
Oct 17, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
713b355
feat: started work on Xavier's lesson_10 parse CSV files/finished ext…
XavierCruz5106 78e7b16
feat: manually parsed CSV for credits
XavierCruz5106 4ea9f8a
chore: removed extra variable
XavierCruz5106 633f562
chore: removed hardcoded 100 and replaced with half of media items le…
XavierCruz5106 e3db735
chore: removed out of context/resolved comments
XavierCruz5106 9abb8d6
feat: updated credits to be a class, parsed with new class
XavierCruz5106 693f645
Merge branch 'main' into feature/lesson_10
XavierCruz5106 ac8ea51
fix: fixed credit class
XavierCruz5106 3f6d268
feat: fixed connecting of credits to mediaItems
XavierCruz5106 2dcc619
Merge branch 'code-differently:main' into feature/lesson_10
XavierCruz5106 108aca8
Revert "feat: updated credits to be a class, parsed with new class"
XavierCruz5106 0184e03
feat: credit.ts matches credit.ts in main
XavierCruz5106 1315a11
feat: reworked credit adding to each media item
XavierCruz5106 6293165
chore: formatting/organizing imports loaders.modules
XavierCruz5106 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.