def add_blacklist_url(bot: Bot, update: Update):
    chat = update.effective_chat
    message = update.effective_message
    urls = message.text.split(None, 1)
    if len(urls) > 1:
        urls = urls[1]
        to_blacklist = list(
            set(uri.strip() for uri in urls.split("\n") if uri.strip()))
        blacklisted = []

        for uri in to_blacklist:
            extract_url = tldextract.extract(uri)
            if extract_url.domain and extract_url.suffix:
                blacklisted.append(extract_url.domain + "." +
                                   extract_url.suffix)
                sql.blacklist_url(
                    chat.id, extract_url.domain + "." + extract_url.suffix)

        if len(to_blacklist) == 1:
            extract_url = tldextract.extract(to_blacklist[0])
            if extract_url.domain and extract_url.suffix:
                message.reply_text(tld(
                    chat.id, "url_blacklist_success").format(
                        html.escape(extract_url.domain + "." +
                                    extract_url.suffix)),
                                   parse_mode=ParseMode.HTML)
            else:
                message.reply_text(tld(chat.id, "url_blacklist_invalid"))
        else:
            message.reply_text(tld(chat.id, "url_blacklist_success_2").format(
                len(blacklisted)),
                               parse_mode=ParseMode.HTML)
    else:
        message.reply_text(tld(chat.id, "url_blacklist_invalid_2"))
Example #2
0
def add_blacklist_url(bot: Bot, update: Update):
    chat = update.effective_chat
    message = update.effective_message
    urls = message.text.split(None, 1)
    if len(urls) > 1:
        urls = urls[1]
        to_blacklist = list(set(uri.strip()
                                for uri in urls.split("\n") if uri.strip()))
        blacklisted = []

        for uri in to_blacklist:
            extract_url = tldextract.extract(uri)
            if extract_url.domain and extract_url.suffix:
                blacklisted.append(extract_url.domain + "." + extract_url.suffix)
                sql.blacklist_url(chat.id, extract_url.domain + "." + extract_url.suffix)

        if len(to_blacklist) == 1:
            extract_url = tldextract.extract(to_blacklist[0])
            if extract_url.domain and extract_url.suffix:
                message.reply_text(
                    "Added <code>{}</code> domain to the blacklist!".format(
                        html.escape(
                            extract_url.domain + "." + extract_url.suffix)),
                    parse_mode=ParseMode.HTML)
            else:
                message.reply_text(
                    "You are trying to blacklist an invalid url")
        else:
            message.reply_text(
                "Added <code>{}</code> domains to the blacklist.".format(
                    len(blacklisted)), parse_mode=ParseMode.HTML)
    else:
        message.reply_text(
            "Tell me which urls you would like to add to the blacklist.")