Repository: twintproject/twint
File: twint/tweet.py (Python)

Code before:
    t.hashtags = [hashtag.text for hashtag in tw.find_all("a","twitter-hashtag")]
    t.cashtags = [cashtag.text for cashtag in tw.find_all("a", "twitter-cashtag")]
    t.replies_count = getStat(tw, "reply")
    t.retweets_count = getStat(tw, "retweet")
    t.likes_count = getStat(tw, "favorite")
    t.link = f"https://twitter.com/{t.username}/status/{t.id}"
    t.user_rt_id, t.user_rt = getRetweet(tw)
    t.retweet = True if t.user_rt else False
    t.retweet_id = tw['data-retweet-id'] if t.user_rt else ''
    t.retweet_date = datetime.fromtimestamp(((int(t.retweet_id) >> 22) + 1288834974657)/1000.0).strftime("%Y-%m-%d %H:%M:%S") if t.user_rt else ''
    t.quote_url = getQuoteURL(tw)
    t.near = config.Near if config.Near else ""
    t.geo = config.Geo if config.Geo else ""
    t.source = config.Source if config.Source else ""
    t.reply_to = [{'user_id': t['id_str'], 'username': t['screen_name']} for t in json.loads(tw["data-reply-to-users-json"])]
    t.translate = ''
    t.trans_src = ''
    t.trans_dest = ''
    if config.Translate == True:
        try:
            # config.Translate
            ts = translator.translate(text=t.tweet, dest=config.TranslateDest)
            t.translate = str(ts.text)
            t.trans_src = str(ts.src)
            t.trans_dest = str(ts.dest)
        except KeyError:
            logme.debug(__name__+':Tweet:translator.translate')
    return t


Review comment (suggestion):
You sure that `KeyError`is the right error to handle? Usually it happens with `dict` variables, my previously described way to handle exceptions was just an example and might be the right exception to handle

Code after:
    t.hashtags = [hashtag.text for hashtag in tw.find_all("a","twitter-hashtag")]
    t.cashtags = [cashtag.text for cashtag in tw.find_all("a", "twitter-cashtag")]
    t.replies_count = getStat(tw, "reply")
    t.retweets_count = getStat(tw, "retweet")
    t.likes_count = getStat(tw, "favorite")
    t.link = f"https://twitter.com/{t.username}/status/{t.id}"
    t.user_rt_id, t.user_rt = getRetweet(tw)
    t.retweet = True if t.user_rt else False
    t.retweet_id = tw['data-retweet-id'] if t.user_rt else ''
    t.retweet_date = datetime.fromtimestamp(((int(t.retweet_id) >> 22) + 1288834974657)/1000.0).strftime("%Y-%m-%d %H:%M:%S") if t.user_rt else ''
    t.quote_url = getQuoteURL(tw)
    t.near = config.Near if config.Near else ""
    t.geo = config.Geo if config.Geo else ""
    t.source = config.Source if config.Source else ""
    t.reply_to = [{'user_id': t['id_str'], 'username': t['screen_name']} for t in json.loads(tw["data-reply-to-users-json"])]
    t.translate = ''
    t.trans_src = ''
    t.trans_dest = ''
    if config.Translate == True:
        try:
            ts = translator.translate(text=t.tweet, dest=config.TranslateDest)
            t.translate = ts.text
            t.trans_src = ts.src
            t.trans_dest = ts.dest
        # ref. https://github.com/SuniTheFish/ChainTranslator/blob/master/ChainTranslator/__main__.py#L31
        except ValueError as e:
            # raise Exception("Invalid destination language: {}".format(config.TranslateDest))
            logme.debug(__name__+':Tweet:translator.translate:'+str(e))
    return t