-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitterApi.py
40 lines (32 loc) · 1.44 KB
/
TwitterApi.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
import tweepy
import logging
class TwitterApi:
logger = logging.getLogger()
def get_api(Self, consumer_key, consumer_secret, access_token, access_token_secret):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
api.verify_credentials()
Self.logger.info("API created")
return api
def media_upload(Self,api,filename):
media = api.media_upload(filename, chunked=True, media_category='tweet_video')
return media
def update_status(Self, api,tweettext,id, media):
status = api.update_status(auto_populate_reply_metadata=False, status=tweettext.strip(), media_ids=[media.media_id], in_reply_to_status_id=id)
return status
def reply(Self,api,tweet,text):
logger = logging.getLogger()
msg=" "
for line in text.split("\n"):
if "ERROR" in line or "error:" in line:
msg=msg+line+"\n"
tweettext = f"@{tweet.user.screen_name} \n {msg}"
logger.info(f"MSG: {tweettext}")
status = {}
try:
status = Self.update_status(api,tweettext.strip(),tweet.id)
except:
logger.error(f"Unable to post message: {status}")
def get_replies(Self, api, since_id):
return tweepy.Cursor(api.mentions_timeline, since_id=since_id, tweet_mode='extended', include_entities=True).items()