-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweet.py
45 lines (35 loc) · 1.7 KB
/
tweet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
import json
import os
import twitter
CREDENTIAL_FILE = "twitter_client_secret.json"
class Tweeter:
def __init__(self, client_secret_file=None, credentials_file=None):
if client_secret_file is None:
client_secret_file = CREDENTIAL_FILE
client_secret = json.load(open(client_secret_file))
if credentials_file is None:
credentials_file = os.path.expanduser('~/.credentials/twitter_credentials')
if not os.path.exists(credentials_file):
twitter.oauth_dance("/r/nfl draft cards", client_secret['consumer_key'], client_secret['consumer_secret'],
credentials_file)
oauth_token, oauth_secret = twitter.read_token_file(credentials_file)
self.auth = twitter.OAuth(oauth_token, oauth_secret, client_secret['consumer_key'],
client_secret['consumer_secret'])
self.api = twitter.Twitter(auth=self.auth, retry=20)
self.upload = twitter.Twitter(domain='upload.twitter.com', auth=self.auth, retry=True)
def tweet(self, status: str, imagedata: bytes = None) -> dict:
image_ids = []
if imagedata is not None:
image = self.upload.media.upload(media=imagedata)
image_ids.append(image["media_id_string"])
# self.upload.media.metadata.create(media_id=image["media_id_string"], text="metadata")
return self.api.statuses.update(status=status, media_ids=",".join(image_ids))
if __name__ == '__main__':
import sys
t = Tweeter()
imagedata = None
if len(sys.argv) > 2:
with open(sys.argv[2], 'rb') as fp:
imagedata = fp.read()
t.tweet(sys.argv[1], imagedata)