-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: integrate valibot for user ratings and reviews validation #73
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import * as v from 'valibot'; | ||
|
|
||
| export const CSFDColorRatingSchema = v.union([ | ||
| v.literal('bad'), | ||
| v.literal('average'), | ||
| v.literal('good'), | ||
| v.literal('unknown') | ||
| ]); | ||
|
|
||
| export const CSFDStarsSchema = v.union([ | ||
| v.literal(0), | ||
| v.literal(1), | ||
| v.literal(2), | ||
| v.literal(3), | ||
| v.literal(4), | ||
| v.literal(5) | ||
| ]); | ||
|
|
||
| export const CSFDFilmTypesSchema = v.union([ | ||
| v.literal('film'), | ||
| v.literal('TV film'), | ||
| v.literal('pořad'), | ||
| v.literal('seriál'), | ||
| v.literal('divadelní záznam'), | ||
| v.literal('koncert'), | ||
| v.literal('série'), | ||
| v.literal('studentský film'), | ||
| v.literal('amatérský film'), | ||
| v.literal('hudební videoklip'), | ||
| v.literal('epizoda') | ||
| ]); | ||
|
|
||
| export const CSFDUserRatingsSchema = v.object({ | ||
| id: v.number(), | ||
| title: v.string(), | ||
| year: v.number(), | ||
| url: v.string(), | ||
| type: CSFDFilmTypesSchema, | ||
| colorRating: CSFDColorRatingSchema, | ||
| userRating: CSFDStarsSchema, | ||
| userDate: v.string() | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import * as v from 'valibot'; | ||
| import { CSFDColorRatingSchema, CSFDFilmTypesSchema, CSFDStarsSchema } from './user-ratings.schema'; | ||
|
|
||
| export const CSFDUserReviewsSchema = v.object({ | ||
| id: v.number(), | ||
| title: v.string(), | ||
| year: v.number(), | ||
| url: v.string(), | ||
| type: CSFDFilmTypesSchema, | ||
| colorRating: CSFDColorRatingSchema, | ||
| userRating: CSFDStarsSchema, | ||
| userDate: v.string(), | ||
| text: v.string(), | ||
| poster: v.nullable(v.string()) | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import { HTMLElement, parse } from 'node-html-parser'; | ||
| import { flatten, safeParse } from 'valibot'; | ||
| import { CSFDColorRating, CSFDStars } from '../dto/global'; | ||
| import { CSFDUserRatingConfig, CSFDUserRatings } from '../dto/user-ratings'; | ||
| import { CSFDUserRatingsSchema } from '../dto/user-ratings.schema'; | ||
| import { fetchPage } from '../fetchers'; | ||
| import { sleep } from '../helpers/global.helper'; | ||
| import { | ||
|
|
@@ -76,19 +78,37 @@ export class UserRatingsScraper { | |
| for (const el of movies) { | ||
| const type = getUserRatingType(el); | ||
|
|
||
| let shouldProcess = true; | ||
|
|
||
| // Filtering includesOnly | ||
| if (config?.includesOnly?.length) { | ||
| if (config.includesOnly.some((include) => type === include)) { | ||
| films.push(this.buildUserRatings(el)); | ||
| if (!config.includesOnly.some((include) => type === include)) { | ||
| shouldProcess = false; | ||
| } | ||
| // Filter excludes | ||
| } else if (config?.excludes?.length) { | ||
| if (!config.excludes.some((exclude) => type === exclude)) { | ||
| films.push(this.buildUserRatings(el)); | ||
| if (config.excludes.some((exclude) => type === exclude)) { | ||
| shouldProcess = false; | ||
| } | ||
| } | ||
|
|
||
| if (shouldProcess) { | ||
| try { | ||
| const item = this.buildUserRatings(el); | ||
| const result = safeParse(CSFDUserRatingsSchema, item); | ||
| if (result.success) { | ||
| films.push(result.output as CSFDUserRatings); | ||
| } else { | ||
| console.warn( | ||
| `Skipping invalid user rating. Title: ${item.title}, ID: ${item.id}`, | ||
| JSON.stringify(flatten(result.issues)) | ||
| ); | ||
| } | ||
| } catch (e) { | ||
| console.warn( | ||
| `Skipping user rating due to scraping error (DOM change?): ${(e as Error).message}` | ||
| ); | ||
| } | ||
| } else { | ||
| // Without filtering | ||
| films.push(this.buildUserRatings(el)); | ||
| } | ||
|
Comment on lines
78
to
112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The try/catch on lines 96–111 protects Move the Proposed fix for (const el of movies) {
- const type = getUserRatingType(el);
-
- let shouldProcess = true;
-
- // Filtering includesOnly
- if (config?.includesOnly?.length) {
- if (!config.includesOnly.some((include) => type === include)) {
- shouldProcess = false;
- }
- // Filter excludes
- } else if (config?.excludes?.length) {
- if (config.excludes.some((exclude) => type === exclude)) {
- shouldProcess = false;
- }
- }
-
- if (shouldProcess) {
- try {
- const item = this.buildUserRatings(el);
+ try {
+ const type = getUserRatingType(el);
+
+ let shouldProcess = true;
+
+ // Filtering includesOnly
+ if (config?.includesOnly?.length) {
+ if (!config.includesOnly.some((include) => type === include)) {
+ shouldProcess = false;
+ }
+ // Filter excludes
+ } else if (config?.excludes?.length) {
+ if (config.excludes.some((exclude) => type === exclude)) {
+ shouldProcess = false;
+ }
+ }
+
+ if (shouldProcess) {
+ const item = this.buildUserRatings(el, type);
const result = safeParse(CSFDUserRatingsSchema, item);
if (result.success) {
films.push(result.output as CSFDUserRatings);
} else {
console.warn(
`Skipping invalid user rating. Title: ${item.title}, ID: ${item.id}`,
JSON.stringify(flatten(result.issues))
);
}
- } catch (e) {
- console.warn(
- `Skipping user rating due to scraping error (DOM change?): ${(e as Error).message}`
- );
}
+ } catch (e) {
+ console.warn(
+ `Skipping user rating due to scraping error (DOM change?): ${(e as Error).message}`
+ );
}
}And update - private buildUserRatings(el: HTMLElement): CSFDUserRatings {
+ private buildUserRatings(el: HTMLElement, type: string): CSFDUserRatings {
return {
id: getUserRatingId(el),
title: getUserRatingTitle(el),
year: getUserRatingYear(el),
- type: getUserRatingType(el),
+ type,
url: getUserRatingUrl(el),
colorRating: getUserRatingColorRating(el) as CSFDColorRating,
userDate: getUserRatingDate(el),
userRating: getUserRating(el) as CSFDStars
};
}🤖 Prompt for AI Agents |
||
| } | ||
| return films; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.