Coverage for src\routers\movie.py: 100%

82 statements  

« prev     ^ index     » next       coverage.py v7.10.1, created at 2025-10-19 21:13 +0800

1from fastapi import APIRouter, Depends, Request 

2from math import floor 

3from src.scrape.scrape_service import info_scrape, review_scrape, search_scrape 

4from src.utility.config import Config 

5from src.utility.endpoints import Endpoint 

6from src.utility.models import InfoResponse, ReviewParams, ListParams, SearchParams, SearchResponse 

7from src.utility.rediss import cache 

8from typing import Annotated 

9 

10router = APIRouter() 

11 

12 

13@router.get("/search/movies", tags=["movie"], response_model=SearchResponse, summary="Search for movies") 

14@cache(expire=Config.REDIS_TTL_CACHE) 

15def search_movies( 

16 search_params: Annotated[SearchParams, Depends()], 

17 req: Request 

18) -> SearchResponse: 

19 

20 return search_scrape( 

21 endpoint=Endpoint.SEARCH_MOVIES, 

22 req=req, 

23 message="Failed to search movies.", 

24 ) 

25 

26 

27@router.get("/movies/{movie_id}", tags=["movie"], response_model=InfoResponse, summary="Retrieve information about a specific movie") 

28@cache(expire=Config.REDIS_TTL_CACHE) 

29def info_movies( 

30 movie_id: int, 

31 req: Request 

32) -> InfoResponse: 

33 

34 return info_scrape( 

35 endpoint=Endpoint.INFO_MOVIES, 

36 req=req, 

37 message=f"Failed to retrieve information for movie with ID: {movie_id}.", 

38 ) 

39 

40 

41@router.get("/movies/{movie_id}/reviews", tags=["movie"], response_model=InfoResponse, summary="Retrieve user reviews about a specific movie") 

42@cache(expire=Config.REDIS_TTL_CACHE) 

43def review_movies( 

44 movie_id: int, 

45 review_params: Annotated[ReviewParams, Depends()], 

46 req: Request 

47) -> InfoResponse: 

48 

49 return review_scrape( 

50 endpoint=Endpoint.REVIEW_MOVIES, 

51 req=req, 

52 message=f"Failed to retrieve reviews for movie with ID: {movie_id}.", 

53 ) 

54 

55 

56@router.get("/movies/{movie_id}/reviews/{review_id}", tags=["movie"], response_model=InfoResponse, summary="Retrieve a specific user review about a specific movie") 

57@cache(expire=Config.REDIS_TTL_CACHE) 

58def review_specific_movies( 

59 movie_id: int, 

60 review_id: int, 

61 req: Request 

62) -> InfoResponse: 

63 

64 return review_scrape( 

65 endpoint=Endpoint.REVIEW_SPECIFIC_MOVIES, 

66 req=req, 

67 message=f"Failed to retrieve review for movie with ID: {movie_id} and review ID: {review_id}.", 

68 ) 

69 

70 

71@router.get("/list-movie/now", tags=["movie"], response_model=SearchResponse, summary="Fetch movies that are currently screening") 

72@cache(expire=Config.REDIS_TTL_CACHE) 

73def list_movies_currently_screening( 

74 list_params: Annotated[ListParams, Depends()], 

75 req: Request 

76) -> SearchResponse: 

77 

78 return search_scrape( 

79 endpoint=Endpoint.LIST_MOVIES_NOW, 

80 req=req, 

81 message="Failed to fetch currently screening movies.", 

82 ) 

83 

84 

85@router.get("/list-movie/coming-soon", tags=["movie"], response_model=SearchResponse, summary="Fetch movies that are coming soon") 

86@cache(expire=Config.REDIS_TTL_CACHE) 

87def list_movies_coming_soon( 

88 list_params: Annotated[ListParams, Depends()], 

89 req: Request 

90) -> SearchResponse: 

91 

92 return search_scrape( 

93 endpoint=Endpoint.LIST_MOVIES_COMING, 

94 req=req, 

95 message="Failed to fetch upcoming movies.", 

96 ) 

97 

98 

99@router.get("/list-movie/opening-this-week", tags=["movie"], response_model=SearchResponse, summary="Fetch movies that are opening this week") 

100@cache(expire=Config.REDIS_TTL_CACHE) 

101def list_movies_opening_this_week( 

102 list_params: Annotated[ListParams, Depends()], 

103 req: Request 

104) -> SearchResponse: 

105 

106 return search_scrape( 

107 endpoint=Endpoint.LIST_MOVIES_UPCOMING, 

108 req=req, 

109 message="Failed to fetch movies opening this week.", 

110 ) 

111 

112 

113@router.get("/list-movie/trend", tags=["movie"], response_model=SearchResponse, summary="Fetch currently trending movies") 

114@cache(expire=Config.REDIS_TTL_CACHE) 

115def list_movies_trending( 

116 list_params: Annotated[ListParams, Depends()], 

117 req: Request 

118) -> SearchResponse: 

119 

120 return search_scrape( 

121 endpoint=Endpoint.LIST_MOVIES_TRENDING, 

122 req=req, 

123 message="Failed to fetch trending movies.", 

124 ) 

125 

126 

127@router.get("/list-movie/vod/{vod_name}", tags=["movie"], response_model=SearchResponse, summary="Fetch movies available on a specific VOD service") 

128@cache(expire=Config.REDIS_TTL_CACHE) 

129def list_movies_vod( 

130 vod_name: str, 

131 list_params: Annotated[ListParams, Depends()], 

132 req: Request 

133) -> SearchResponse: 

134 

135 return search_scrape( 

136 endpoint=Endpoint.LIST_MOVIES_VOD, 

137 req=req, 

138 message=f"Failed to fetch movies from VOD service: {vod_name}.", 

139 ) 

