top | item 8384800

(no title)

glimcat | 11 years ago

Well here's the essential bits in Python. I leave turning this into an actual CLI as an exercise for the interested reader.

    from requests_oauthlib import OAuth1Session
    import json


    keys = json.loads(open(keyfile).read())
    #  {"TWITTER_CONSUMER_KEY": "blah_blah_blah",
    #   "TWITTER_CONSUMER_SECRET": "blah_blah_blah",
    #   "TWITTER_ACCESS_TOKEN": "blah_blah_blah",
    #   "TWITTER_ACCESS_TOKEN_SECRET": "blah_blah_blah",}


    def twitter():
        return OAuth1Session(keys['TWITTER_CONSUMER_KEY'],
                             client_secret=keys['TWITTER_CONSUMER_SECRET'],
                             resource_owner_key=keys['TWITTER_ACCESS_TOKEN'],
                             resource_owner_secret=keys['TWITTER_ACCESS_TOKEN_SECRET'])


    def tweet(status, reply_to=None):
        endpoint = 'https://api.twitter.com/1.1/statuses/update.json'
        data = {'status': status}
        r = twitter().post(endpoint, data=data)
        return r.status_code


    def tweet_with_image(status, imgfile, reply_to=None):
        endpoint = 'https://api.twitter.com/1.1/statuses/update_with_media.json'
        data = {'status': status}
        if reply_to is not None:
            data.update({'in_reply_to_status_id': reply_to})
        r = twitter().post(endpoint, data=data, files={'media[]': open(imgfile, 'rb').read()})
        return r.status_code

discuss

order

No comments yet.