Commit | Line | Data |
---|---|---|
f9ace606 IK |
1 | #!/usr/bin/env python3 |
2 | ||
3 | # based on | |
4 | # https://github.com/python-twitter-tools/twitter | |
5 | ||
6 | import sys | |
7 | import os | |
8 | from twitter import * | |
9 | import fileinput | |
10 | ||
11 | # if needed for development, just hardcode | |
12 | #oauth_token = "" | |
13 | #oauth_secret = "" | |
14 | ||
15 | ||
16 | first = True | |
213207f1 | 17 | with fileinput.input(files=(os.environ.get('HOME') + os.sep + '.rainbow_oauth')) as f: |
f9ace606 IK |
18 | # ok, this is dumb, but it works |
19 | for line in f: | |
20 | if first: | |
21 | oauth_token = line.rstrip() | |
22 | first = False | |
23 | else: | |
24 | oauth_secret = line.rstrip() | |
25 | ||
26 | CONSUMER_KEY = 'hJHyPhuU7nSVHrKTVsGVDM4Lw' | |
27 | CONSUMER_SECRET = '8UCYePqc1y9DY6mg0yQzLoTuq57AIysype2Si63714uACGMCbO' | |
28 | ||
29 | t = Twitter(auth=OAuth(oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET)) | |
30 | ||
31 | tweet_text = input() | |
32 | have_image = False | |
33 | try: | |
34 | image_path = input() | |
35 | have_image = True | |
36 | except: | |
37 | pass | |
38 | ||
39 | have_alt = False | |
40 | try: | |
41 | alt = input() | |
42 | have_alt = True | |
43 | except: | |
44 | pass | |
45 | ||
46 | if have_image: | |
47 | with open(image_path, "rb") as imagefile: | |
48 | imagedata = imagefile.read() | |
49 | t_upload = Twitter(domain='upload.twitter.com', | |
50 | auth=OAuth(oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET)) | |
51 | id_img = t_upload.media.upload(media=imagedata)["media_id_string"] | |
52 | ||
53 | if have_alt: | |
54 | t_upload.media.metadata.create(media_id=id_img, text=alt) | |
55 | ||
56 | if have_image: | |
57 | t.statuses.update(status=tweet_text, media_ids=id_img) | |
58 | else: | |
59 | t.statuses.update(status=tweet_text) |