<commit_before>import twitter
import re
import os

TWITTER_PERMALINK_RE = re.compile("https?://(?:www.)?twitter.com/(\w+)/status/(\w+)")

_api = None
def get_api():
    if not get_api.cached: 
        CONSUMER_KEY = "Cm9wmVhNTgES6xm2mUwRtg"
        CONSUMER_SECRET = "<KEY>"
        MY_TWITTER_CREDS = os.path.expanduser('~/.groomsman/twitter_credentials')
        if not os.path.exists(MY_TWITTER_CREDS):
            twitter.oauth_dance("groomsman-kylewm.com", CONSUMER_KEY, CONSUMER_SECRET,
                                MY_TWITTER_CREDS)
        oauth_token, oauth_secret = twitter.read_token_file(MY_TWITTER_CREDS)
        get_api.cached = twitter.Twitter(auth=twitter.OAuth(oauth_token, oauth_secret,
                                                  CONSUMER_KEY, CONSUMER_SECRET))
    return get_api.cached

get_api.cached = None

def handle_new_or_edit(post):
    api = get_api()
    args = {
        'status' : create_status(post),
        'trim_user' : True
    }
    
    match = TWITTER_PERMALINK_RE.match(post.in_reply_to)
    if match:
        args['in_reply_to_status_id'] = match.group(2)

    result = api.statuses.update(**args)
    if result:
        status_id = result.get('id_str')
        post.twitter_status_id = status_id
        
def create_status(post):
    permalink = post.permalink_short_url<commit_msg>Perform "native" retweets when repost source is a twitter URL<commit_after>import twitter
import re
import os

TWITTER_PERMALINK_RE = re.compile("https?://(?:www.)?twitter.com/(\w+)/status/(\w+)")

def get_api():
    if not get_api.cached: 
        CONSUMER_KEY = "Cm9wmVhNTgES6xm2mUwRtg"
        CONSUMER_SECRET = "<KEY>"
        MY_TWITTER_CREDS = os.path.expanduser('~/.groomsman/twitter_credentials')
        if not os.path.exists(MY_TWITTER_CREDS):
            twitter.oauth_dance("groomsman-kylewm.com", CONSUMER_KEY, CONSUMER_SECRET,
                                MY_TWITTER_CREDS)
        oauth_token, oauth_secret = twitter.read_token_file(MY_TWITTER_CREDS)
        get_api.cached = twitter.Twitter(auth=twitter.OAuth(oauth_token, oauth_secret,
                                                  CONSUMER_KEY, CONSUMER_SECRET))
    return get_api.cached

get_api.cached = None

def handle_new_or_edit(post):
    api = get_api()
    # check for RT's
    match = TWITTER_PERMALINK_RE.match(post.repost_source)
    if match:
        api.statuses.retweet(id=match.group(2), trim_user=True)
    else:
        match = TWITTER_PERMALINK_RE.match(post.in_reply_to)
        in_reply_to = match.group(2) if match else None
        result = api.statuses.update(status=create_status(post),
                                     in_reply_to_status_id=in_reply_to,
                                     trim_user=True)
        if result:
            post.twitter_status_id = result.get('id_str')
        
def create_status(post):
    permalink = post.permalink_short_url
{'fraction_non_alphanumeric': 0.10179434092477571, 'fraction_numerical': 0.003105590062111801, 'mean_word_length': 2.5658056580565805, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3695879', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1017, 'n_words': 147}