def run_all(): order = settings.ORDER guess = 0 if settings.ODDS and not settings.DEBUG: guess = random.randint(0, settings.ODDS - 1) if guess is not 0: # message if the random number fails. print(f"{guess} No, sorry, not this time.") return else: twitter_api = connect() mine = markov.MarkovChainer(order) seed_chainer(chainer=mine, twitter_api=twitter_api) if settings.REPLY_TO_MENTIONS: handle_mentions(twitter_api, chainer=mine) rtn_bool, *other_rtns = mine.new_phrase() if rtn_bool and other_rtns[0]: msg = other_rtns[0] if not settings.DEBUG: if settings.ENABLE_TWITTER_POSTING: twitter_api.PostUpdate(msg) if settings.ENABLE_MASTODON_POSTING: pass print(msg) if not rtn_bool: print("Couldn't generate message")
def generateTweets(consumer_key, consumer_secret, target, access_key, access_secret): order = 2 build_corpus(consumer_key, consumer_secret, target, access_key, access_secret) mine = markov.MarkovChainer(order) for tweet in tweet_texts: if re.search('([\.\!\?\"\']$)', tweet): pass else: tweet += "." mine.add_text(tweet) for x in range(0, 10): tweet = mine.generate_sentence() if random.randint(0, 4) == 0 and re.search( r'(in|to|from|for|with|by|our|of|your|around|under|beyond)\s\w+$', tweet) != None: tweet = re.sub(r'\s\w+.$', '', tweet) #if a tweet is very short, this will randomly add a second sentence to it. if tweet != None and len(tweet) < 40: rando = random.randint(0, 10) if rando == 0 or rando == 7: newer_tweet = mine.generate_sentence() if newer_tweet != None: tweet += " " + mine.generate_sentence() else: tweet = tweet elif rando == 1: tweet = tweet.upper() print tweet return tweet
def generate_tweet(source_tweets, length="long", reply_to_user=None, reply_to_id = None, mentioned_users=None): mMarkov = markov.MarkovChainer(2) if initialize_markov(mMarkov, source_tweets) == None: return "" if length == "short" : mintweetlength = SHORTTWEET else: mintweetlength = LONGTWEET # print(source_tweets) tweet_text = generate_fragment(mMarkov) #if a tweet is very short, this will randomly add a second sentence to it. while len(tweet_text) < mintweetlength: newer_tweet = tweet_text + " " + generate_fragment(mMarkov) if len(newer_tweet) < MAXTWEETLENGTH: tweet_text = newer_tweet #throw out tweets that match anything from the source account. for user_tweet in source_tweets: if tweet_text[:-1] in filter_tweet(user_tweet): add_text = generate_fragment(mMarkov) if add_text.lower()[:-1] != tweet_text[:-1].lower(): tweet_text = tweet_text + " " + add_text if tweet_text == None: print "Tweet is empty, sorry." return "" if reply_to_user != None and reply_to_id != None: reply_text = u"@"+reply_to_user+u" " for user in mentioned_users: reply_text = reply_text + u"@"+user+u" " tweet_text = reply_text + tweet_text if len(tweet_text) >= MAXTWEETLENGTH: tweet_text = tweet_text[:MAXTWEETLENGTH-1] + u"\u2026" post_tweet(tweet_text, reply_to_id) return tweet_text
import helpers import markov import random # Pull in the config json file and parse it into a dict config_txtfile = open("config/config_secret.json", "rt+") config = json.load(config_txtfile) config_txtfile.close() # Start the bot twbot = commands.Bot(irc_token=config["twitch_irc_token"], client_id=config["twitch_client_id"], nick=config["twitch_bot_username"], prefix=config["twitch_bot_prefix"], initial_channels=config["twitch_initial_channels"]) m = markov.MarkovChainer(2) helpers.markov_startup(m) # bot commands below this line @twbot.event async def event_ready(): print("bot ready......") ws = twbot._ws await ws.send_privmsg(config["twitch_initial_channels"][0], f"/me has Logged In. Howdy gamers.") return @twbot.command(name="h") async def bothelp(ctx):
source_tweets = item.split(",") else: source_tweets = [] for handle in SOURCE_ACCOUNTS: user = handle api = connect() max_id = None for x in range(17)[1:]: source_tweets_iter, max_id = grab_tweets(api, max_id) source_tweets += source_tweets_iter print "{0} tweets found in {1}".format(len(source_tweets), handle) if len(source_tweets) == 0: print "Error fetching tweets from Twitter. Aborting." sys.exit() mine = markov.MarkovChainer(order) for tweet in source_tweets: if re.search('([\.\!\?\"\']$)', tweet): pass else: tweet += "." mine.add_text(tweet) for x in range(0, 10): ebook_tweet = mine.generate_sentence() #randomly drop the last word, as Horse_ebooks appears to do. if random.randint(0, 4) == 0 and re.search( r'(in|to|from|for|with|by|our|of|your|around|under|beyond)\s\w+$', ebook_tweet) != None: print "Losing last word randomly"
# Get x pages of tweets. for x in range(2)[1:]: source_tweets_iter, max_id = grab_tweets(api, max_id) # grab_tweets will return max_id==None if there's no more to get. This is ok. if max_id is None: break source_tweets += source_tweets_iter print "Running total: {0} tweets. Added more from {1}".format( len(source_tweets), handle) if len(source_tweets) == 0: print "Error fetching tweets from Twitter. Aborting." sys.exit() mine = markov.MarkovChainer(ORDER) counter = 0 for tweet in source_tweets: tweet += "." mine.add_text(tweet) for x in range(0, 10): ebook_tweet = mine.generate_sentence() #randomly drop the last word, as Horse_ebooks appears to do. if random.randint(0, 4) == 0 and re.search( r'(in|to|from|for|with|by|our|of|your|around|under|beyond)\s\w+$', ebook_tweet) != None: print "Losing last word randomly" ebook_tweet = re.sub(r'\s\w+.$', '', ebook_tweet)
def markov_tweet(): """Publishes tweets generated from Markov chains""" acc1 = random.choice(ACCOUNTS) acc2 = random.choice(ACCOUNTS) while acc1 == acc2: acc2 = random.choice(ACCOUNTS) corpus = [] cprint('Mode: Markov', 'yellow') cprint('Getting tweets...', 'yellow') if M_COUNT >= 500: cprint('Buckle in, this could take a while! ', 'yellow') # Generates a corpus based on two twitter accounts corpus_tweets1 = list( filter(None, [ clean(t.full_text) for t in tweepy.Cursor(API.user_timeline, id=acc1, tweet_mode="extended").items(M_COUNT) ])) cprint('Adding %s to corpus' % (acc1, ), 'yellow') if not corpus_tweets1: cprint( 'GetTweetsError: wtf, corpus_tweets1 is empty, this is very unusual. Check: ' + acc1) logging.error( str('GetTweetsError: corpus_tweets1 is empty, this is very unusual. Check: ' + acc1)) raise GetTweetsError else: corpus += corpus_tweets1 corpus_tweets2 = list( filter(None, [ clean(t.full_text) for t in tweepy.Cursor(API.user_timeline, id=acc2, tweet_mode="extended").items(M_COUNT) ])) cprint('Adding %s to corpus' % (acc2, ), 'yellow') if not corpus_tweets2: cprint( 'GetTweetsError: wtf, corpus_tweets2 is empty, this is very unusual. Check: ' + acc2) logging.error( str('GetTweetsError: corpus_tweets2 is empty, this is very unusual. Check: ' + acc2)) raise GetTweetsError else: corpus += corpus_tweets2 cprint('Corpus complete!', 'yellow') if len(corpus) == 0: cprint('GetTweetsError: Aw hell naw, no statuses found in corpus!', 'red') logging.error(str('GetTweetsError: No statuses found in corpus!')) raise GetTweetsError markov_tweet() mine = markov.MarkovChainer(ORDER) for status in corpus: if not re.search('([\.\!\?\"\']$)', status): status += "." mine.add_text(status) for x in range(0, 10): newtweet = mine.generate_sentence() # randomly drop the last word, like the horse_ebooks of yore. if random.randint(0, 4) == 0 and re.search( r'(in|to|from|for|with|by|our|of|your|around|under|beyond)\s\w+$', newtweet) is not None: cprint('Losing last word randomly', 'yellow') newtweet = re.sub(r'\s\w+.$', '', newtweet) # if a tweet is very short, this will add a second sentence to it. if newtweet is not None and len(newtweet) < 40: cprint('Short tweet. Adding another sentence.', 'yellow') newer_status = mine.generate_sentence() if newer_status is not None: newtweet += " " + mine.generate_sentence() else: newtweet = newtweet elif random.random() < UPPER_PROB: newtweet = newtweet.upper() # SAVING, UPLOADING & CONSOLE OUTPUT cprint('Saving to database...', 'yellow') tweet_type = "markov" save_tweet(newtweet, tweet_type) cprint('Updating status...', 'yellow') newtweet = html.unescape(newtweet) API.update_status(status=newtweet) cprint('Markov - ', 'magenta', end='') cprint(newtweet, 'cyan') logging.info('\t|\t'.join((newtweet)))