#! /usr/bin/env python import os import time import tweepy from didyoumean3.didyoumean import did_you_mean from tweepy import Cursor def get_cred(name: str, kind: str): codenames = { "LeBoulbiNet": "BOULBI", "KoozDawa": "DAWA" } return f"ZOO_{codenames[name]}_{kind.upper()}" class Tweeper(object): def __init__(self, name: str): auth = tweepy.OAuthHandler( os.environ[get_cred(name, "key")], os.environ[get_cred(name, "key_secret")]) auth.set_access_token( os.environ[get_cred(name, "token")], os.environ[get_cred(name, "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) def next_tweet(self, candidates): for txt in candidates: if txt not in self.all_tweets: self.tweet(txt) return print(f"None of these {len(candidates)} candidates has not been tweeted.")