def anime_in_db(chat_id, anime_name): ''' Function to check if an anime exists in the database ''' try: Anime.get(name=anime_name) return True except: return False
def create_quote(anime): characters_models = [] characters = anime.get_characters() for character in characters: quotes_models = [] quotes = character.get_quotes() for quote in quotes: print(type(quote.get_tags())) quotes_models.append( Quote(quote=quote.get_quote(), tags=quote.get_tags(), views=quote.get_views(), likes=quote.get_likes())) characters_models.append( Character(name=character.get_name(), image=character.get_image(), quotes=quotes_models, views=character.get_views())) db.session.add( Anime(name=anime.get_name(), image=anime.get_image(), characters=characters_models, views=anime.get_views())) print("{} added".format(anime.get_name()))
def get_anime(entry, year, season): mal_id = get_mal_id(entry.find('a').get('href')) if mal_id: theme_list = [] title = [entry.text] entry = entry.nextSibling.nextSibling # Extra names if entry.name == 'p': title.extend(entry.text.split(', ')) entry = entry.nextSibling.nextSibling category = "" if entry.name == 'p': category = entry.text entry = entry.nextSibling.nextSibling theme_size = 0 for index, item in enumerate(entry.findAll('tr')[1:]): if item.find('td').text != "": theme = get_theme(item, mal_id, f'{mal_id}-{f"{theme_size:02d}"}', category=category) if theme: theme_list.append(theme) theme_size += 1 entry = entry.nextSibling.nextSibling if entry: if entry.name == 'p': category = entry.text entry = entry.nextSibling.nextSibling for index, item in enumerate(entry.findAll('tr')[1:], start=theme_size): theme = get_theme(item, mal_id, f'{mal_id}-{f"{index:02d}"}', category=category) if theme: theme_list.append(theme) return Anime.create(mal_id=mal_id, title=title, year=year, season=season, cover=get_cover(mal_id), themes=theme_list) else: return None, None
def addNewAnime(): if request.method == "POST": animeid = request.form['animeid'] animename = request.form['animename'] animegenre = request.form['animegenre'] animetype = request.form['animetype'] animenumepisodes = request.form['animenumepisodes'] animerating = request.form['animerating'] animemembers = request.form['animemembers'] if not animeid: success = "ID missing" elif not animename: success = "Name missing" elif not animegenre: success = "Genre missing" elif not animetype: success = "Type missing" elif not animenumepisodes: success = "Episodes missing" elif not animerating: success = "Rating missing" elif not animemembers: success = "Members missing" else: success = True if success == True: newAnime = Anime(animeid, animename, animegenre, animetype, animenumepisodes, animerating, animemembers) db.session.add(newAnime) db.session.commit() return render_template("addNew.html", success=success) else: return render_template("addNew.html")
def get_anime_by_id(chat_id, anime_id): ''' Function to get a specific anime by it's id ''' try: anime = Anime.get(id=anime_id) return anime except: return None
def get_anime_subscriber_bundle(anime_name=None): ''' Function to get all anime as well as the users that are subscribed to each. ''' # bundle = {anime_name: [anime_object, [chat_id1, chat_id2, chat_id3] ] } bundle = {} if isinstance(anime_name, type(None)): animes = Anime.select() else: anime = Anime.get(name=anime_name) animes = [anime] for anime in animes: bundle[anime.name] = [anime, []] users = anime.users for user in users: bundle[anime.name][1].append(user.chat_id) return bundle
def atualiza_base_animes(driver=driver): driver.get('https://punchsubs.net/principal') #pagina inicial driver.maximize_window() time.sleep(1) try: driver.find_element_by_class_name('close').click() except NoSuchElementException: pass time.sleep(1) btn = driver.find_element_by_xpath('//*[@id="navbarCollapse"]/ul/li[2]/a') btn.click() time.sleep(2) check = True while check == True: #animes ul = driver.find_element_by_class_name('list') li_all = ul.find_elements_by_xpath('./li') for li in li_all: try: eps = li.find_element_by_xpath('./a/div/p[3]').text eps = eps.replace(' episódios', '') eps = int(eps) anime = Anime.create( nome=li.find_element_by_xpath('./a/div/p[2]').text, genero=li.get_attribute('data-genero'), numeroEpisodio=eps, imagem=li.find_element_by_xpath('./a/img').get_attribute( 'src'), link=li.find_element_by_xpath('./a').get_attribute('href'), status=0) except NoSuchElementException: print('Anime {} já adicionado.'.format( li.find_element_by_xpath('./a/div/p[2]').text)) #paginação ul = driver.find_element_by_class_name('pagination') li_all = ul.find_elements_by_xpath('./li') for i in range(len(li_all)): if li_all[i].get_attribute('class') == 'active' and i == len( li_all): check = False break elif li_all[i].get_attribute( 'class') == 'active' and i != len(li_all): li_all[i + 1].click() break
def subscribe_user_to_anime(chat_id, anime_name): ''' Function to subscribe a user to an anime that already exists in the database ''' chat_id = int(chat_id) if not user_is_subscribed(chat_id, anime_name): anime = Anime.get(name=anime_name) user = User.get(chat_id=chat_id) anime.users.add(user) anime.save()
def anime_from_cell(cell): e = pq(cell) a = Anime() a.rank = int(e('.rank').text()) a.score = float(e('.score').text()) te = e('.title') a.image = te('img').attr('data-src') a.url = te('a').attr('href') de = e('.detail') a.title = de('a.hoverinfo_trigger').text() info = de('.information').text().split('\n') a.type = info[0] a.time = info[1] # 逗号需要手动处理:'1,340,302' => 1340302 num_str = info[2].split()[0] a.members = int(num_str.replace(',', '')) return a
def add_new_anime(chat_id, anime_name, episodes_list_url, last_episode): ''' A function to add a new anime entry to the database ''' chat_id, last_episode = (int(chat_id), int(last_episode)) user = User.get(chat_id=chat_id) new_anime, created = Anime.get_or_create(name=anime_name) new_anime.episodes_url, new_anime.last_episode = (episodes_list_url, last_episode) new_anime.save() if not user_is_subscribed(chat_id, anime_name): user.animes.add(new_anime) return new_anime
def user_is_subscribed(chat_id, anime_name): ''' Functionn to check if a user is already subscribed to some anime in the database ''' chat_id = int(chat_id) if not anime_in_db(chat_id, anime_name): return False else: user, created = User.get_or_create(chat_id=chat_id) anime = Anime.get(name=anime_name) if user in anime.users: return True return False
def create_anime(payload): if request.data: body = request.get_json() title = body.get('title', None) image_url = body.get('image_url', None) mal_url = body.get('mal_url', None) score = body.get('score', None) rank = body.get('rank', None) if title is None: abort(422) anime = Anime(title=title, image_url=image_url, mal_url=mal_url, score=score, rank=rank) Anime.insert(anime) new_anime = Anime.query.filter_by(id=anime.id).first() return jsonify({'success': True, 'anime': new_anime.format()}) else: abort(422)
def unsubscribe_user_from_anime(chat_id, anime_name): ''' Function to unsubscribe a user from an anime. ''' chat_id = int(chat_id) if user_is_subscribed(chat_id, anime_name): anime = Anime.get(name=anime_name) user = User.get(chat_id=chat_id) try: user.animes.remove(anime) anime.users.remove(anime) except: pass anime.save() user.save()
def create(titulo, episodio_atual, total_episodios, status_id): anime = Anime() anime.titulo = titulo anime.episodio_atual = episodio_atual anime.total_episodios = total_episodios anime.status = status_id session.add(anime) session.commit() return anime
async def getAnimeInfo(url): print('getAnimeInfo') html = getHtml(url) html = html.decode('utf-8') reAnimeJson = re.compile(regAnimeJson) animeStr = re.findall(reAnimeJson, html) animeJson = json.loads(animeStr[0]) if (animeJson['message'] == 'success'): uid = next_id() result = animeJson['result'] animeInfo = Anime(id=uid, area=result['area'], bangumiId=result['bangumi_id'], title=result['title'], coins=result['coins'], cover=result['cover'], danmakuCount=result['danmaku_count'], evaluate=result['evaluate'], favorites=result['favorites'], playCount=result['play_count'], seasonId=result['season_id'], shareUrl=result['share_url'], weekday=result['weekday']) animeInfo.pubTime = time.mktime( time.strptime(result['pub_time'], "%Y-%m-%d %H:%M:%S")) animeInfo.seasonTitle = result['season_title'] tags = "" for tag in result['tags']: tags = str(tags + ',' + tag['tag_name']) animeInfo.tags = tags if 'payment' in result: animeInfo.price = result['payment']['price'] episodes = result['episodes'] for episode in episodes: await getAnimeItem(episode, animeInfo.seasonId, animeInfo.seasonTitle) await animeInfo.save()
season_meta, season_data = re.compile(r"\n\n").split(season_data, 1) if (match := RE_SEASON.match(season_meta)) is None: print(f"SESSON ERROR: {season_meta}") return {} season = match["season"] year = match["year"] # Anime entries always start with "###[One Piece]" animes_data = season_data.split("###")[1:] for anime_data in animes_data: name, mal, aliases, entries = parse_anime_data(anime_data, data) anime = Anime(name=name, season=season, year=year, entries=entries, mal=mal, aliases=aliases) data.add_anime(anime) def parse_anime_data(anime_data: str, data: ParserData) -> (str, str, list, list): # Separate anime metadata from actual entries anime_meta, anime_data = re.compile(r"\n ?\n?(?!\*\*)").split( anime_data.strip(), 1) if (match := RE_ANIME.match(anime_meta)) is None: print(f"ANIME ERROR: {anime_meta}") return {}
from playhouse.migrate import migrate from models import Anime, Episodios, db, migrator try: selecao = peewee.BooleanField(default=True) migrate( migrator.add_column('anime', 'selecao', selecao) ) except peewee.OperationalError: pass try: migrate( #migrator.drop_column('episodios', 'nome') migrator.drop_column('episodios', 'video') ) except peewee.OperationalError: pass if __name__ == '__main__': try: Anime.create_table() except peewee.OperationalError: print('Tabela Anime já existe.') try: Episodios.create_table() except peewee.OperationalError: print('Tabela Episodios já existe.')
anime_name_dict = { "english": ANIME_EXAMPLE["name"]["english"], "romaji": ANIME_EXAMPLE["name"]["romaji"], "native": ANIME_EXAMPLE["name"]["native"], "preferred": "", "synonyms": [""], } anime = Anime( #id=ANIME_EXAMPLE["id"], name=anime_name_dict, format=ANIME_EXAMPLE["format"], tags=ANIME_EXAMPLE["tags"], genres=ANIME_EXAMPLE["genres"], synopsis=ANIME_EXAMPLE["synopsis"], season=ANIME_EXAMPLE["season"], duration=ANIME_EXAMPLE["duration"], release_date=ANIME_EXAMPLE["release_date"], studios=ANIME_EXAMPLE["studios"], episodes=ANIME_EXAMPLE["episodes"], source=ANIME_EXAMPLE["source"], rating=ANIME_EXAMPLE["rating"], ) #debug_dump = anime.dump() try: anime.commit() except DuplicateKeyError as e: print("Record already exists.") print(e) pass
Base.metadata.create_all(engine) # Iterate over the PEOPLE structure and populate the database for ANIME_EXAMPLE in ANIME_EXAMPLE_DATA: anime_name = AnimeName(English=ANIME_EXAMPLE["name"]["english"], Romaji=ANIME_EXAMPLE["name"]["romaji"], Native=ANIME_EXAMPLE["name"]["native"]) characters = [] for character in ANIME_EXAMPLE["characters"]: characters.append( Character(name=character["name"], actor="", picture="", actor_picture="")) p = Anime(id=ANIME_EXAMPLE["id"], animename=anime_name, format=ANIME_EXAMPLE["format"], tags=ANIME_EXAMPLE["tags"], genres=ANIME_EXAMPLE["genres"], synopsis=ANIME_EXAMPLE["synopsis"], season=ANIME_EXAMPLE["season"], duration=ANIME_EXAMPLE["duration"], release_date=ANIME_EXAMPLE["release_date"], studios=ANIME_EXAMPLE["studios"], episodes=ANIME_EXAMPLE["episodes"], characters=characters, rating=ANIME_EXAMPLE["rating"], source=ANIME_EXAMPLE["source"]) session.add(p) session.commit()