示例#1
0
def process_user_search_answer(message: Message, searched_anime_id, bot=None):
    if message.text == 'No':
        markup = types.ForceReply(selective=False)
        message_text = """Ok, try again
Find title:"""
        user_answer = bot.send_message(message.chat.id, message_text, reply_markup=markup)
        bot.register_next_step_handler(user_answer, find_anime, bot=bot)
    elif message.text == 'Yes':
        removed_title = db.get_first_title(searched_anime_id)
        message_text = f"Rating for {removed_title} is no longer in your account"
        markup = variables.main_menu()
        user_id = message.from_user.id
        db.remove_rating(user_id, searched_anime_id)
        bot.send_message(message.chat.id, message_text, reply_markup=markup)
    elif message.text == 'Exit removing mode':
        markup = variables.main_menu()
        bot.send_message(message.chat.id, 'Ok, leaving removing mode', reply_markup=markup)
    else:
        second_message_text = "<b>Please, tell me 'Yes' or 'No'!</b>"
        titles = db.get_meta_by_id(searched_anime_id)['titles'].split(',')
        first_message_text = f"""Is it <b>{titles[0]}</b>?\n\n"""
        if len(titles) > 1:
            first_message_text += 'Alternative titles:\n'
            for title_ind in range(1, len(titles)):
                first_message_text += f'{titles[title_ind]}\n'
        markup = types.ReplyKeyboardMarkup(row_width=2, selective=False)
        yes_button = types.KeyboardButton('Yes')
        no_button = types.KeyboardButton('No')
        markup.add(no_button, yes_button)
        bot.send_message(message.chat.id, first_message_text)
        user_answer = bot.send_message(message.chat.id, second_message_text, reply_markup=markup)
        bot.register_next_step_handler(user_answer, process_user_search_answer, searched_anime_id, bot=bot)
示例#2
0
def receive_user_rating(message: Message, anime_id, bot=None):
    rating = None
    try:
        rating = float(message.text)
    except ValueError:
        pass
    title = db.get_first_title(anime_id)
    if rating and 0 <= rating <= 10:
        user_id = message.from_user.id
        db.add_rating(user_id, anime_id, rating)
        db.update_factors(user_id)
        message_text = f"Rating <b>{rating}</b> for <b>{title}</b> successfully added to your account!"
        markup = variables.main_menu()
        bot.send_message(message.chat.id, message_text, reply_markup=markup)
    else:
        message_text = "<b>Rating must be a number from 0 to 10!</b>"
        message_text += f"\nYour rating for <b>{title}</b>:"
        markup = types.ForceReply(selective=False)
        user_answer = bot.send_message(message.chat.id,
                                       message_text,
                                       reply_markup=markup)
        bot.register_next_step_handler(user_answer,
                                       receive_user_rating,
                                       anime_id,
                                       bot=bot)
示例#3
0
def find_anime(message: Message, bot=None):
    search_guesses = search.find(message.text)[:6:]
    search_result = search_guesses.pop(0)
    message_text = ""
    if len(search_guesses) > 0:
        message_text += "<b>Perhaps you meant:</b>"
        for search_guess in search_guesses:
            related_title = db.get_first_title(search_guess)
            message_text += f'\n{related_title}'
        message_text += '\n\n'
    titles = db.get_meta_by_id(search_result)['titles'].split(',')
    message_text += f"Is it <b>{titles[0]}</b>?"
    release_date = db.get_meta_by_id(search_result)['release_date']
    message_text += f'\nYear: {release_date}\n\n'
    if len(titles) > 1:
        message_text += '<b>Alternative titles:</b>\n'
        for title_ind in range(1, len(titles)):
            message_text += f'{titles[title_ind]}\n'
    markup = types.ReplyKeyboardMarkup(row_width=2)
    yes_button = types.KeyboardButton('Yes')
    no_button = types.KeyboardButton('No')
    exit_button = types.KeyboardButton('Exit removing mode')
    markup.add(no_button, yes_button, exit_button)
    anime_poster = db.get_poster(search_result)
    if anime_poster:
        bot.send_photo(message.chat.id, anime_poster)
    user_answer = bot.send_message(message.chat.id, message_text, reply_markup=markup)
    bot.register_next_step_handler(user_answer, process_user_search_answer, search_result, bot=bot)
