tweet_reply.py 파일을 새로 만들었다

 

import tweepy
import logging

import credentials

api_key = credentials.api_key
api_secret_key = credentials.api_secret_key
access_token = credentials.access_token
access_token_secret = credentials.access_token_secret

auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

# For adding logs in application
logger = logging.getLogger()
logging.basicConfig(level=logging.INFO)
logger.setLevel(logging.INFO)

def get_last_tweet(file):
    f = open(file, 'r')
    lastId = int(f.read().strip())
    f.close()
    return lastId

def put_last_tweet(file, Id):
    f = open(file, 'w')
    f.write(str(Id))
    f.close()
    logger.info("Updated the file with the latest tweet Id")
    return

def respondToTweet(file='tweet_ID.txt'):
    put_last_tweet(file, 0)
    last_id = get_last_tweet(file)
    mentions = api.mentions_timeline()
    if len(mentions) == 0:
        return

    new_id = 0
    logger.info("someone mentioned me...")

    for mention in reversed(mentions):
        logger.info(str(mention.id) + '-' + mention.text)
        new_id = mention.id
        mention_text = mention.text

        logger.info("Responding back to -{}".format(mention.id))
        try:
            logger.info("liking and replying to tweet")

            api.create_favorite(mention.id)
            api.update_status('@' + mention.user.screen_name + " 안디", in_reply_to_status_id=mention.id)
        except:
            logger.info("Already replied to {}".format(mention.id))

    put_last_tweet(file, new_id)

if __name__=="__main__":
    respondToTweet()

이 상태서 실행하면 지금까지 나를 멘션한 사람들한테 "안디"라고 답장을 하면서 좋아요를 누른다

중복을 방지하기 위해 멘션된/될 사람은 tweet_ID.txt에 저장된다

 

다음에 할 것은 koBERT 모델 붙이고 AWS 배포!

복사했습니다!