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

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

"""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.Date import Date 

from anime_list_apis.models.attributes.Id import Id 

from anime_list_apis.models.attributes.MediaType import MediaType 

from anime_list_apis.models.attributes.MediaFormat import MediaFormat 

from anime_list_apis.models.attributes.Title import Title 

from anime_list_apis.models.attributes.Relation import Relation 

from anime_list_apis.models.attributes.ReleasingStatus import ReleasingStatus 

 

 

# noinspection PyAbstractClass 

class MediaData(MediaSerializable, CacheAble): 

""" 

Class that models user-independent media data 

""" 

 

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 None 

 

def get_model_type(self) -> CacheModelType: 

""" 

Retrieves the cache model type 

:return: The model type 

""" 

return CacheModelType.MEDIA_DATA 

 

def __init__( 

self, 

media_type: MediaType, 

_format: MediaFormat, 

_id: Id, 

title: Title, 

relations: List[Relation], 

releasing_status: ReleasingStatus, 

releasing_start: Optional[Date], 

releasing_end: Optional[Date], 

cover_url: Optional[str] 

): 

""" 

Initializes the Media Data object and checks for type issues 

Some values may be None, for example if a media has not completed 

releasing yet. 

This constructor initializes all common values of all media types. 

:param media_type: The type of media 

:param _format: The media format 

:param _id: The ID of the media 

:param title: The title of the media 

:param relations: The relations of the media to other media 

:param releasing_status: The releasing status of the media 

:param releasing_start: The day the media started releasing 

:param releasing_end: The day the last content was released 

:param cover_url: An URL pointing to a cover image for the anime 

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

""" 

self.ensure_type(media_type, MediaType) 

self.ensure_type(_format, MediaFormat) 

self.ensure_type(_id, Id) 

self.ensure_type(title, Title) 

self.ensure_type(relations, list) 

list(map(lambda x: self.ensure_type(x, Relation), relations)) 

self.ensure_type(releasing_status, ReleasingStatus) 

self.ensure_type(releasing_start, Date, True) 

self.ensure_type(releasing_end, Date, True) 

self.ensure_type(cover_url, str, True) 

 

self.media_type = media_type 

self.format = _format 

self.id = _id 

self.title = title 

self.relations = relations 

self.releasing_status = releasing_status 

self.releasing_start = releasing_start 

self.releasing_end = releasing_end 

self.cover_url = cover_url 

 

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 

""" 

releasing_start = None if self.releasing_start is None \ 

else self.releasing_start.serialize() 

releasing_end = None if self.releasing_end is None \ 

else self.releasing_end.serialize() 

 

return { 

"media_type": self.media_type.name, 

"format": self.format.name, 

"id": self.id.serialize(), 

"title": self.title.serialize(), 

"relations": list(map(lambda x: x.serialize(), self.relations)), 

"releasing_status": self.releasing_status.name, 

"releasing_start": releasing_start, 

"releasing_end": releasing_end, 

"cover_url": self.cover_url 

} 

 

@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 AnimeData if media_type == MediaType.ANIME else MangaData 

 

@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"]], 

"format": MediaFormat[data["format"]], 

"id": Id.deserialize(data["id"]), 

"title": Title.deserialize(data["title"]), 

"relations": list(map( 

lambda x: Relation.deserialize(x), 

data["relations"] 

)), 

"releasing_status": ReleasingStatus[data["releasing_status"]], 

"cover_url": data["cover_url"] 

} 

 

for date in ["releasing_start", "releasing_end"]: 

date_data = data[date] 

if date_data is not None: 

deserialized[date] = Date.deserialize(date_data) 

else: 

deserialized[date] = None 

 

return deserialized 

 

@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 [ 

"format", "id", "title", "relations", "releasing_status", 

"releasing_start", "releasing_end", "cover_url" 

] 

 

 

class AnimeData(MediaData): 

""" 

Class that models anime data 

""" 

 

def __init__( 

self, 

_format: MediaFormat, 

_id: Id, 

title: Title, 

relations: List[Relation], 

releasing_status: ReleasingStatus, 

releasing_start: Optional[Date], 

releasing_end: Optional[Date], 

cover_url: Optional[str], 

episode_count: Optional[int], 

episode_duration: Optional[int], 

): 

""" 

Initializes the Anime Data object and checks for type issues 

:param _format: The media format 

:param _id: The ID of the anime 

:param title: The title of the anime 

:param relations: The relations of the anime 

:param releasing_status: The airing status of the anime 

:param releasing_start: The day the anime started airing 

:param releasing_end: The day the last episode aired 

:param episode_count: The amount of episodes of the anime 

:param episode_duration: The duration of the episodes in minutes 

:param cover_url: An URL pointing to a cover image for the anime 

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

""" 

super().__init__( 

MediaType.ANIME, 

_format, 

_id, 

title, 

relations, 

releasing_status, 

releasing_start, 

releasing_end, 

cover_url 

) 

self.ensure_type(episode_count, int, True) 

self.ensure_type(episode_duration, int, True) 

self.episode_count = episode_count 

self.episode_duration = episode_duration 

 

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 

""" 

data = super()._serialize() 

data["episode_count"] = self.episode_count 

data["episode_duration"] = self.episode_duration 

return data 

 

@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 { 

"episode_count": data["episode_count"], 

"episode_duration": data["episode_duration"] 

} 

 

@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 ["episode_count", "episode_duration"] 

 

 

class MangaData(MediaData): 

""" 

Class that models manga data 

""" 

 

def __init__( 

self, 

_format: MediaFormat, 

_id: Id, 

title: Title, 

relations: List[Relation], 

releasing_status: ReleasingStatus, 

releasing_start: Optional[Date], 

releasing_end: Optional[Date], 

cover_url: Optional[str], 

chapter_count: Optional[int], 

volume_count: Optional[int], 

): 

""" 

Initializes the Manga Data object and checks for type issues 

:param _format: The media format 

:param _id: The ID of the manga 

:param title: The title of the manga 

:param relations: The relations of the manga 

:param releasing_status: The releasing status of the manga 

:param releasing_start: The day the manga started releasing 

:param releasing_end: The day the last chapter/volume released 

:param chapter_count: The total amount of chapters of this manga 

:param volume_count: The total amount of volumes of this manga 

:param cover_url: An URL pointing to a cover image for the manga 

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

""" 

super().__init__( 

MediaType.MANGA, 

_format, 

_id, 

title, 

relations, 

releasing_status, 

releasing_start, 

releasing_end, 

cover_url 

) 

self.ensure_type(chapter_count, int, True) 

self.ensure_type(volume_count, int, True) 

self.chapter_count = chapter_count 

self.volume_count = volume_count 

 

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 

""" 

data = super()._serialize() 

data["chapter_count"] = self.chapter_count 

data["volume_count"] = self.volume_count 

return data 

 

@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 { 

"chapter_count": data["chapter_count"], 

"volume_count": data["volume_count"] 

} 

 

@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 ["chapter_count", "volume_count"]