示例#4
0
def add_rating_from_inline(anime_id, chat_id, bot=None):
    title = db.get_first_title(anime_id)
    message_text = f"Your rating (from 0 to 10) for <b>{title}</b>:"
    markup = types.ForceReply(selective=False)
    user_answer = bot.send_message(chat_id, message_text, reply_markup=markup)
    bot.register_next_step_handler(user_answer,
                                   receive_user_rating,
                                   anime_id,
                                   bot=bot)
示例#5
0
def ban_anime(anime_id, chat_id, bot=None):
    user_id = db.get_user_id_by_chat_id(chat_id)
    db.ban_anime(user_id, anime_id)
    ban_title = db.get_first_title(anime_id)
    message_text = f"<b>{ban_title}</b> will no longer appear in your recommendations"
    bot.send_message(chat_id, message_text)
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)
示例#7
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)
def process_user_search_answer(message: Message, searched_anime_id, bot=None):
    if message.text == 'No':
        markup = types.ForceReply(selective=False)
        message_text = """Ok, try again
Find title:"""
        user_answer = bot.send_message(message.chat.id,
                                       message_text,
                                       reply_markup=markup)
        bot.register_next_step_handler(user_answer, find_anime, bot=bot)
    elif message.text == 'Yes':
        markup = variables.main_menu()
        bot.send_message(message.chat.id, 'Information:', reply_markup=markup)
        message_text = ""
        title = db.get_first_title(searched_anime_id)
        anime_meta = db.get_meta_by_id(searched_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
        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
        message_text += f"<b>{title}</b>"
        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}'

        anime_poster = db.get_poster(searched_anime_id)

        inline_keyboard = InlineKeyboardMarkup(row_width=1)
        rate_button = InlineKeyboardButton(
            text='Rate',
            callback_data=f'rate-{searched_anime_id}-{message.chat.id}')
        ban_button = InlineKeyboardButton(
            text='Ban',
            callback_data=f'ban-{searched_anime_id}-{message.chat.id}')
        inline_keyboard.add(ban_button, rate_button)

        if anime_poster and len(message_text) <= 1020:
            bot.send_photo(message.chat.id,
                           anime_poster,
                           caption=message_text,
                           reply_markup=inline_keyboard)
        else:
            if anime_poster:
                bot.send_photo(message.chat.id, anime_poster)
                bot.send_message(message.chat.id,
                                 message_text,
                                 reply_markup=inline_keyboard)
            else:
                bot.send_message(message.chat.id,
                                 message_text,
                                 reply_markup=inline_keyboard)

    elif message.text == 'Exit info mode':
        markup = variables.main_menu()
        bot.send_message(message.chat.id,
                         'Ok, leaving info mode',
                         reply_markup=markup)
    else:
        second_message_text = "<b>Please, tell me 'Yes' or 'No'!</b>"
        titles = db.get_meta_by_id(searched_anime_id)['titles'].split(',')
        first_message_text = f"""Is it <b>{titles[0]}</b>?\n\n"""
        if len(titles) > 1:
            first_message_text += 'Alternative titles:\n'
            for title_ind in range(1, len(titles)):
                first_message_text += f'{titles[title_ind]}\n'
        markup = types.ReplyKeyboardMarkup(row_width=2, selective=False)
        yes_button = types.KeyboardButton('Yes')
        no_button = types.KeyboardButton('No')
        markup.add(no_button, yes_button)
        bot.send_message(message.chat.id, first_message_text)
        user_answer = bot.send_message(message.chat.id,
                                       second_message_text,
                                       reply_markup=markup)
        bot.register_next_step_handler(user_answer,
                                       process_user_search_answer,
                                       searched_anime_id,
                                       bot=bot)