<commit_before>import logging

from hive.models import NotificationLog, NotificationService
from bot.tweepy_api import get_api
from tweepy.error import TweepError

logger = logging.getLogger(__name__)

dm = NotificationService.objects.get(name="dm")

def notify(statusid, json, socialuser, tweetsubscription, dct=None):
    for service in tweetsubscription.service.all().values_list("name", flat=True):
        if service == "dm":
            notify_dm(statusid, json, socialuser, tweetsubscription, dct=dct)
        
def notify_dm(
    statusid,
    json,
    socialuser,
    tweetsubscription,
    bot_screen_name="doctoctocbot2",
    dct=None,
    ):
    logger.debug(
       f"{statusid}, {socialuser}, {tweetsubscription}"    
    )
    api=get_api(username="doctoctocbot2", backend=True)
    try:
        screen_name = json["user"]["screen_name"]
    except:
        screen_name = bot_screen_name
    status_url = "https://twitter.com/{screen_name}/status/{status_id}".format(
        screen_name=screen_name,
        status_id=statusid,
    )
    try:
        status_text = json["full_text"]
    except KeyError:
        status_text = json["text"]
    except:
        status_text = ""
    # human readable category count dictionary
    rt_cat_count = {}
    for i in dct.keys():
        rt_cat_count[dct[i]["name"]]=dct[i]["count"]
    text = f"id: {tweetsubscription.id} " \
           f"lang: {[tli.tag for tli in tweetsubscription.language.all()]} " \
           f"cat: {[cat.name for cat in tweetsubscription.category.all()]} " \
           f"RT: {tweetsubscription.retweet_count} " \
           f"QT: {tweetsubscription.quote_count} \n" \
           f"{str(rt_cat_count)} \n" \
           f"{status_text} \n" \
           f"{status_url}"
    try:
        res = api.send_direct_message(
            recipient_id=socialuser.user_id,
            text=text
        )
        NotificationLog.objects.create(
            statusid=statusid,
            tweetsubscription=tweetsubscription,
            socialuser=socialuser,
            service=dm,
            success=True,
            success_log=res._json
        )
    except TweepError as e:
        # if e.args[0][0]['code'] == 136:
        NotificationLog.objects.create(
            statusid=statusid,
            tweetsubscription=tweetsubscription,
            socialuser=socialuser,
            service=dm,
            success=False,
            error_log=e.args[0][0]
        )<commit_msg>hot fix<commit_after>import logging

from hive.models import NotificationLog, NotificationService
from bot.tweepy_api import get_api
from tweepy.error import TweepError

logger = logging.getLogger(__name__)

def get_dm():
    try:
        return NotificationService.objects.get(name="dm")
    except NotificationService.DoesNotExist:
        return

def notify(statusid, json, socialuser, tweetsubscription, dct=None):
    for service in tweetsubscription.service.all().values_list("name", flat=True):
        if service == "dm":
            notify_dm(statusid, json, socialuser, tweetsubscription, dct=dct)
        
def notify_dm(
    statusid,
    json,
    socialuser,
    tweetsubscription,
    bot_screen_name="doctoctocbot2",
    dct=None,
    ):
    logger.debug(
       f"{statusid}, {socialuser}, {tweetsubscription}"    
    )
    api=get_api(username="doctoctocbot2", backend=True)
    try:
        screen_name = json["user"]["screen_name"]
    except:
        screen_name = bot_screen_name
    status_url = "https://twitter.com/{screen_name}/status/{status_id}".format(
        screen_name=screen_name,
        status_id=statusid,
    )
    try:
        status_text = json["full_text"]
    except KeyError:
        status_text = json["text"]
    except:
        status_text = ""
    # human readable category count dictionary
    rt_cat_count = {}
    for i in dct.keys():
        rt_cat_count[dct[i]["name"]]=dct[i]["count"]
    text = f"id: {tweetsubscription.id} " \
           f"lang: {[tli.tag for tli in tweetsubscription.language.all()]} " \
           f"cat: {[cat.name for cat in tweetsubscription.category.all()]} " \
           f"RT: {tweetsubscription.retweet_count} " \
           f"QT: {tweetsubscription.quote_count} \n" \
           f"{str(rt_cat_count)} \n" \
           f"{status_text} \n" \
           f"{status_url}"
    try:
        res = api.send_direct_message(
            recipient_id=socialuser.user_id,
            text=text
        )
        NotificationLog.objects.create(
            statusid=statusid,
            tweetsubscription=tweetsubscription,
            socialuser=socialuser,
            service=get_dm(),
            success=True,
            success_log=res._json
        )
    except TweepError as e:
        # if e.args[0][0]['code'] == 136:
        NotificationLog.objects.create(
            statusid=statusid,
            tweetsubscription=tweetsubscription,
            socialuser=socialuser,
            service=get_dm(),
            success=False,
            error_log=e.args[0][0]
        )
{'fraction_non_alphanumeric': 0.11474755537816803, 'fraction_numerical': 0.003592097385751347, 'mean_word_length': 2.418826739427012, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2066552', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1594, 'n_words': 273}