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"""LICENSE
2Copyright 2020 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of otaku-info-web.
6otaku-info-web 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.
11otaku-info-web 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.
16You should have received a copy of the GNU General Public License
17along with otaku-info-web. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
20from typing import Dict, List
21from puffotter.flask.base import db
22from otaku_info_web.db.TelegramChatId import TelegramChatId
23from otaku_info_web.db.MediaUserState import MediaUserState
24from otaku_info_web.db.MediaNotification import MediaNotification
25from otaku_info_web.db.MangaChapterGuess import MangaChapterGuess
26from otaku_info_web.db.MediaId import MediaId
27from otaku_info_web.db.MediaItem import MediaItem
28from otaku_info_web.db.NotificationSetting import NotificationSetting
29from otaku_info_web.utils.enums import \
30 MediaType, MediaSubType, ConsumingState, NotificationType
33def send_new_manga_chapter_notifications():
34 """
35 Sends out telegram notifications for manga chapter updates
36 :return: None
37 """
39 chats: Dict[int, TelegramChatId] = {
40 x.user_id: x for x in TelegramChatId.query.all()
41 }
42 chapter_guesses: Dict[int, int] = {
43 x.media_id_id: x.guess for x in MangaChapterGuess.query.all()
44 }
45 user_states: List[MediaUserState] = MediaUserState.query\
46 .join(MediaId) \
47 .join(MediaItem) \
48 .filter(MediaItem.media_type == MediaType.MANGA)\
49 .filter(MediaItem.media_subtype == MediaSubType.MANGA)\
50 .filter(MediaUserState.consuming_state == ConsumingState.CURRENT)\
51 .all()
52 notifications: Dict[int, MediaNotification] = {
53 x.media_user_state_id: x for x in MediaNotification.query.all()
54 }
55 notification_settings: Dict[int, bool] = {
56 x.user_id: x.value
57 for x in NotificationSetting.query.filter_by(
58 notification_type=NotificationType.NEW_MANGA_CHAPTERS
59 ).all()
60 }
62 for user_state in user_states:
64 if not notification_settings.get(user_state.user_id):
65 continue
67 guess = chapter_guesses.get(user_state.media_id_id)
68 notification = notifications.get(user_state.id)
69 chat = chats.get(user_state.user_id)
71 if guess is None or chat is None:
72 continue
73 if notification is None:
74 notification = MediaNotification(
75 media_user_state=user_state, last_update=guess
76 )
77 db.session.add(notification)
79 if guess != notification.last_update:
80 notification.last_update = guess
82 title = user_state.media_id.media_item.title
83 url = user_state.media_id.service_url
84 chat.send_message(
85 f"New Chapter for {title}\n\n"
86 f"Chapter {guess}\n\n"
87 f"{url}"
88 )
90 db.session.commit()