def __init__(self): """Initiate bot by loading JSON, setting a lock and connecting to the API. """ print("Initiating bot uwu") self.json_lock = threading.Lock() self.markov_lock = threading.Lock() try: with open("data.json", "r") as f: self.data = json.load(f) except IOError: self.data = { "base": input("What account is your ebook based on? "), "keys": { "consumer_token": input("Consumer key "), "consumer_secret": input("Consumer secret "), }, "last_id": 1, "last_reply": 1, "uid": 0, } self.dump() self.api = self.connect() if self.data["uid"] == 0: try: self.data["uid"] = self.api.lookup_users( screen_names=[self.data["base"]])[0].id except tweepy.TweepError as e: print("Couldn't get uid twt") exit() d = date.now() self.wait = 3.6e3 - (60 * d.minute + d.second) self.chain = Markov.Chain() # This really long regex array filters out tags, websites, tickers, # weird quotes, long white space, and beginning spaces. self.ignore = [ r"[ |\.]?(@[A-Za-z0-9_]{1,15})(?![A-Z0-9a-z])", r" ?(https?|www)[A-Za-z0-9:\/\.\-_?=%@~\+]*", r" ?\$[A-Za-z]{1,6}(?![A-Za-z])", r'(?<=\s)"\s', r"^ ", r'"', ] self.space_filter = (r"(?<= ) {1,}", ) self.special = ",.?!:;"
def chunks(lst, n): """Yield successive n-sized chunks from lst.""" for i in range(0, len(lst), n): yield lst[i : i + n] def uni_norm(text): text = text.translate( {0x2018: 0x27, 0x2019: 0x27, 0x201C: 0x22, 0x201D: 0x22, 0xA0: 0x20} ) return unescape(text) chain = Markov.Chain() ignore = [ r"[ |\.]?(@[A-Za-z0-9_]{1,15})(?![A-Z0-9a-z])", r" ?(https?|www)[A-Za-z0-9:\/\.\-_?=%@~\+]*", r" ?\$[A-Za-z]{1,6}(?![A-Za-z])", r'(?<=\s)"\s', r"(?<= ) {1,}", r"^ ", r'"', ] def add_tweets(tweets): """Adds new tweets to the Markov chain.