Exemple #1
0
    def handle_states(self, message, first_entry=False):
        user = users_controller.get_user(message.chat.id)
        if user is None:
            users_controller.create_user(message)
            user = users_controller.get_user(message.chat.id)

        self.states[user['state']](message, first_entry)
Exemple #2
0
 def settings_state(self, message, first_entry=False):
     user = users_controller.get_user(message.chat.id)
     lang = user['language']
     if first_entry:
         self.bot.send_message(
             message.chat.id,
             LANG_DICT[lang]['settings_btn'],
             reply_markup=keyboards.set_settings_keyboard(lang))
     else:
         if message.text == LANG_DICT[lang]['change_lang_btn']:
             users_controller.set_lang(message.chat.id, lang)
             updated_lang = users_controller.get_user(
                 message.chat.id).language
             bot_methods.update_names(user, updated_lang)
             self.bot.send_message(
                 message.chat.id,
                 LANG_DICT[updated_lang]['changed_lang_msg'])
             self.go_to_state(message, 'settings_state')
         elif message.text == LANG_DICT[lang]['choose_team_btn']:
             self.go_to_state(message, 'choose_champ_state')
         elif message.text == LANG_DICT[lang]['notifications_btn']:
             self.go_to_state(message, 'notifications_state')
         elif message.text == LANG_DICT[lang]['return_btn']:
             self.go_to_state(message, 'start')
         else:
             self.go_to_state(message, 'settings_state')
Exemple #3
0
    def start_state(self, message, first_entry=False):
        user = users_controller.get_user(message.chat.id)
        lang = user['language']
        if first_entry:
            self.bot.send_message(
                message.chat.id,
                LANG_DICT[lang]['hello_msg'],
                reply_markup=keyboards.set_main_keyboard(lang))
            if not user.team:
                self.bot.send_message(message.chat.id,
                                      LANG_DICT[lang]['first_choose_team_msg'])
                self.go_to_state(message, 'choose_champ_state')
            else:
                pass
        else:
            if message.text == LANG_DICT[lang]['next_match_btn']:
                self.bot.send_message(message.chat.id,
                                      bot_methods.get_match_info(
                                          users_controller.get_user(
                                              message.chat.id)),
                                      parse_mode='markdown')

            elif message.text == LANG_DICT[lang]['last_match_btn']:
                self.bot.send_message(message.chat.id,
                                      bot_methods.get_match_info(
                                          users_controller.get_user(
                                              message.chat.id),
                                          match_type='last'),
                                      parse_mode='markdown')

            elif message.text == LANG_DICT[lang]['news_btn']:
                parse.send_news(self, message.chat.id, user.language)

            elif message.text == LANG_DICT[lang]['articles_btn']:
                parse.send_articles(self, message.chat.id, user.language)

            elif message.text == LANG_DICT[lang]['squad_btn']:
                self.bot.send_message(message.chat.id,
                                      parse.get_teams_squad(message.chat.id),
                                      parse_mode='markdown')

            elif message.text == LANG_DICT[lang]['time_btn']:
                bot_methods.send_time_to_match(
                    self.bot, users_controller.get_user(message.chat.id))

            elif message.text == LANG_DICT[lang]['settings_btn']:
                self.go_to_state(message, 'settings_state')
            else:
                self.bot.send_message(
                    message.chat.id,
                    LANG_DICT[lang]['hello_msg'],
                    reply_markup=keyboards.set_main_keyboard(lang))
