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

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

"""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 List, Optional 

from anime_list_apis.cache.Cache import Cache 

from anime_list_apis.models.CacheAble import CacheAble 

from anime_list_apis.models.attributes.Id import Id, IdType 

from anime_list_apis.models.attributes.MediaType import MediaType 

from anime_list_apis.models.MediaData import AnimeData, MangaData, MediaData 

from anime_list_apis.models.MediaUserData import \ 

MediaUserData, AnimeUserData, MangaUserData 

from anime_list_apis.models.MediaListEntry import \ 

AnimeListEntry, MangaListEntry, MediaListEntry 

 

 

class ApiInterface: 

""" 

Defines methods that every API connector should implement 

""" 

 

def __init__( 

self, 

id_type: IdType, 

cache: Cache = None, 

rate_limit_pause: float = 0.0 

): 

""" 

Initializes the Api interface. 

Intializes cache or uses the one provided. 

:param id_type: TheID type of the API Interface 

:param cache: The cache to use. If left as None, will use default cache 

:param rate_limit_pause: A duration in seconds that the API Interface 

will pause after a network operation to 

prevent being rate limited 

""" 

self.cache = cache if cache is not None else Cache() 

self.id_type = id_type 

self.rate_limit_pause = rate_limit_pause 

 

# Public Methods ---------------------------------------------------------- 

 

def get_data( 

self, 

media_type: MediaType, 

_id: int or Id, 

fresh: bool = False 

) -> Optional[MediaData]: 

""" 

Retrieves a single data object using the API 

Tries to get the cached value first, then checks Anilist 

:param media_type: The media type to retrieve 

:param _id: The ID to retrieve. May be either an int or an Id object 

:param fresh: Fetches a fresh, i.e. non-cached version 

:return: The Media Data or None if no valid data was found 

""" 

cached = self.cache.get_media_data(IdType.ANILIST, media_type, _id) 

 

if fresh or cached is None: 

cached = self._get_data(media_type, self.__generate_id_obj(_id)) 

self.__cache(cached) 

 

return cached 

 

def get_user_data( 

self, 

media_type: MediaType, 

_id: int or Id, 

username: str, 

fresh: bool = True 

) -> Optional[MediaUserData]: 

""" 

Retrieves the user data of a single entry for a user 

:param media_type: The type of media to fetch 

:param _id: The ID to fetch 

:param username: The username for which to fetch 

:param fresh: Fetches a fresh, i.e. non-cached version 

:return: The MediaUserData object or None if not found 

""" 

cached = self.cache.get_media_user_data( 

IdType.ANILIST, media_type, _id, username 

) 

 

if fresh or cached is None: 

cached = self._get_user_data( 

media_type, self.__generate_id_obj(_id), username 

) 

self.__cache(cached) 

 

return cached 

 

def get_user_data_list( 

self, 

media_type: MediaType, 

username: str 

) -> List[MediaUserData]: 

""" 

Retrieves a user's entire list with only the user data 

:param media_type: The media type to fetch the entries for 

:param username: The user for whom to fetch the entries for 

:return: The retrieves user data in a list 

""" 

data = self._get_user_data_list(media_type, username) 

for obj in data: 

self.__cache(obj, dont_write=True) 

return data 

 

def get_list_entry( 

self, 

media_type: MediaType, 

_id: int or Id, 

username: str, 

fresh: bool = False 

) -> Optional[MediaListEntry]: 

""" 

Retrieves a user list entry. 

First checks for cached entries, otherwise fetches from anilist 

:param media_type: The media type to fetch 

:param _id: The ID to retrieve. May be and int or an Id object 

:param username: The user for which to fetch the entry 

:param fresh: Fetches a fresh, i.e. non-cached version 

:return: The entry for the user or 

None if the user doesn't have such an entry 

""" 

cached = self.cache.get_media_list_entry( 

IdType.ANILIST, media_type, _id, username 

) 

 

if fresh or cached is None: 

cached = self._get_list_entry( 

media_type, 

self.__generate_id_obj(_id), 

username 

) 

self.__cache(cached) 

 

return cached 

 

def get_list( 

self, 

media_type: MediaType, 

username: str 

) -> List[MediaListEntry]: 

""" 

Retrieves a user's entire list 

Stores all entries in the cache upon completion 

:param media_type: The media type to fetch 

:param username: The username for which to fetch the list 

:return: The list of List entries 

""" 

entries = self._get_list(media_type, username) 

for entry in entries: 

self.__cache(entry, dont_write=True) 

return entries 

 

def is_in_list( 

self, 

media_type: MediaType, 

_id: int or Id, 

username: str, 

fresh: bool = False 

) -> bool: 

""" 

Checks if an entry is in a user's list 

:param media_type: The media type to check for 

:param _id: The ID to check for 

