-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.py
67 lines (59 loc) · 1.59 KB
/
twitter.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import sys
import os
import logging
import tweepy
from dotenv import load_dotenv
import tweepy.client
logger = logging.getLogger("liggma")
try:
load_dotenv()
consumer_key = os.getenv("consumer_key")
consumer_secret = os.getenv("consumer_secret")
access_token = os.getenv("access_token")
access_token_secret = os.getenv("access_token_secret")
except Exception as e:
logger.critical(e)
sys.exit(1)
try:
clientV2 = tweepy.Client(
consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token=access_token, access_token_secret=access_token_secret
)
authV1 = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
api = tweepy.API(authV1)
except Exception as e:
logger.critical(e)
sys.exit(1)
def tweet(content):
try:
res = clientV2.create_tweet(
text=content
)
return res.data['id']
except Exception as e:
logger.critical(e)
sys.exit(1)
def reply(content, id, media_id=""):
try:
if media_id:
res = clientV2.create_tweet(
text=content,
in_reply_to_tweet_id=id,
media_ids=media_id
)
else:
res = clientV2.create_tweet(
text=content,
in_reply_to_tweet_id=id
)
return res.data['id']
except Exception as e:
logger.error(e)
def upload(filename):
try:
res = api.media_upload(filename)
return res.media_id
except Exception as e:
logger.error(e)