This repository was archived by the owner on May 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter.py
More file actions
executable file
·66 lines (54 loc) · 2.19 KB
/
Copy pathtwitter.py
File metadata and controls
executable file
·66 lines (54 loc) · 2.19 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import logging
import sys
import tweepy
import config
# v1, still necessary for image upload, see https://github.com/tweepy/tweepy/discussions/1954
auth = tweepy.OAuthHandler(config.twitter_oauth_consumer_key,
config.twitter_oauth_consumer_secret)
auth.set_access_token(config.twitter_oauth_access_token_key,
config.twitter_oauth_access_token_secret)
api = tweepy.API(auth)
# v2
twitter = tweepy.Client(
consumer_key=config.twitter_oauth_consumer_key,
consumer_secret=config.twitter_oauth_consumer_secret,
access_token=config.twitter_oauth_access_token_key,
access_token_secret=config.twitter_oauth_access_token_secret)
def tweet(msg, dry_run=False, img=""):
try:
logging.debug("Tweeting %s: (%s)" % (len(msg), msg))
if dry_run:
return
if not img:
return twitter.create_tweet(text=msg)
else:
media = api.media_upload(img)
return twitter.create_tweet(text=msg, media_ids=[media.media_id])
except Exception as e:
logging.error("There was an exception tweeting: %s" % e)
# TODO: use tweet above
def thread(statuses, start_tweet_id=None):
try:
prev_status_id = start_tweet_id
i = 0
for status in statuses:
if prev_status_id == None:
logging.debug("Tweeting '%s' as first tweet in thread" % status)
new_status = twitter.update_status(status)
else:
logging.debug("(%d) Tweeting '%s' in reply to %d" % (i+1, status, prev_status_id))
new_status = twitter.update_status(status,
in_reply_to_status_id=prev_status_id,
auto_populate_reply_metadata=True)
i += 1
prev_status_id = new_status.id
except Exception as e:
logging.error("There was an exception tweeting: %s" % e)
if __name__ == '__main__':
l = logging.getLogger()
l.addHandler(logging.StreamHandler(sys.stdout))
l.setLevel(logging.DEBUG)
if len(sys.argv) > 1:
tweet(sys.argv[1])
else:
logging.error("No argument, usage: python twitter.py <tweet>")