:param username: The username for which to check 

:param fresh: Indicates if the most up-to-date results should be used 

:return: True if the id is in the list, False otherwise 

""" 

return self.get_user_data(media_type, _id, username, fresh) is not None 

 

def get_related_data( 

self, 

datas: List[MediaData] or MediaData, 

fresh: bool = False 

) -> List[MediaData]: 

""" 

Retrieves related data for either a list of MediaData objects or 

a single MediaData object 

:param datas: A list of MediaData objects (or a single one) 

:param fresh: Indicates if the most up-to-date results should be used 

:return: A list of related media data 

""" 

if not isinstance(datas, list): 

datas = [datas] 

 

related = [] 

for data in datas: 

self.__append_related_data(data, related, fresh) 

 

return related 

 

# Abstract Methods -------------------------------------------------------- 

 

def _get_data( 

self, 

media_type: MediaType, 

_id: Id 

) -> Optional[MediaData]: 

""" 

Retrieves a single data object using the API 

Actual implementation should be done by subclasses 

:param media_type: The media type to retrieve 

:param _id: The ID to retrieve. 

:return: The Anime Data or None if no valid data was found 

""" 

raise NotImplementedError() # pragma: no cover 

 

def _get_user_data( 

self, 

media_type: MediaType, 

_id: Id, 

username: str 

) -> Optional[MediaUserData]: 

""" 

Actual implementation of the get_user_data for each subclass 

:param media_type: The media type to fetch 

:param _id: The ID to retrieve 

:param username: The user for which to fetch the data 

:return: The user data for the entry or 

None if the user doesn't have such an entry 

""" 

raise NotImplementedError() # pragma: no cover 

 

def _get_user_data_list(self, media_type: MediaType, username: str) \ 

-> List[MediaUserData]: 

""" 

Retrieves a user's entire list of user data 

Actual implementation method to be implemented by subclasses 

:param media_type: The media type to fetch 

:param username: The username for which to fetch the list 

:return: The list of user data 

""" 

raise NotImplementedError() # pragma: no cover 

 

def _get_list_entry( 

self, 

media_type: MediaType, 

_id: Id, 

username: str 

) -> Optional[MediaListEntry]: 

""" 

Actual implementation of the get_list_entry for each subclass 

:param media_type: The media type to fetch 

:param _id: The ID to retrieve 

:param username: The user for which to fetch the entry 

:return: The entry for the user or 

None if the user doesn't have such an entry 

""" 

raise NotImplementedError() # pragma: no cover 

 

def _get_list(self, media_type: MediaType, username: str) \ 

-> List[MediaListEntry]: 

""" 

Retrieves a user's entire list 

Actual implementation method to be implemented by subclasses 

:param media_type: The media type to fetch 

:param username: The username for which to fetch the list 

:return: The list of List entries 

""" 

raise NotImplementedError() # pragma: no cover 

 

# Shortcut Methods -------------------------------------------------------- 

 

def get_anime_data(self, _id: int or Id, fresh: bool = False) \ 

-> Optional[AnimeData]: 

""" 

Shortcut for get_data that retrieves only Anime media 

:param _id: The ID to fetch. May be int or Id object 

:param fresh: Fetches a fresh, i.e. non-cached version 

:return: The retrieved AnimeData object or None if not a valid ID 

""" 

return self.get_data(MediaType.ANIME, _id, fresh) 

 

def get_anime_user_data( 

self, 

_id: int or Id, 

username: str, 

fresh: bool = False 

) -> Optional[AnimeUserData]: 

""" 

Retrieves a user's user data for a single entry 

:param _id: The ID to fetch. May be int or an Id object 

:param username: The user for which to fetch the user data 

:param fresh: Fetches a fresh, i.e. non-cached version 

:return: The AnimeUserData or None if there is not such entry 

""" 

return self.get_user_data(MediaType.ANIME, _id, username, fresh) 

 

def get_anime_user_data_list(self, username: str) -> List[AnimeUserData]: 

""" 

Retrieves a user's complete anime list of user data objects 

:param username: The user's username 

:return: The user's list of AnimeUserData objects 

""" 

# noinspection PyTypeChecker 

return self.get_user_data_list(MediaType.ANIME, username) 

 

def get_anime_list_entry( 

self, 

_id: int or Id, 

username: str, 

fresh: bool = False 

) -> Optional[AnimeListEntry]: 

""" 

Retrieves a user's list entry for a single entry 

:param _id: The ID to fetch. May be int or an Id object 

:param username: The user for which to fetch the entry 

:param fresh: Fetches a fresh, i.e. non-cached version 

:return: The Anime List Entry or None if there is not such entry 

""" 

return self.get_list_entry(MediaType.ANIME, _id, username, fresh) 

 

def get_anime_list(self, username: str) -> List[AnimeListEntry]: 

""" 

