Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

"""LICENSE 

Copyright 2018 Hermann Krumrey <hermann@krumreyh.com> 

 

This file is part of anime-list-apis. 

 

anime-list-apis is free software: you can redistribute it and/or modify 

it under the terms of the GNU General Public License as published by 

the Free Software Foundation, either version 3 of the License, or 

(at your option) any later version. 

 

anime-list-apis is distributed in the hope that it will be useful, 

but WITHOUT ANY WARRANTY; without even the implied warranty of 

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

GNU General Public License for more details. 

 

You should have received a copy of the GNU General Public License 

along with anime-list-apis. If not, see <http://www.gnu.org/licenses/>. 

LICENSE""" 

 

from typing import Dict, List, Tuple, Set, Optional 

 

from anime_list_apis.models.CacheAble import CacheAble, CacheModelType 

from anime_list_apis.models.Serializable import MediaSerializable 

from anime_list_apis.models.attributes.Id import Id 

from anime_list_apis.models.attributes.ReleasingStatus import ReleasingStatus 

from anime_list_apis.models.attributes.MediaType import MediaType 

from anime_list_apis.models.attributes.Score import ScoreType 

from anime_list_apis.models.attributes.ConsumingStatus import ConsumingStatus 

from anime_list_apis.models.MediaData import \ 

AnimeData, MangaData, MediaData 

from anime_list_apis.models.MediaUserData import \ 

AnimeUserData, MangaUserData, MediaUserData 

 

 

class MediaListEntry(MediaSerializable, CacheAble): 

""" 

Class that models a user's media list entry 

""" 

 

def get_id(self) -> Id: 

""" 

Retrieves the cache entry's ID 

:return: The ID 

""" 

return self.id 

 

def get_media_type(self) -> MediaType: 

""" 

Retrieves the media type 

:return: The media type 

""" 

return self.media_type 

 

def get_username(self) -> Optional[str]: 

""" 

Retrieves the username, if applicable. Else None 

:return: The username or None if not applicable 

""" 

return self.username 

 

def get_model_type(self) -> CacheModelType: 

""" 

Retrieves the cache model type 

:return: The model type 

""" 

return CacheModelType.MEDIA_LIST_ENTRY 

 

def __init__(self, media_type: MediaType, 

media_data: MediaData, user_data: MediaUserData): 

""" 

Initializes the media list entry. 

:param media_data: The media data 

:param user_data: The user data 

:raises TypeError: If any of the parameters has a wrong type 

:raises ValueError: If the media and user data do not match 

""" 

self.ensure_type(media_data, 

MediaData.get_class_for_media_type(media_type)) 

self.ensure_type(user_data, 

MediaUserData.get_class_for_media_type(media_type)) 

 

if media_data.id != user_data.id \ 

or media_data.media_type != user_data.media_type \ 

or media_type != media_data.media_type: 

raise ValueError("Mismatching User and Media Data") 

 

self.media_type = media_type 

self.format = media_data.format 

self.id = media_data.id 

self.title = media_data.title 

self.relations = media_data.relations 

self.releasing_status = media_data.releasing_status 

self.releasing_start = media_data.releasing_start 

self.releasing_end = media_data.releasing_end 

self.cover_url = media_data.cover_url 

 

self.username = user_data.username 

self.score = user_data.score 

self.consuming_status = user_data.consuming_status 

self.consuming_start = user_data.consuming_start 

self.consuming_end = user_data.consuming_end 

 

self.__media_data_cls = type(media_data) 

self.__user_data_cls = type(user_data) 

 

def get_media_data(self) -> MediaData: 

""" 

Generates a new MediaData object from the internal representation 

:return: The generated MediaData object 

:raises TypeError: If any of the internal parameters has a wrong type 

""" 

raise NotImplementedError() # pragma: no cover 

 

def get_user_data(self) -> MediaUserData: 

""" 

Generates a new MediaUserData object from the internal representation 

:return: The generated MediaUserData object 

:raises TypeError: If any of the internal parameters has a wrong type 

""" 

raise NotImplementedError() # pragma: no cover 

 

def is_valid_entry(self) -> bool: 

""" 

Checks if the data has a valid combination of entry data 

:return: True, if all required attributes are valid and present 

""" 

score_zero = self.score.get(ScoreType.PERCENTAGE) == 0 

begin_none = self.consuming_start is None 

complete_none = self.consuming_end is None 

consuming_complete = self.consuming_status == ConsumingStatus.COMPLETED 

started_watching = self.consuming_status in [ 

ConsumingStatus.CURRENT, 

ConsumingStatus.PAUSED, 

ConsumingStatus.DROPPED 

] 

 

if not self.get_user_data().is_valid_entry(): 

return False 

 

if self.releasing_status in [ 

ReleasingStatus.FINISHED 

]: 

return True 

 

elif self.releasing_status in [ 

ReleasingStatus.RELEASING, ReleasingStatus.CANCELLED, 

]: 

return complete_none and score_zero and not consuming_complete 

 

else: # AiringStatus.NOT_RELEASESD 

return begin_none and complete_none and score_zero \ 

and not consuming_complete and not started_watching 

 

