tweeper.py 1.03 KB
Newer Older
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
#! /usr/bin/env python
import os
import time

import tweepy
from didyoumean3.didyoumean import did_you_mean
from tweepy import Cursor


class Tweeper(object):
    def __init__(self, name: str):
        auth = tweepy.OAuthHandler(
            os.environ["ZOO_DAWA_KEY"],
            os.environ["ZOO_DAWA_KEY_SECRET"])
        auth.set_access_token(
            os.environ["ZOO_DAWA_TOKEN"],
            os.environ["ZOO_DAWA_TOKEN_SECRET"])
        self.api = tweepy.API(auth)
        self.name = name

    @property
    def all_tweets(self):
        return [t.text for t in Cursor(self.api.user_timeline, id=self.name).items()]

    def tweet(self, message, wait_delay=5, prevent_duplicate=True):
        """Tweets a message after spellchecking it."""
        if prevent_duplicate and message in self.all_tweets:
            print("Was already tweeted: %s." % message)
        else:
            message = did_you_mean(message)
            print("About to tweet:", message)
            time.sleep(wait_delay)
            self.api.update_status(message)