Retrieves a user's complete anime list 

:param username: The user's username 

:return: The user's list of AnimeListEntry objects 

""" 

# noinspection PyTypeChecker 

return self.get_list(MediaType.ANIME, username) 

 

def is_in_anime_list( 

self, 

_id: int or Id, 

username: str, 

fresh: bool = False 

) -> bool: 

""" 

Checks if an entry is in a user's list 

:param _id: The ID to check for 

:param username: The username for which to check 

:param fresh: Indicates if the most up-to-date results should be used 

:return: True if the id is in the list, False otherwise 

""" 

return self.is_in_list(MediaType.ANIME, _id, username, fresh) 

 

def get_manga_data(self, _id: int or Id, fresh: bool = False) \ 

-> Optional[MangaData]: 

""" 

Shortcut for get_data that retrieves only Manga media 

:param _id: The ID to fetch. May be int or Id object 

:param fresh: Fetches a fresh, i.e. non-cached version 

:return: The retrieved MangaData object or None if not a valid ID 

""" 

return self.get_data(MediaType.MANGA, _id, fresh) 

 

def get_manga_user_data( 

self, 

_id: int or Id, 

username: str, 

fresh: bool = False 

) -> Optional[MangaUserData]: 

""" 

Retrieves a user's user data for a single entry 

:param _id: The ID to fetch. May be int or an Id object 

:param username: The user for which to fetch the user data 

:param fresh: Fetches a fresh, i.e. non-cached version 

:return: The MangaUserData or None if there is not such entry 

""" 

return self.get_user_data(MediaType.MANGA, _id, username, fresh) 

 

def get_manga_user_data_list(self, username: str) -> List[AnimeUserData]: 

""" 

Retrieves a user's complete manga list of user data objects 

:param username: The user's username 

:return: The user's list of MangaUserData objects 

""" 

# noinspection PyTypeChecker 

return self.get_user_data_list(MediaType.MANGA, username) 

 

def get_manga_list_entry( 

self, 

_id: int or Id, 

username: str, 

fresh: bool = False 

) -> Optional[MangaListEntry]: 

""" 

Retrieves a user's list entry for a single entry 

:param _id: The ID to fetch. May be int or an Id object 

:param username: The user for which to fetch the entry 

:param fresh: Fetches a fresh, i.e. non-cached version 

:return: The Manga List Entry or None if there is not such entry 

""" 

return self.get_list_entry(MediaType.MANGA, _id, username, fresh) 

 

def get_manga_list(self, username: str) -> List[MangaListEntry]: 

""" 

Retrieves a user's complete manga list 

:param username: The user's username 

:return: The user's list of MangaListEntry objects 

""" 

# noinspection PyTypeChecker 

return self.get_list(MediaType.MANGA, username) 

 

def is_in_manga_list( 

self, 

_id: int or Id, 

username: str, 

fresh: bool = False 

) -> bool: 

""" 

Checks if an entry is in a user's manga list 

:param _id: The ID to check for 

:param username: The username for which to check 

:param fresh: Indicates if the most up-to-date results should be used 

:return: True if the id is in the list, False otherwise 

""" 

return self.is_in_list(MediaType.MANGA, _id, username, fresh) 

 

# Helper Methods ---------------------------------------------------------- 

 

def __cache(self, data: Optional[CacheAble], dont_write: bool = True): 

""" 

Caches a cache-able data object 

:param data: The data object to cache 

:param dont_write: Will make sure that the cache won't be written to 

file. 

:return: None 

""" 

if data is not None: 

self.cache.add( 

self.id_type, data, ignore_for_write_count=dont_write 

) 

 

def __generate_id_obj(self, _id: int or Id) -> Id: 

""" 

Generates an Id object if the given ID is an integer 

:param _id: The ID to make sure is an Id object 

:return: The generated Id object 

""" 

if isinstance(_id, int): 

_id = Id({self.id_type: _id}) 

return _id 

 

def __append_related_data( 

self, 

data: MediaData, 

previous: List[MediaData], 

fresh: bool = False 

): 

""" 

Retrieves the related MediaData objects for a given MediaData object. 

This method works in-place. 

:param data: The data for which to retrieve the related data 

:param previous: The previously found data, used in recursive calls 

:param fresh: Indicates if the most up-to-date results should be used 

:return: None 

""" 

previous.append(data) 

 

for relation in data.relations: 

 

filtered = list(filter( 

lambda x: x.id == relation.dest, previous 

)) 

 

if len(filtered) == 1: # If already in list, skip 

continue 

else: 

# In-place recursive method 

relation_data = \ 

self.get_data(relation.dest_type, relation.dest, fresh) 

self.__append_related_data(relation_data, previous, fresh)