@classmethod 

def get_class_for_media_type(cls, media_type: MediaType): 

""" 

Maps a class to a media type 

:param media_type: The media type 

:return: The class mapped to that media type 

""" 

return AnimeListEntry \ 

if media_type == MediaType.ANIME \ 

else MangaListEntry 

 

def _serialize(self) -> Dict[str, Optional[str or int or float or bool 

or Dict or List or Tuple or Set]]: 

""" 

Serializes the object into a dictionary 

:return: The serialized form of this object 

""" 

return { 

"media_type": self.media_type.name, 

"media_data": self.get_media_data().serialize(), 

"user_data": self.get_user_data().serialize() 

} 

 

@classmethod 

def _get_common_deserialized_components( 

cls, 

data: Dict[str, Optional[str or int or float or bool or 

Dict or List or Tuple or Set]]) \ 

-> Dict[str, Optional[str or int or float or bool or 

Dict or List or Tuple or Set]]: 

""" 

Deserializes the common child components of the data dictionary 

:param data: The data to deserialize 

:return: The deserialized dictionary 

""" 

deserialized = { 

"media_type": MediaType[data["media_type"]], 

"media_data": MediaData.deserialize(data["media_data"]), 

"user_data": MediaUserData.deserialize(data["user_data"]) 

} 

return deserialized 

 

@classmethod 

def _get_specific_deserialized_components( 

cls, 

data: Dict[str, Optional[str or int or float or bool or 

Dict or List or Tuple or Set]]) \ 

-> Dict[str, Optional[str or int or float or bool or 

Dict or List or Tuple or Set]]: 

""" 

Deserializes class-specific child components of the data dictionary 

:param data: The data to deserialize 

:return: The deserialized dictionary 

""" 

return {} 

 

@classmethod 

def _get_common_parameter_order(cls) -> List[str]: 

""" 

Generates an order of constructor parameters for the common attributes 

used for media classes 

:return: The order of common parameters 

""" 

return ["media_data", "user_data"] 

 

@classmethod 

def _get_additional_parameter_order(cls) -> List[str]: 

""" 

Generates the order of class-specific additional constructor parameters 

:return: The order of the additional parameters 

""" 

return [] 

 

 

class AnimeListEntry(MediaListEntry): 

""" 

Class that models a user's anime list entry 

""" 

 

def __init__(self, anime_data: AnimeData, user_data: AnimeUserData): 

""" 

Initializes the anime list entry. 

:param anime_data: The anime data 

:param user_data: The user data 

:raises TypeError: If any of the parameters has a wrong type 

""" 

super().__init__(MediaType.ANIME, anime_data, user_data) 

self.episode_count = anime_data.episode_count 

self.episode_duration = anime_data.episode_duration 

self.episode_progress = user_data.episode_progress 

 

def get_media_data(self) -> AnimeData: 

""" 

Generates a new AnimeData object from the internal representation 

:return: The generated AnimeData object 

:raises TypeError: If any of the internal parameters has a wrong type 

""" 

return AnimeData( 

self.format, 

self.id, 

self.title, 

self.relations, 

self.releasing_status, 

self.releasing_start, 

self.releasing_end, 

self.cover_url, 

self.episode_count, 

self.episode_duration 

) 

 

def get_user_data(self) -> AnimeUserData: 

""" 

Generates a new AnimeUserData object from the internal representation 

:return: The generated AnimeUserData object 

:raises TypeError: If any of the internal parameters has a wrong type 

""" 

return AnimeUserData( 

self.id, 

self.username, 

self.score, 

self.consuming_status, 

self.consuming_start, 

self.consuming_end, 

self.episode_progress 

) 

 

 

class MangaListEntry(MediaListEntry): 

""" 

Class that models a user's manga list entry 

""" 

 

def __init__(self, manga_data: MangaData, user_data: MangaUserData): 

""" 

Initializes the manga list entry. 

:param manga_data: The manga data 

:param user_data: The user data 

:raises TypeError: If any of the parameters has a wrong type 

""" 

super().__init__(MediaType.MANGA, manga_data, user_data) 

self.chapter_count = manga_data.chapter_count 

self.volume_count = manga_data.volume_count 

self.chapter_progress = user_data.chapter_progress 

self.volume_progress = user_data.volume_progress 

 

def get_media_data(self) -> MangaData: 

""" 

Generates a new MangaData object from the internal representation 

:return: The generated MangaData object 

:raises TypeError: If any of the internal parameters has a wrong type 

""" 

return MangaData( 

self.format, 

self.id, 

self.title, 

self.relations, 

self.releasing_status, 

self.releasing_start, 

self.releasing_end, 

self.cover_url, 

self.chapter_count, 

self.volume_count 

) 

 

def get_user_data(self) -> MangaUserData: 

""" 

Generates a new MangaUserData object from the internal representation 

:return: The generated MangaUserData object 

:raises TypeError: If any of the internal parameters has a wrong type 

""" 

return MangaUserData( 

self.id, 

self.username, 

self.score, 

self.consuming_status, 

self.consuming_start, 

self.consuming_end, 

self.chapter_progress, 

self.volume_progress 

)