def get_manga_chapters_value(manga_site, manga_id): """ manga_site - 'readmanga' or 'mintmanga' """ if manga_site == 'readmanga': manga = s.query(Readmanga).filter_by(id=manga_id).first() if manga_site == 'mintmanga': manga = s.query(Mintmanga).filter_by(id=manga_id).first() return manga.number_of_chapters
def delete_manga(manga_site, manga_id): """ deletes manga from readmanga or mintmanga tables """ if manga_site == 'readmanga': manga = s.query(Readmanga).filter_by(id=manga_id).first() if manga_site == 'mintmanga': manga = s.query(Mintmanga).filter_by(id=manga_id).first() s.delete(manga) s.commit()
def get_manga_title(manga_site, manga_id): """ manga_site - 'readmanga' or 'mintmanga' returns manga title """ if manga_site == 'readmanga': manga = s.query(Readmanga).filter_by(id=manga_id).first() if manga_site == 'mintmanga': manga = s.query(Mintmanga).filter_by(id=manga_id).first() return manga.title
def unsubscribe_from_manga(manga_site, user_id, manga_id): """ manga_site - 'readmanga' or 'mintmanga' user_id - telegram user_id from effective_user returns list with readmanga/mintmanga ids """ user = get_user(user_id) if manga_site == 'readmanga': manga = s.query(Readmanga).filter_by(id=manga_id).first() user.readmanga.remove(manga) elif manga_site == 'mintmanga': manga = s.query(Mintmanga).filter_by(id=manga_id).first() user.mintmanga.remove(manga) s.commit()
def search_manga_titles(manga_site, title): """ manga_site - 'readmanga' or 'mintmanga' returns list of titles """ if manga_site == 'readmanga': manga_list = s.query(Readmanga).all() elif manga_site == 'mintmanga': manga_list = s.query(Mintmanga).all() result = [] for manga in manga_list: if title.strip().lower() in manga.title.lower(): result.append({'title': manga.title, 'id': manga.id}) return result
def subscribe_to_manga(manga_site, user_id, manga_id): """ manga_site - 'readmanga' or 'mintmanga' user_id - telegram user_id from effective_user manga_id - integer adding readmanga/mintmanga to user supscriptions """ user = get_user(user_id) if manga_site == 'readmanga': manga = s.query(Readmanga).filter_by(id=manga_id).first() user.readmanga.append(manga) elif manga_site == 'mintmanga': manga = s.query(Mintmanga).filter_by(id=manga_id).first() user.mintmanga.append(manga) s.commit()
def erase_new_chapters(context): manga_list = s.query(Tracking).all() for manga in manga_list: manga.new_chapters = None s.commit() s.close() print("new chapters erased")
def toogle_subscription(user_id): user = s.query(User).filter_by(user_id=user_id).first() if not user.active: user.active = True else: user.active = False s.commit()
def get_all_updated_user_manga(user_id): user = s.query(User).filter_by(user_id=user_id).first() updated_manga = [] for manga_id in user.tracking_list: manga = s.query(Tracking).filter_by(manga_id=manga_id).first() if manga.new_chapters: updated_manga.append({ "manga_title": get_manga_title(manga_id), "last_chapter": manga.chapters[0] }) if len(updated_manga) > 0: text = f"""New chapters available:\n""" for manga in updated_manga: text += f""" Title: {manga["manga_title"]} Chapter: {manga["last_chapter"]["chapter_name"]} URL: {manga["last_chapter"]["chapter_url"]} """ return text return False
def get_or_create_subscriber(effective_user, message): subscriber = s.query(User).filter_by(user_id=effective_user.id).first() if not subscriber: subscriber = User( username=effective_user.username, user_id=effective_user.id, chat_id=message.chat.id, active=False ) s.add(subscriber) s.commit() return subscriber
def update_manga(manga_site, manga_id): """ manga_site - 'readmanga' or 'mintmanga' returns False if manga has no chapters or returns True """ time.sleep(get_random_sleep_time()) if manga_site == 'readmanga': manga = s.query(Readmanga).filter_by(id=manga_id).first() elif manga_site == 'mintmanga': manga = s.query(Mintmanga).filter_by(id=manga_id).first() if manga.update_date != date.today() or manga.update_date is None: html = get_html(manga.url) chapters = get_chapters_value(html) if chapters == "no chapters": return False manga.chapters = chapters manga.update_date = date.today() manga.number_of_chapters = len(chapters) s.commit() return True
def import_manga(filename, table): with open(filename, 'r') as f: data = json.load(f) for manga in data: manga_exists = s.query(table).filter_by( title=manga['title']).count() if manga_exists == 0: new_manga = table(title=manga['title'], url=manga['url'], img_url=manga['img_url']) s.add(new_manga) s.commit() else: continue
def get_active_tracking_manga_id(): """returns set of active tracking manga or False""" active_users_list = [ user for user in s.query(Subscribers).all() if user.active and user.tracking_list ] s.close() if active_users_list: set_manga_id = set() for user in active_users_list: for manga_id in user.tracking_list: set_manga_id.add(manga_id) return set_manga_id return False
def update_all_active_mangas(context): manga_id_list = get_active_tracking_manga_id() if manga_id_list: for manga_id in manga_id_list: chapters_before = get_chapters_value(manga_id) updated_manga = check_manga_in_tracking(manga_id) if updated_manga == "not up to date": update_manga_in_tracking(manga_id) manga = s.query(Tracking).filter_by(manga_id=manga_id).first() if chapters_before < manga.chapters: manga.new_chapters = True s.close() time.sleep(get_random_sleep_time()) print("all active manga were updated") else: print("no active manga")
def get_all_active_users(): users_list = s.query(User).filter_by(active=True).all() return users_list
def get_manga_chapters(manga_site, manga_id): if manga_site == 'readmanga': manga = s.query(Readmanga).filter_by(id=manga_id).first() if manga_site == 'mintmanga': manga = s.query(Mintmanga).filter_by(id=manga_id).first() return manga.chapters
def get_user(user_id): user = s.query(User).filter_by(user_id=user_id).first() return user