Repository navigation
Expand file tree
/
Copy pathtypings.ts
More file actions
268 lines (231 loc) · 7.44 KB
/
Copy pathtypings.ts
File metadata and controls
268 lines (231 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
export interface Genre {
id: number
name: string
}
export interface Person {
id: number
name: string
profile_path: string | null
}
/**
* Trending person from TMDB with additional metadata
*/
export interface TrendingPerson extends Person {
known_for_department: string
popularity: number
gender: number
known_for: Content[]
}
export interface CastMember extends Person {
character: string
order: number
}
export interface CrewMember extends Person {
job: string
department: string
}
export interface Credits {
cast: CastMember[]
crew: CrewMember[]
}
export interface ExternalIds {
imdb_id: string | null
facebook_id: string | null
instagram_id: string | null
twitter_id: string | null
}
export interface ProductionCompany {
id: number
name: string
logo_path: string | null
origin_country: string
}
export interface ReleaseDate {
certification: string
iso_639_1: string
release_date: string
type: number
note?: string
}
export interface ReleaseDatesResult {
iso_3166_1: string
release_dates: ReleaseDate[]
}
export interface ContentRating {
iso_3166_1: string
rating: string
}
// Base interface for shared properties between movies and TV shows
export interface BaseContent {
id: number
backdrop_path: string
genre_ids: number[]
genres?: Genre[]
origin_country: string[]
original_language: string
overview: string
popularity: number
poster_path: string
vote_average: number
vote_count: number
tagline?: string
status?: string
production_companies?: ProductionCompany[]
credits?: Credits
external_ids?: ExternalIds
}
// Movie-specific interface
export interface Movie extends BaseContent {
media_type: 'movie'
title: string
original_title: string
release_date: string
runtime?: number
adult?: boolean
budget?: number
revenue?: number
release_dates?: {
results: ReleaseDatesResult[]
}
}
// TV Show-specific interface
export interface TVShow extends BaseContent {
media_type: 'tv'
name: string
original_name: string
first_air_date: string
last_air_date?: string
number_of_seasons?: number
number_of_episodes?: number
episode_run_time?: number[]
content_ratings?: {
results: ContentRating[]
}
created_by?: Person[]
networks?: ProductionCompany[]
}
// Union type for content that can be either movies or TV shows
export type Content = Movie | TVShow
// Type guards for runtime checking
export function isMovie(content: Content): content is Movie {
return content.media_type === 'movie'
}
export function isTVShow(content: Content): content is TVShow {
return content.media_type === 'tv'
}
// Utility functions for consistent access to properties
export function getTitle(content: Content): string {
return isMovie(content) ? content.title : content.name
}
export function getOriginalTitle(content: Content): string {
return isMovie(content) ? content.original_title : content.original_name
}
export function getReleaseDate(content: Content): string {
return isMovie(content) ? content.release_date : content.first_air_date
}
export function getYear(content: Content): string {
// Get the appropriate date field
const date = isMovie(content) ? content.release_date : content.first_air_date
// Extract year from valid date string
if (date && typeof date === 'string' && date.length >= 4) {
const year = date.slice(0, 4)
// Validate it's a reasonable year
const yearNum = parseInt(year)
if (!isNaN(yearNum) && yearNum > 1800 && yearNum < 2100) {
return year
}
}
// If primary date field is missing, return 'Unknown'
return 'Unknown'
}
/**
* Sanitize poster_path to remove any size parameters that may have been incorrectly included
* Ensures poster_path always starts with / followed by the file path (e.g. /abc123.jpg)
*/
export function sanitizePosterPath(path: string | null | undefined): string | null {
if (!path) return null
// If path already includes a size parameter like /w500/ or /original/, extract just the filename
// Match patterns like: /w500/file.jpg, /original/file.jpg, w500/file.jpg, etc.
const sizeMatch = path.match(/\/(w\d+|original)\/(.+)/)
if (sizeMatch) {
return `/${sizeMatch[2]}`
}
// Ensure path starts with /
return path.startsWith('/') ? path : `/${path}`
}
export function getPosterPath(content: Content): string {
const posterPath = sanitizePosterPath(content.poster_path)
if (posterPath) {
return `https://image.tmdb.org/t/p/w500${posterPath}`
}
// Return a placeholder image if no poster is available
return '/placeholder-poster.png'
}
export function getContentType(content: Content): string {
return isMovie(content) ? 'Movie' : 'TV Show'
}
// Utility functions for new metadata
export function getDirector(content: Content): string | null {
if (!content.credits?.crew) return null
const director = content.credits.crew.find((person) => person.job === 'Director')
return director?.name || null
}
export function getDirectorInfo(content: Content): CrewMember | null {
if (!content.credits?.crew) return null
return content.credits.crew.find((person) => person.job === 'Director') || null
}
export function getMainCast(content: Content, limit: number = 5): CastMember[] {
if (!content.credits?.cast) return []
return content.credits.cast.slice(0, limit)
}
export function getGenreNames(content: Content): string[] {
if (content.genres) {
return content.genres.map((genre) => genre.name)
}
return []
}
export function getGenres(content: Content): Genre[] {
return content.genres || []
}
export function getProductionCompanyNames(content: Content): string[] {
if (!content.production_companies) return []
return content.production_companies.map((company) => company.name)
}
export function getRating(content: Content): string | null {
if (isMovie(content) && content.release_dates?.results) {
// Look for US rating first
const usRating = content.release_dates.results.find((r) => r.iso_3166_1 === 'US')
if (usRating?.release_dates && usRating.release_dates.length > 0) {
return usRating.release_dates[0].certification || null
}
} else if (isTVShow(content) && content.content_ratings?.results) {
// Look for US TV rating
const usRating = content.content_ratings.results.find((r) => r.iso_3166_1 === 'US')
return usRating?.rating || null
}
return null
}
export function getRuntime(content: Content): string | null {
if (isMovie(content) && content.runtime) {
const hours = Math.floor(content.runtime / 60)
const minutes = content.runtime % 60
if (hours > 0) {
return `${hours}h ${minutes}m`
}
return `${minutes}m`
} else if (isTVShow(content) && content.episode_run_time?.length) {
const avgRuntime = content.episode_run_time[0]
return `${avgRuntime}m per episode`
}
return null
}
export function getIMDbRating(content: Content): { url: string | null; id: string | null } {
const imdbId = content.external_ids?.imdb_id
return {
id: imdbId || null,
url: imdbId ? `https://www.imdb.com/title/${imdbId}` : null,
}
}
export interface Element {
type: 'Bloopers' | 'Featurette' | 'Behind the Scenes' | 'Clip' | 'Trailer' | 'Teaser'
}