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

58 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/dramas", tags=["drama"], response_model=SearchResponse, summary="Search for dramas") 

14@cache(expire=Config.REDIS_TTL_CACHE) 

15def search_dramas( 

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

17 req: Request 

18) -> SearchResponse: 

19 

20 return search_scrape( 

21 endpoint=Endpoint.SEARCH_DRAMAS, 

22 req=req, 

23 message="Failed to search dramas.", 

24 ) 

25 

26 

27@router.get("/dramas/{drama_series_id}/{drama_season_id}", tags=["drama"], response_model=InfoResponse, summary="Retrieve information about a specific drama") 

28@cache(expire=Config.REDIS_TTL_CACHE) 

29def info_dramas( 

30 drama_series_id: int, 

31 drama_season_id: int, 

32 req: Request 

33) -> InfoResponse: 

34 

35 return info_scrape( 

36 endpoint=Endpoint.INFO_DRAMAS, 

37 req=req, 

38 message=f"Failed to retrieve information for drama with series ID: {drama_series_id} and season ID: {drama_season_id}.", 

39 ) 

40 

41 

42@router.get("/dramas/{drama_series_id}/{drama_season_id}/reviews", tags=["drama"], response_model=InfoResponse, summary="Retrieve user reviews about a specific drama") 

43@cache(expire=Config.REDIS_TTL_CACHE) 

44def review_dramas( 

45 drama_series_id: int, 

46 drama_season_id: int, 

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

48 req: Request 

49) -> InfoResponse: 

50 

51 return review_scrape( 

52 endpoint=Endpoint.REVIEW_DRAMAS, 

53 req=req, 

54 message=f"Failed to retrieve reviews for drama with series ID: {drama_series_id} and season ID: {drama_season_id}.", 

55 ) 

56 

57 

58@router.get("/dramas/{drama_series_id}/{drama_season_id}/reviews/{review_id}", tags=["drama"], response_model=InfoResponse, summary="Retrieve a specific user review about a specific drama") 

59@cache(expire=Config.REDIS_TTL_CACHE) 

60def review_specific_dramas( 

61 drama_series_id: int, 

62 drama_season_id: int, 

63 review_id: int, 

64 req: Request 

65) -> InfoResponse: 

66 

67 return review_scrape( 

68 endpoint=Endpoint.REVIEW_SPECIFIC_DRAMAS, 

69 req=req, 

70 message=f"Failed to retrieve review for drama with series ID: {drama_series_id}, season ID: {drama_season_id}, and review ID: {review_id}.", 

71 ) 

72 

73 

74@router.get("/list-drama/trend", tags=["drama"], response_model=SearchResponse, summary="Fetch currently trending dramas") 

75@cache(expire=Config.REDIS_TTL_CACHE) 

76def list_dramas_trending( 

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

78 req: Request 

79) -> SearchResponse: 

80 

81 return search_scrape( 

82 endpoint=Endpoint.LIST_DRAMAS_TRENDING, 

83 req=req, 

84 message="Failed to fetch trending dramas.", 

85 ) 

86 

87 

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

89@cache(expire=Config.REDIS_TTL_CACHE) 

90def list_dramas_vod( 

91 vod_name: str, 

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

93 req: Request 

94) -> SearchResponse: 

95 

96 return search_scrape( 

97 endpoint=Endpoint.LIST_DRAMAS_VOD, 

98 req=req, 

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

100 ) 

101 

102 

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

104@cache(expire=Config.REDIS_TTL_CACHE) 

105def list_dramas_year_series( 

106 year_series: int, 

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

108 req: Request 

109) -> SearchResponse: 

110 

111 return search_scrape( 

112 endpoint=Endpoint.LIST_DRAMAS_YEAR_SERIES, 

113 req=req, 

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

115 ) 

116 

117 

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

119@cache(expire=Config.REDIS_TTL_CACHE) 

120def list_dramas_year_specific( 

121 year: int, 

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

123 req: Request 

124) -> SearchResponse: 

125 

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

127 

128 return search_scrape( 

129 endpoint=Endpoint.LIST_DRAMAS_YEAR_SPECIFIC, 

130 req=req, 

131 message=f"Failed to fetch dramas from year: {year}.", 

132 ) 

133 

134 

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

136@cache(expire=Config.REDIS_TTL_CACHE) 

137def list_dramas_country( 

138 country_id: int, 

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

140 req: Request 

141) -> SearchResponse: 

142 

143 return search_scrape( 

144 endpoint=Endpoint.LIST_DRAMAS_COUNTRY, 

145 req=req, 

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

147 ) 

148 

149 

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

151@cache(expire=Config.REDIS_TTL_CACHE) 

152def list_dramas_genre( 

153 genre_id: int, 

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

155 req: Request 

156) -> SearchResponse: 

157 

158 return search_scrape( 

159 endpoint=Endpoint.LIST_DRAMAS_GENRE, 

160 req=req, 

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

162 ) 

163 

164 

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

166@cache(expire=Config.REDIS_TTL_CACHE) 

167def list_dramas_tag( 

168 tag: str, 

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

170 req: Request 

171) -> SearchResponse: 

172 

173 return search_scrape( 

174 endpoint=Endpoint.LIST_DRAMAS_TAG, 

175 req=req, 

176 message=f"Failed to fetch dramas with tag: {tag}.", 

177 ) 

178 

179 

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

181@cache(expire=Config.REDIS_TTL_CACHE) 

182def list_dramas_person( 

183 person_id: int, 

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

185 req: Request 

186) -> SearchResponse: 

187 

188 return search_scrape( 

189 endpoint=Endpoint.LIST_DRAMAS_PERSON, 

190 req=req, 

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

192 )