Exemple #4
0
 def notifications_state(self, message, first_entry=False):
     user = users_controller.get_user(message.chat.id)
     lang = user['language']
     if first_entry:
         self.bot.send_message(
             message.chat.id,
             LANG_DICT[lang]['choose_notifications_msg'],
             reply_markup=keyboards.set_notifications_keyboard(user))
     else:
         if message.text == LANG_DICT[lang]['match_started_btn'].format(
                 BOOL_DICT[user.match_started]):
             self.bot.send_message(
                 message.chat.id,
                 LANG_DICT[lang]['update_match_started_notif_msg'].format(
                     LANG_DICT[lang]['enable_msg'][not user.match_started]))
             bot_methods.update_notifications(user, 'match_started')
             self.go_to_state(message, 'notifications_state')
         elif message.text == LANG_DICT[lang]['text_broadcast_btn'].format(
                 BOOL_DICT[user.text_broadcast]):
             self.bot.send_message(
                 message.chat.id,
                 LANG_DICT[lang]['update_text_broadcast_notif_msg'].format(
                     LANG_DICT[lang]['enable_msg']
                     [not user.text_broadcast]))
             bot_methods.update_notifications(user, 'text_broadcast')
             self.go_to_state(message, 'notifications_state')
         elif message.text == LANG_DICT[lang]['return_btn']:
             self.go_to_state(message, 'settings_state')
Exemple #5
0
def get_teams_list(user_id):
    user = users_controller.get_user(user_id)
    url = CHAMPIONATS_DICT[user.language][user.champ]

    page = requests.get(url)
    html = page.text

    soup = BeautifulSoup(html, 'lxml')

    teams = [
        team['alt']
        for team in soup.find('section', class_='top-teams').find_all('img')
    ]

    translated_teams = [
        team for team in translator.translate(
            '\n'.join(teams), src='ru', dest='uk').text.split('\n')
    ]
    user_teams = dict(zip(translated_teams, teams))

    try:
        with open('teams.json') as file:
            data = json.load(file)
    except:
        data = {}

    with open('teams.json', 'w') as file:
        data[user_id] = user_teams
        json.dump(data, file)

    if user.language == 'ru':
        return teams
    else:
        return translated_teams
Exemple #6
0
def send_articles(self, user_id, lang):
    titles = []
    urls = []
    all_articles = {}

    user = users_controller.get_user(user_id)

    url = get_team_foot_url(user)

    page = requests.get(url)
    html = page.text

    soup = BeautifulSoup(html, 'lxml')

    articles_block = soup.find('ul', class_='archive-list')

    try:
        articles = articles_block.find_all('li')

        for article in articles:
            titles.append(article.find('h4').find('a').get_text())
            urls.append(article.find('h4').find('a')['href'])

        all_articles['titles'] = titles
        all_articles['urls'] = urls

        self.bot.send_message(user_id,
                              LANG_DICT[lang]['articles_msg'],
                              reply_markup=keyboards.set_news_buttons(
                                  user_id, all_articles))

    except:
        self.bot.send_message(user_id, LANG_DICT[lang]['no_articles_msg'])
Exemple #7
0
def send_news(self, user_id, lang):
    titles = []
    urls = []
    all_news = {}

    user = users_controller.get_user(user_id)

    url = get_team_foot_url(user)

    page = requests.get(url)
    html = page.text

    soup = BeautifulSoup(html, 'lxml')

    news = soup.find('article', class_='news-feed')

    try:
        other_news = news.find('ul').find_all('li', attrs={'class': None})[:10]

        for article in other_news:
            titles.append(article.find('a').get_text())
            urls.append(article.find('a')['href'])
            all_news['titles'] = titles
            all_news['urls'] = urls

        self.bot.send_message(user_id,
                              LANG_DICT[lang]['last_news_msg'],
                              reply_markup=keyboards.set_news_buttons(
                                  user_id, all_news))
    except:
        self.bot.send_message(user_id, LANG_DICT[lang]['no_news_msg'])
Exemple #8
0
def start_handler(message):
    try:
        user = users_controller.get_user(message.chat.id)
        if user is None:
            users_controller.create_user(message)
        else:
            users_controller.update_state(message.chat.id, 'start')

        state_handler.handle_states(message, first_entry=True)
    except Exception as e:
        print('[ERROR]: {}'.format(e))
