Skip to content

Commit

Permalink
chore: refine daily challenge generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mgramigna committed Feb 2, 2025
1 parent 52e4dd4 commit a580a0a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/tmdb/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export class TMDBClient {
path: "/movie/popular",
query: new URLSearchParams({
page: page.toString(),
language: "en-US",
}).toString(),
});

Expand Down Expand Up @@ -192,6 +193,7 @@ export class TMDBClient {
path: "/person/popular",
query: new URLSearchParams({
page: page.toString(),
language: "en-US",
}).toString(),
});

Expand Down
11 changes: 11 additions & 0 deletions packages/tmdb/src/types/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ export const PersonSearchResultSchema = BaseSearchResultSchema.extend({
.object({
id: z.number(),
name: z.string(),
original_name: z.string().optional(),
adult: z.boolean().optional(),
known_for: z
.array(
z.object({
id: z.number(),
media_type: z.enum(["movie", "tv"]).optional(),
adult: z.boolean().optional(),
}),
)
.optional(),
})
.array(),
});
Expand Down
12 changes: 10 additions & 2 deletions packages/www/app/lib/api/movie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const movie = {
// Choose between the first 10 pages of popular people
const randomPage = Math.floor(Math.random() * 10) + 1;

const result = await tmdb.discoverMovies({
const result = await tmdb.searchPopularMovies({
page: randomPage,
});

Expand All @@ -41,6 +41,14 @@ export const movie = {

const { results } = result.value;

return results[Math.floor(Math.random() * results.length)];
const filteredResults = results.filter(
(movie) => movie.original_language === "en" && !movie.adult,
);

if (filteredResults.length === 0) {
return results[0];
}

return filteredResults[Math.floor(Math.random() * filteredResults.length)];
},
};
13 changes: 12 additions & 1 deletion packages/www/app/lib/api/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ export const person = {

const { results } = result.value;

return results[Math.floor(Math.random() * results.length)];
const filteredResults = results.filter(
(person) =>
person.name === person.original_name &&
!person.adult &&
person.known_for?.at(0)?.media_type === "movie",
);

if (filteredResults.length === 0) {
return results[0];
}

return filteredResults[Math.floor(Math.random() * filteredResults.length)];
},
};

0 comments on commit a580a0a

Please sign in to comment.