Ejemplo n.º 1
0
def remove_from_ban_list(user_id, bot=None):
    _, chat_id = db.get_user_data(user_id)
    markup = types.ForceReply(selective=False)
    user_answer = bot.send_message(chat_id,
                                   'Which anime do you want to remove?',
                                   reply_markup=markup)
    bot.register_next_step_handler(user_answer, find_anime, bot=bot)
Ejemplo n.º 2
0
def change_rating(user_id, bot):
    _, chat_id = db.get_user_data(user_id)
    markup = types.ForceReply(selective=False)
    user_answer = bot.send_message(chat_id,
                                   "What anime's rating you want to change?",
                                   reply_markup=markup)
    bot.register_next_step_handler(user_answer, find_anime, bot=bot)
Ejemplo n.º 3
0
def recommend(chat_id, recommendation_index, bot=None):
    user_id = db.get_user_id_by_chat_id(chat_id)
    factors = db.get_factors(user_id)
    user = db.get_ratings_by(user_id)
    if user:
        user = json.loads(user)

    if not user or len(user) == 0:
        bot.send_message(chat_id,
                         'You need to have ratings to get recommendations')
        return

    recommendations = get_recommendations(user, factors)
    ban_list = db.get_ban_list(user_id)
    if not ban_list:
        ban_list = []
    while recommendations[recommendation_index][
            1] in ban_list and recommendation_index < len(recommendations):
        recommendation_index += 1

    user_name, _ = db.get_user_data(user_id)
    message_text = ""
    anime_id = recommendations[recommendation_index][1]
    recommendation_score = recommendations[recommendation_index][0]
    title = db.get_first_title(anime_id)
    anime_meta = db.get_meta_by_id(anime_id)
    alternative_titles = anime_meta['titles'].split(',')
    if len(alternative_titles) > 1:
        alternative_titles = alternative_titles[1::]
    else:
        alternative_titles = []
    genres = ', '.join(anime_meta['genres'].split(','))
    rating = anime_meta['rating']
    members = db.convert_number_to_readable(anime_meta['members'])
    episodes = anime_meta['episodes']
    duration = anime_meta['duration']
    release_date = anime_meta['release_date']
    related_ids_text = anime_meta['related'].split(',')
    related_ids = []
    for related_ind in range(len(related_ids_text)):
        try:
            related_ids.append(int(related_ids_text[related_ind]))
        except ValueError:
            pass
    message_text += f"<b>{title}</b>"
    message_text += f'\n\nRecommendation score: {round(recommendation_score * 10, 2)}'
    if len(alternative_titles) > 0:
        message_text += '\n\nAlternative titles:'
        for alternative_title in alternative_titles:
            message_text += f'\n{alternative_title}'

    message_text += f'\n\nYear: <b>{release_date}</b>'
    message_text += f'\nRating: <b>{rating}</b>'
    message_text += f'\nViews: {members}'
    message_text += f'\n\nGenres:\n{genres}'
    message_text += f'\n\nEpisodes: {episodes}'
    message_text += f'\nEpisode duration: {duration} min(s)'

    real_related_ids = []
    for related_id in related_ids:
        if db.check_anime(related_id):
            real_related_ids.append(related_id)
    related_ids = real_related_ids
    if len(related_ids) > 0:
        message_text += '\n\nRelated to:'
        for related_id in related_ids:
            related_title = db.get_first_title(related_id)
            message_text += f'\n{related_title}'

    message_text += f"\n\nRecommendation for <b>{user_name}</b>"

    anime_poster = db.get_poster(anime_id)

    inline_keyboard = InlineKeyboardMarkup(row_width=2)
    rate_button = InlineKeyboardButton(
        text='Rate', callback_data=f'rate-{anime_id}-{chat_id}')
    ban_button = InlineKeyboardButton(
        text='Ban', callback_data=f'ban-{anime_id}-{chat_id}')
    if recommendation_index + 1 < len(recommendations):
        next_button = InlineKeyboardButton(
            text='Next', callback_data=f'next-{recommendation_index + 1}')
    else:
        next_button = InlineKeyboardButton(text='Next',
                                           callback_data=f'next-{0}')
    inline_keyboard.add(ban_button, rate_button, next_button)

    if anime_poster and len(message_text) <= 1020:
        bot.send_photo(chat_id,
                       anime_poster,
                       caption=message_text,
                       reply_markup=inline_keyboard)
    else:
        if anime_poster:
            bot.send_photo(chat_id, anime_poster)
            bot.send_message(chat_id,
                             message_text,
                             reply_markup=inline_keyboard)
        else:
            bot.send_message(chat_id,
                             message_text,
                             reply_markup=inline_keyboard)
Ejemplo n.º 4
0
def show_account(message: Message, bot=None):
    user_id = message.from_user.id
    user_name, _ = db.get_user_data(user_id)
    user_ratings = db.get_ratings_by(user_id)
    if user_ratings:
        user_ratings = json.loads(user_ratings)
    else:
        user_ratings = {}
    message_text = f"<b>{user_name}</b>'s account"
    empty_account = True
    watched_ids = list(user_ratings)
    rating_list = []
    for watched_id in watched_ids:
        record = [user_ratings[watched_id], int(watched_id)]
        rating_list.append(record)
    rating_list.sort(reverse=True)
    if len(rating_list) > 0:
        empty_account = False
        message_text += "\n\nRatings: "
        for record in rating_list:
            rating = record[0]
            anime_id = record[1]
            title = db.get_meta_by_id(anime_id)['titles'].split(',')[0]
            line = f"\n○ {rating} - {title}"
            message_text += line
        last_line = f"\nYou've rated {len(rating_list)} titles"
        message_text += last_line

    user_ban_list = db.get_ban_list(user_id)
    if user_ban_list:
        empty_account = False
        message_text += "\n\nBan list:"
        for banned_id in user_ban_list:
            banned_title = db.get_first_title(banned_id)
            message_text += f"\n○ {banned_title}"
        message_text += f"\nYou've banned {len(user_ban_list)} titles"

    if empty_account:
        message_text += "\n\nYou don't have anything here yet"

    inline_keyboard = InlineKeyboardMarkup(row_width=2)
    remove_rating_button = InlineKeyboardButton(
        text='Remove rating', callback_data=f'remove_rating-{user_id}')
    remove_from_ban_list_button = InlineKeyboardButton(
        text='Remove from ban list',
        callback_data=f'remove_from_ban_list-{user_id}')
    change_rating_button = InlineKeyboardButton(
        text='Change rating', callback_data=f'change_rating-{user_id}')
    user_ratings = db.get_ratings_by(user_id)
    if user_ratings:
        user_ratings = json.loads(user_ratings)
    else:
        user_ratings = {}
    if len(list(user_ratings)) > 0:
        inline_keyboard.add(remove_rating_button, change_rating_button)
    user_ban_list = db.get_ban_list(user_id)
    if not user_ban_list:
        user_ban_list = []
    if len(user_ban_list) > 0:
        inline_keyboard.add(remove_from_ban_list_button)
    if not empty_account:
        bot.send_message(message.chat.id,
                         message_text,
                         reply_markup=inline_keyboard)
    else:
        bot.send_message(message.chat.id, message_text)