Exemple #9
0
def callback_inline(call):
    if call.message:
        user = users_controller.get_user(call.message.chat.id)
        for i, url in enumerate(user['news_urls']):
            if call.data == "button{}".format(i):
                instant_view = parse.create_instant_view(url)
                if len(instant_view) == 2:
                    bot.send_message(call.message.chat.id, instant_view[0])
                    sleep(1)
                    bot.send_message(call.message.chat.id, instant_view[1])
                else:
                    bot.send_message(call.message.chat.id, instant_view)
Exemple #10
0
def get_match_link(user):
    user = users_controller.get_user(user.id)
    url = get_team_foot_url(user)

    page = requests.get(url)
    html = page.text

    soup = BeautifulSoup(html, 'lxml')

    match = soup.find_all('table', class_='feed-table')[1].find_all('tr')[1]
    match_link = match.find('td', class_='score').find('a')['href']

    return match_link
Exemple #11
0
 def choose_team_state(self, message, first_entry=False):
     user = users_controller.get_user(message.chat.id)
     lang = user['language']
     if first_entry:
         self.bot.send_message(message.chat.id,
                               LANG_DICT[lang]['choose_team_msg'],
                               reply_markup=keyboards.set_teams_keyboard(
                                   lang, message.chat.id))
     else:
         if message.text in parse.get_teams_list(message.chat.id):
             users_controller.set_team(message.chat.id, message.text)
             self.go_to_state(message, 'start')
         elif message.text == LANG_DICT[lang]['return_btn']:
             self.go_to_state(message, 'choose_champ_state')
         else:
             self.go_to_state(message, 'choose_team_state')
Exemple #12
0
    def choose_champ_state(self, message, first_entry=False):
        user = users_controller.get_user(message.chat.id)
        lang = user['language']
        if first_entry:
            self.bot.send_message(
                message.chat.id,
                LANG_DICT[lang]['choose_champ_msg'],
                reply_markup=keyboards.set_champs_keyboard(lang))
        else:
            if message.text in CHAMPIONATS_DICT[lang].keys():
                users_controller.set_champ(message.chat.id, message.text)
                self.go_to_state(message, 'choose_team_state')

            elif message.text == LANG_DICT[lang]['return_btn']:
                self.go_to_state(message, 'settings_state')
            else:
                self.go_to_state(message, 'choose_champ_state')
Exemple #13
0
def get_teams_squad(user_id):
    user = users_controller.get_user(user_id)
    url = get_team_foot_url(user)
    countries_dict = bot_methods.get_countries_dict()
    lang = user.language

    message_text = ''
    squad_positions = LANG_DICT[lang]['positions']

    page = requests.get(url)
    html = page.text

    soup = BeautifulSoup(html, 'lxml')

    squad_block = soup.find('article', class_='team-consist')

    if not squad_block:
        return LANG_DICT[lang]['no_squad_msg']

    for i, position_table in enumerate(
            squad_block.find_all('table', class_='consist-table')):
        message_text += squad_positions[i]

        footballers = position_table.find_all('tr')
        country_emoji = ''

        footballer_names_str = '\n'.join([
            footballer.find('td', class_='num').get_text() + '. ' +
            footballer.find('a').get_text() for footballer in footballers
        ])
        if lang == 'ua':
            translated_names = translator.translate(footballer_names_str,
                                                    src='ru',
                                                    dest='uk').text.split('\n')
        else:
            translated_names = footballer_names_str.split('\n')

        for trans_footballer in translated_names:
            num = trans_footballer.split('.')[0]
            name = trans_footballer.split('.')[1].strip()
            footballer = [
                footballer for footballer in footballers
                if footballer.find('td', class_='num').get_text() == num
            ][0]
            try:
                birth_date = footballer.find(
                    'td', class_='birth').find('p').get_text()
            except:
                birth_date = LANG_DICT[lang]['not_mentioned_str']
            country_name = footballer.find('img')['alt']

            for key in countries_dict:
                if countries_dict[key] == country_name:
                    country_code = key
                    country_emoji = flag.flagize(':{}:'.format(country_code))

            message_text += '{}. {} {} (_{}_)\n'.format(
                num, country_emoji, name, birth_date)
        message_text += '\n'

    return message_text