예제 #1
0
def handle_button_press(bot, update):
    """
    Handles any button presses via in-place
    message editing
    """
    callback_query = update.callback_query.data.split('@')[0]
    msg_id = update.callback_query.message.message_id
    cht_id = update.callback_query.message.chat.id

    if callback_query in sc.days:
        bot.editMessageText(text=f'{config["en_gb"]["shows_day"]} {callback_query} :',
                            chat_id=cht_id, message_id=msg_id)
        bot.editMessageReplyMarkup(chat_id=cht_id, message_id=msg_id,
                                   reply_markup=build_button_list(show=True, gen_whichday=callback_query, u_id=cht_id))


    elif 'back' in callback_query:
        bot.editMessageText(text=config['en_gb']['pick_day'],
                            chat_id=cht_id, message_id=msg_id)
        bot.editMessageReplyMarkup(chat_id=cht_id, message_id=msg_id, reply_markup=build_button_list(days=True))

    else:
        if check_subscribed(cht_id, get_show_id_by_name(callback_query)):
            day_context = update.callback_query.message.text.split(' ')[5] # what a hack
            remove_subscription(cht_id, get_show_id_by_name(callback_query))
            bot.editMessageReplyMarkup(chat_id=cht_id, message_id=msg_id,
                                       reply_markup=build_button_list(show=True, gen_whichday=day_context,
                                                                      u_id=cht_id))

        else:
            day_context = update.callback_query.message.text.split(' ')[5]
            insert_subscription(cht_id, get_show_id_by_name(callback_query))
            bot.editMessageReplyMarkup(chat_id=cht_id, message_id=msg_id,
                                       reply_markup=build_button_list(show=True, gen_whichday=day_context,
                                                                      rtitle=callback_query, u_id=cht_id))
예제 #2
0
    def iter_schedule(self, days=None):
        if not days:
            days = self.days
        elif not isinstance(days, (list, tuple)):
            days = [days]

        tables = self.tree.select('#main > div > article > div > table')

        for day in days:
            # tables start from 1 rather than from 0, 1 day = 1 table
            dayindex = self.days.index(day)
            rows = tables[dayindex].find_all('tr', recursive=False)

            for item in rows:
                title = item.a.text
                time = item('td')[1].text
                link = f'{self.baselink}{item.a["href"]}'

                show_id = get_show_id_by_name(title)

                if show_id is None:
                    page = requests.get(link).text
                    show_id = re.search(r'var hs_showid = (\d+);', page).group(1)

                yield self.show(show_id, day, title, time, link)
예제 #3
0
def send_notif(bot, show_title):
    logger.info('Send notif entered...')
    logger.info(f'Sending out notifications for {show_title}...')
    for user in return_users_subbed(get_show_id_by_name(show_title)):
        try:
            with Pool(processes=2) as pool:
                show_up_res = pool.apply_async(check_show_up,
                                               (show_title, )).get(timeout=30)
                logger.info(
                    f'{show_title} - result from check_show_up: {show_up_res}')
                if show_up_res:
                    info = pool.apply_async(get_show_ep_magnet,
                                            (show_title, )).get(timeout=30)
                    bot.sendMessage(
                        chat_id=user,
                        text=f'Hello, @{get_username_by_userid(user)}!\n'
                        f'{show_title} - {info[0]} is out!\n'
                        f'• 720p: <a href="{sc.shorten_magnet(info[1])}">click</a>\n'
                        f'• 1080p:  <a href="{sc.shorten_magnet(info[2])}">click</a>',
                        parse_mode=parsemode.ParseMode.HTML,
                        disable_web_page_preview=True)
                else:
                    bot.sendMessage(
                        chat_id=user,
                        text=
                        f"{show_title} was supposed to already be out but it isn't!\n"
                        "It might've finished airing or there might be delays.\n"
                        "For more info, please check the site!")
        except Exception as e:
            logger.warning(f'send_notif failed with exception: {e}')
            pass
예제 #4
0
def build_button_list(days=False, show=False, rtitle=None, gen_whichday=None, u_id=None):
    """
    Generates the appropriate button list depending on the
    given state
    """
    if days:
       return InlineKeyboardMarkup(([[InlineKeyboardButton(day, callback_data=day)] for day in sc.days]))

    if show:
        buttons = []
        backbutton = [InlineKeyboardButton('⏪ Back', callback_data='back')]

        for show in sc.iter_schedule(gen_whichday):
            if rtitle == show.title or check_subscribed(userid=u_id, showid=get_show_id_by_name(show.title)):
                buttons.append([InlineKeyboardButton(f'{show.title}'
                                                     f' @ {show.time} PST ✅',
                                                     callback_data=show.title)])
            else:
                buttons.append([InlineKeyboardButton(f'{show.title} @ {show.time} PST', callback_data=show.title)])

        buttons.append(backbutton)
        return InlineKeyboardMarkup(buttons)

    else:
        return None
예제 #5
0
def get_show_ep_magnet(show_title):
    show_id = get_show_id_by_name(show_title)

    url = f'https://horriblesubs.info/api.php?method=getshows&type=show&showid={show_id}'
    page = requests.get(url)
    soup = BeautifulSoup(page.text, 'lxml')

    episode = soup.select('a.rls-label > strong')[0].text
    magnets = soup.select('span.hs-magnet-link > a')
    magnet720 = magnets[1]['href']
    magnet1080 = magnets[2]['href']
    return episode, magnet720, magnet1080
예제 #6
0
def test_command(bot, update):
    text = get_show_id_by_name('Detetive Conan')
    bot.sendMessage(chat_id=update.message.chat_id, text=text)