<commit_before># encoding: utf-8

from utils import get_url
import logging
from pytgbot.bot import Bot
from pytgbot.exceptions import TgApiException

logger = logging.getLogger(__name__)

subreddit = 'unexpected'
t_channel = '@r_unexpected'

NSFW_emoji = u"\U0001F51E"

def send_post(submission, bot):
    bot = Bot(bot._token)
    what, gif_url, _ = get_url(submission)
    if what != 'gif':
        return False

    title = submission.title
    link = submission.short_link

    if submission.over_18:
        url = submission.url
        text = '{emoji}NSFW\n{url}\n{title}\n\n{link}\n\nby {channel}'.format(
            emoji=NSFW_emoji, url=url, title=title, link=link, channel=t_channel
        )
        bot.send_message(t_channel, text, disable_web_page_preview=True)
        return True
    text = '{title}\n{link}\n\nby {channel}'.format(title=title, link=link, channel=t_channel)
    logger.info("{channel} Posting {gif_url}:\n{text}".format(channel=t_channel, gif_url=gif_url, text=text))
    try:
        bot.send_document(t_channel, document=gif_url, caption=text)
    except TgApiException:
        from pytgbot.api_types.sendable.files import InputFileFromURL
        bot.send_document(t_channel, document=InputFileFromURL(gif_url), caption=text)
    return True
<commit_msg>Workaround for strange fails in @r_unexpected.<commit_after># encoding: utf-8

import logging

from pytgbot.bot import Bot
from pytgbot.exceptions import TgApiException

from utils import get_url


logger = logging.getLogger(__name__)


subreddit = 'unexpected'
t_channel = '@r_unexpected'


NSFW_emoji = u'\U0001F51E'


def send_post(submission, bot):
    bot_old = bot
    bot = Bot(bot_old._token)
    what, gif_url, _ = get_url(submission)
    if what != 'gif':
        return False

    title = submission.title
    link = submission.short_link

    if submission.over_18:
        url = submission.url
        text = '{emoji}NSFW\n{url}\n{title}\n\n{link}\n\nby {channel}'.format(
            emoji=NSFW_emoji, url=url, title=title, link=link, channel=t_channel
        )
        bot.send_message(t_channel, text, disable_web_page_preview=True)
        return True
    text = '{title}\n{link}\n\nby {channel}'.format(title=title, link=link, channel=t_channel)
    logger.info("{channel} Posting {gif_url}:\n{text}".format(channel=t_channel, gif_url=gif_url, text=text))
    try:
        bot.send_document(t_channel, document=gif_url, caption=text)
    except TgApiException:
        from pytgbot.api_types.sendable.files import InputFileFromURL
        try:
            bot.send_document(t_channel, document=InputFileFromURL(gif_url), caption=text)
        except:
            import os
            from utils import download_file, telegram_autoplay_limit
            filename = 'r_unexpected.gif'
            if not download_file(gif_url, filename):
                return False
            if os.path.getsize(filename) > telegram_autoplay_limit:
                return False
            f = open(filename, 'rb')
            bot_old.sendDocument(t_channel, f, caption=text)
            f.close()
    return True

{'fraction_non_alphanumeric': 0.10643086816720257, 'fraction_numerical': 0.005787781350482315, 'mean_word_length': 3.255813953488372, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5601832', 'n_tokens_mistral': 1046, 'n_tokens_neox': 1021, 'n_words': 204}