Coverage for src\routers\anime.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/animes", tags=["anime"], response_model=SearchResponse, summary="Search for animes") 

14@cache(expire=Config.REDIS_TTL_CACHE) 

15def search_animes( 

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

17 req: Request 

18) -> SearchResponse: 

19 

20 return search_scrape( 

21 endpoint=Endpoint.SEARCH_ANIMES, 

22 req=req, 

23 message="Failed to search animes.", 

24 ) 

25 

26 

27@router.get("/animes/{anime_series_id}/{anime_season_id}", tags=["anime"], response_model=InfoResponse, summary="Retrieve information about a specific anime") 

28@cache(expire=Config.REDIS_TTL_CACHE) 

29def info_animes( 

30 anime_series_id: int, 

31 anime_season_id: int, 

32 req: Request 

33) -> InfoResponse: 

34 

35 return info_scrape( 

36 endpoint=Endpoint.INFO_ANIMES, 

37 req=req, 

38 message=f"Failed to retrieve information for anime with series ID: {anime_series_id} and season ID: {anime_season_id}.", 

39 ) 

40 

41 

42@router.get("/animes/{anime_series_id}/{anime_season_id}/reviews", tags=["anime"], response_model=InfoResponse, summary="Retrieve user reviews about a specific anime") 

43@cache(expire=Config.REDIS_TTL_CACHE) 

44def review_animes( 

45 anime_series_id: int, 

46 anime_season_id: int, 

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

48 req: Request 

49) -> InfoResponse: 

50 

51 return review_scrape( 

52 endpoint=Endpoint.REVIEW_ANIMES, 

53 req=req, 

54 message=f"Failed to retrieve reviews for anime with series ID: {anime_series_id} and season ID: {anime_season_id}.", 

55 ) 

56 

57 

58@router.get("/animes/{anime_series_id}/{anime_season_id}/reviews/{review_id}", tags=["anime"], response_model=InfoResponse, summary="Retrieve a specific user review about a specific anime") 

59@cache(expire=Config.REDIS_TTL_CACHE) 

60def review_specific_animes( 

61 anime_series_id: int, 

62 anime_season_id: int, 

63 review_id: int, 

64 req: Request 

65) -> InfoResponse: 

66 

67 return review_scrape( 

68 endpoint=Endpoint.REVIEW_SPECIFIC_ANIMES, 

69 req=req, 

70 message=f"Failed to retrieve review for anime with series ID: {anime_series_id}, season ID: {anime_season_id}, and review ID: {review_id}.", 

71 ) 

72 

73 

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

75@cache(expire=Config.REDIS_TTL_CACHE) 

76def list_animes_trending( 

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

78 req: Request 

79) -> SearchResponse: 

80 

81 return search_scrape( 

82 endpoint=Endpoint.LIST_ANIMES_TRENDING, 

83 req=req, 

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

85 ) 

86 

87 

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

89@cache(expire=Config.REDIS_TTL_CACHE) 

90def list_animes_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_ANIMES_VOD, 

98 req=req, 

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

100 ) 

101 

102 

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

104@cache(expire=Config.REDIS_TTL_CACHE) 

105def list_animes_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_ANIMES_YEAR_SERIES, 

113 req=req, 

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

115 ) 

116 

117 

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

119@cache(expire=Config.REDIS_TTL_CACHE) 

120def list_animes_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_ANIMES_YEAR_SPECIFIC, 

130 req=req, 

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

132 ) 

133 

134 

135@router.get("/list-anime/year/{year}/{season_id}", tags=["anime"], response_model=SearchResponse, summary="Fetch animes released in a specific year and season") 

136@cache(expire=Config.REDIS_TTL_CACHE) 

137def list_animes_year_season( 

138 year: int, 

139 season_id: int, 

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

141 req: Request 

142) -> SearchResponse: 

143 

144 return search_scrape( 

145 endpoint=Endpoint.LIST_ANIMES_YEAR_SEASON, 

146 req=req, 

147 message=f"Failed to fetch animes from year: {year} with season ID: {season_id}.", 

148 ) 

149 

150 

151@router.get("/list-anime/company/{company_id}", tags=["anime"], response_model=SearchResponse, summary="Fetch animes associated with a specific production company") 

152@cache(expire=Config.REDIS_TTL_CACHE) 

153def list_animes_company( 

154 company_id: int, 

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

156 req: Request 

157) -> SearchResponse: 

158 

159 return search_scrape( 

160 endpoint=Endpoint.LIST_ANIMES_COMPANY, 

161 req=req, 

162 message=f"Failed to fetch animes with company ID: {company_id}.", 

163 ) 

164 

165 

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

167@cache(expire=Config.REDIS_TTL_CACHE) 

168def list_animes_tag( 

169 tag: str, 

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

171 req: Request 

172) -> SearchResponse: 

173 

174 return search_scrape( 

175 endpoint=Endpoint.LIST_ANIMES_TAG, 

176 req=req, 

177 message=f"Failed to fetch animes with tag: {tag}.", 

178 ) 

179 

180 

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

182@cache(expire=Config.REDIS_TTL_CACHE) 

183def list_animes_person( 

184 person_id: int, 

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

186 req: Request 

187) -> SearchResponse: 

188 

189 return search_scrape( 

190 endpoint=Endpoint.LIST_ANIMES_PERSON, 

191 req=req, 

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

193 )