140 

141 

142@router.get("/list-movie/award/{award_id}", tags=["movie"], response_model=SearchResponse, summary="Fetch movies that received a specific award") 

143@cache(expire=Config.REDIS_TTL_CACHE) 

144def list_movies_award( 

145 award_id: int, 

146 list_params: Annotated[ListParams, Depends()], 

147 req: Request 

148) -> SearchResponse: 

149 

150 return search_scrape( 

151 endpoint=Endpoint.LIST_MOVIES_AWARD, 

152 req=req, 

153 message=f"Failed to fetch movies with award ID: {award_id}.", 

154 ) 

155 

156 

157@router.get("/list-movie/year/{year_series}s", tags=["movie"], response_model=SearchResponse, summary="Fetch movies released in a specific decade") 

158@cache(expire=Config.REDIS_TTL_CACHE) 

159def list_movies_year_series( 

160 year_series: int, 

161 list_params: Annotated[ListParams, Depends()], 

162 req: Request 

163) -> SearchResponse: 

164 

165 return search_scrape( 

166 endpoint=Endpoint.LIST_MOVIES_YEAR_SERIES, 

167 req=req, 

168 message=f"Failed to fetch movies from year series: {year_series}s.", 

169 ) 

170 

171 

172@router.get("/list-movie/year/{year}", tags=["movie"], response_model=SearchResponse, summary="Fetch movies released in a specific year") 

173@cache(expire=Config.REDIS_TTL_CACHE) 

174def list_movies_year_specific( 

175 year: int, 

176 list_params: Annotated[ListParams, Depends()], 

177 req: Request 

178) -> SearchResponse: 

179 

180 req.path_params["year_series"] = floor(year / 10) * 10 

181 

182 return search_scrape( 

183 endpoint=Endpoint.LIST_MOVIES_YEAR_SPECIFIC, 

184 req=req, 

185 message=f"Failed to fetch movies from year: {year}.", 

186 ) 

187 

188 

189@router.get("/list-movie/country/{country_id}", tags=["movie"], response_model=SearchResponse, summary="Fetch movies from a specific country") 

190@cache(expire=Config.REDIS_TTL_CACHE) 

191def list_movies_country( 

192 country_id: int, 

193 list_params: Annotated[ListParams, Depends()], 

194 req: Request 

195) -> SearchResponse: 

196 

197 return search_scrape( 

198 endpoint=Endpoint.LIST_MOVIES_COUNTRY, 

199 req=req, 

200 message=f"Failed to fetch movies with country ID: {country_id}.", 

201 ) 

202 

203 

204@router.get("/list-movie/genre/{genre_id}", tags=["movie"], response_model=SearchResponse, summary="Fetch movies categorised under a specific genre") 

205@cache(expire=Config.REDIS_TTL_CACHE) 

206def list_movies_genre( 

207 genre_id: int, 

208 list_params: Annotated[ListParams, Depends()], 

209 req: Request 

210) -> SearchResponse: 

211 

212 return search_scrape( 

213 endpoint=Endpoint.LIST_MOVIES_GENRE, 

214 req=req, 

215 message=f"Failed to fetch movies with genre ID: {genre_id}.", 

216 ) 

217 

218 

219@router.get("/list-movie/distributor/{distributor_id}", tags=["movie"], response_model=SearchResponse, summary="Fetch movies associated with a specific distributor") 

220@cache(expire=Config.REDIS_TTL_CACHE) 

221def list_movies_distributor( 

222 distributor_id: int, 

223 list_params: Annotated[ListParams, Depends()], 

224 req: Request 

225) -> SearchResponse: 

226 

227 return search_scrape( 

228 endpoint=Endpoint.LIST_MOVIES_DISTRIBUTOR, 

229 req=req, 

230 message=f"Failed to fetch movies with distributor ID: {distributor_id}.", 

231 ) 

232 

233 

234@router.get("/list-movie/series/{series_id}", tags=["movie"], response_model=SearchResponse, summary="Fetch movies categorised under a specific series") 

235@cache(expire=Config.REDIS_TTL_CACHE) 

236def list_movies_series( 

237 series_id: int, 

238 list_params: Annotated[ListParams, Depends()], 

239 req: Request 

240) -> SearchResponse: 

241 

242 return search_scrape( 

243 endpoint=Endpoint.LIST_MOVIES_SERIES, 

244 req=req, 

245 message=f"Failed to fetch movies with series ID: {series_id}.", 

246 ) 

247 

248 

249@router.get("/list-movie/tag/{tag}", tags=["movie"], response_model=SearchResponse, summary="Fetch movies categorised under a specific tag") 

250@cache(expire=Config.REDIS_TTL_CACHE) 

251def list_movies_tag( 

252 tag: str, 

253 list_params: Annotated[ListParams, Depends()], 

254 req: Request 

255) -> SearchResponse: 

256 

257 return search_scrape( 

258 endpoint=Endpoint.LIST_MOVIES_TAG, 

259 req=req, 

260 message=f"Failed to fetch movies with tag: {tag}.", 

261 ) 

262 

263 

264@router.get("/list-movie/person/{person_id}", tags=["movie"], response_model=SearchResponse, summary="Fetch movies linked to a specific person") 

265@cache(expire=Config.REDIS_TTL_CACHE) 

266def list_movies_person( 

267 person_id: int, 

268 list_params: Annotated[ListParams, Depends()], 

269 req: Request 

270) -> SearchResponse: 

271 

272 return search_scrape( 

273 endpoint=Endpoint.LIST_MOVIES_PERSON, 

274 req=req, 

275 message=f"Failed to fetch movies with person ID: {person_id}.", 

276 )