Coverage for otaku_info/background/anilist_manga_chapter_guesses.py: 21%

Shortcuts 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

28 statements  

1"""LICENSE 

2Copyright 2020 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of otaku-info. 

5 

6otaku-info is free software: you can redistribute it and/or modify 

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

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

9(at your option) any later version. 

10 

11otaku-info is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

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

17along with otaku-info. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import time 

21from typing import List 

22from jerrycan.base import db, app 

23from otaku_info.db.MediaUserState import MediaUserState 

24from otaku_info.db.MangaChapterGuess import MangaChapterGuess 

25from otaku_info.enums import MediaType, ListService 

26from otaku_info.external.anilist import guess_latest_manga_chapter 

27 

28 

29def update_anilist_manga_chapter_guesses(): 

30 """ 

31 Updates the manga chapter guesses for anilist items 

32 :return: None 

33 """ 

34 start = time.time() 

35 app.logger.info("Starting update of manga chapter guesses") 

36 

37 guesses: List[MangaChapterGuess] = MangaChapterGuess.query.filter_by( 

38 service=ListService.ANILIST 

39 ).all() 

40 existing_ids = [x.service_id for x in guesses] 

41 

42 anilist_items: List[MediaUserState] = MediaUserState.query.filter_by( 

43 service=ListService.ANILIST, media_type=MediaType.MANGA 

44 ).all() 

45 

46 for item in anilist_items: 

47 if item.service_id not in existing_ids: 

48 new_guess = MangaChapterGuess( 

49 service=item.service, 

50 service_id=item.service_id, 

51 media_type=item.media_type 

52 ) 

53 new_guess = db.session.merge(new_guess) 

54 guesses.append(new_guess) 

55 existing_ids.append(item.service_id) 

56 

57 db.session.commit() 

58 

59 for guess in guesses: 

60 app.logger.debug(f"Updating chapter guess for {guess.service_id}") 

61 delta = time.time() - guess.last_update 

62 if delta > 60 * 60: 

63 guess.last_update = int(time.time()) 

64 guess.guess = guess_latest_manga_chapter(int(guess.service_id)) 

65 db.session.commit() 

66 

67 app.logger.info(f"Finished updating manga chapter guesses " 

68 f"in {time.time() - start}")