Skip to content

Instantly share code, notes, and snippets.

@busterbenson
Created September 25, 2013 05:05
Show Gist options
  • Save busterbenson/6695350 to your computer and use it in GitHub Desktop.
Save busterbenson/6695350 to your computer and use it in GitHub Desktop.
How I do @buster_ebooks.
#!/usr/bin/ruby
# Make sure you have these gems installed
require 'rubygems'
require 'thread'
require 'csv'
require 'twitter'
require 'marky_markov'
# Create a new Twitter account that you'd like to have your auto-tweets posted to
# Go to dev.twitter.com, create a new application with Read+Write permissions
# Create an access token + secret for the account and copy that and the consumer key and secrets here.
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''
PATH_TO_TWEETS_CSV = 'tweets.csv'
PATH_TO_TWEETS_CLEAN = 'markov_dict.txt'
### -----------------------------------------------------------------------------------------------------
# Go to Twitter.com -> Settings -> Download Archive.
# This tweets.csv file is in the top directory. Put it in the same directory as this script.
csv_text = CSV.parse(File.read(PATH_TO_TWEETS_CSV))
# Create a new clean file of text that acts as the seed for your Markov chains
File.open(PATH_TO_TWEETS_CLEAN, 'w') do |file|
csv_text.reverse.each do |row|
# Strip links and new lines
tweet_text = row[5].gsub(/(?:f|ht)tps?:\/[^\s]+/, '').gsub(/\n/,' ')
# Save the text
file.write("#{tweet_text}\n")
end
end
# Run when you want to generate a new Markov tweet
markov = MarkyMarkov::Dictionary.new('dictionary') # Saves/opens dictionary.mmd
markov.parse_file PATH_TO_TWEETS_CLEAN
tweet_text = markov.generate_n_sentences(2).split(/\#\</).first.chomp.chop
markov.save_dictionary!
# Connect to your Twitter account
Twitter.configure do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
end
twitter_client = Twitter::Client.new(:oauth_token => ACCESS_TOKEN,
:oauth_token_secret => ACCESS_TOKEN_SECRET)
p "#{Time.now}: #{tweet_text}"
twitter_client.update(tweet_text)
@parkr
Copy link

parkr commented Sep 29, 2013

If you'd like a gem version, check out https://github.com/parkr/ebooks

@peteyreplies
Copy link

buster, parkr - what does dictionary.mmd do? After three iterations, mine is 66 megabytes (I have a large corpus), and I'm trying to figure out what to do to keep it manageable.

e: looks like there is an option in markymarkov to replace permanent dictionary w/ a temp one: https://github.com/zolrath/marky_markov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment