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

Code before:
        t.mentions = []
    try:
        t.urls = [_url['expanded_url'] for _url in tw['entities']['urls']]
    except KeyError:
        t.urls = []
    try:
        t.photos = [_img['media_url_https'] for _img in tw['entities']['media'] if _img['type'] == 'photo' and
                    _img['expanded_url'].find('/photo/') != -1]
    except KeyError:
        t.photos = []
    try:
        t.video = 1 if len(tw['extended_entities']['media']) else 0
    except KeyError:
        t.video = 0
    try:
        t.thumbnail = tw['extended_entities']['media'][0]['media_url_https']
    except KeyError:
        t.thumbnail = ''
    t.tweet = tw['full_text']
    t.lang = tw['lang']
    try:
        t.hashtags = [hashtag['text'] for hashtag in tw['entities']['hashtags']]
    except KeyError:
        t.hashtags = []
    # don't know what this is
    t.cashtags = [cashtag for cashtag in cashtag_pattern.findall(tw['full_text'])]
    t.replies_count = tw['reply_count']
    t.retweets_count = tw['retweet_count']
    t.likes_count = tw['favorite_count']
    t.link = f"https://twitter.com/{t.username}/status/{t.id}"
    # TODO: someone who is familiar with this code, needs to take a look at what this is
    # t.user_rt_id, t.user_rt = getRetweet(tw, config)
    # t.retweet = True if t.user_rt else False
    # t.retweet_id = ''
    # t.retweet_date = ''
    # if not config.Profile:
    #     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 ''
    try:
        t.quote_url = tw['quoted_status_permalink']['expanded'] if tw['is_quote_status'] else ''
    except KeyError:
        # means that the quoted tweet have been deleted
        t.quote_url = 0
    t.near = config.Near if config.Near else ""
    t.geo = config.Geo if config.Geo else ""
    t.source = config.Source if config.Source else ""
    # TODO: check this whether we need the list of all the users to whom this tweet is a reply or we only need
    #  the immediately above user id
    t.reply_to = {'user_id': tw['in_reply_to_user_id_str'], 'username': tw['in_reply_to_screen_name']}
    t.translate = ''

Review comment (suggestion):
simply replace this line with this: `t.cashtags = [cashtag['text'] for cashtag in tw['entities']['symbols']]`
this is because the data we fetch from twitter already contains thes _cashtags__ in the JSON Object.
all the above code (regex.py and import) won't be required

Code after:
    except KeyError:
        t.mentions = []
    try:
        t.urls = [_url['expanded_url'] for _url in tw['entities']['urls']]
    except KeyError:
        t.urls = []
    try:
        t.photos = [_img['media_url_https'] for _img in tw['entities']['media'] if _img['type'] == 'photo' and
                    _img['expanded_url'].find('/photo/') != -1]
    except KeyError:
        t.photos = []
    try:
        t.video = 1 if len(tw['extended_entities']['media']) else 0
    except KeyError:
        t.video = 0
    try:
        t.thumbnail = tw['extended_entities']['media'][0]['media_url_https']
    except KeyError:
        t.thumbnail = ''
    t.tweet = tw['full_text']
    t.lang = tw['lang']
    try:
        t.hashtags = [hashtag['text'] for hashtag in tw['entities']['hashtags']]
    except KeyError:
        t.hashtags = []
    # don't know what this is
    t.cashtags = [cashtag['text'] for cashtag in tw['entities']['symbols']]
    t.replies_count = tw['reply_count']
    t.retweets_count = tw['retweet_count']
    t.likes_count = tw['favorite_count']
    t.link = f"https://twitter.com/{t.username}/status/{t.id}"
    # TODO: someone who is familiar with this code, needs to take a look at what this is
    # t.user_rt_id, t.user_rt = getRetweet(tw, config)
    # t.retweet = True if t.user_rt else False
    # t.retweet_id = ''
    # t.retweet_date = ''
    # if not config.Profile:
    #     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 ''
    try:
        t.quote_url = tw['quoted_status_permalink']['expanded'] if tw['is_quote_status'] else ''
    except KeyError:
        # means that the quoted tweet have been deleted
        t.quote_url = 0
    t.near = config.Near if config.Near else ""
    t.geo = config.Geo if config.Geo else ""
    t.source = config.Source if config.Source else ""
    # TODO: check this whether we need the list of all the users to whom this tweet is a reply or we only need
    #  the immediately above user id
    t.reply_to = {'user_id': tw['in_reply_to_user_id_str'], 'username': tw['in_reply_to_screen_name']}