Skip to content

Instantly share code, notes, and snippets.

@soh335
Created October 8, 2009 03:02
Show Gist options
  • Save soh335/204673 to your computer and use it in GitHub Desktop.
Save soh335/204673 to your computer and use it in GitHub Desktop.
ruby main.rb go
require 'yaml'
require 'MeCab'
require 'pp'
require 'rubygems'
require 'twitter'
class MyTwitter
def initialize
config = "setting.yml"
begin
yaml = YAML.load_file(config)
rescue Errno::ENOENT
puts "#{config} is not found"
exit
end
@database = yaml['database'] || self.alert('database')
@last_file = yaml['last'] || self.alert('last')
@username = yaml['twitter']['username'] || self.alert('twitter username')
@password = yaml['twitter']['password'] || self.alert('twitter password')
@exclude_names = yaml['exclude_names'] || Array.new
@hashtag = yaml['hashtag'] || self.alert('hashtag')
@follow_reply = yaml['follow_reply'] || false
@timeline_follow = yaml['timeline_follow'] || false
end
def stack_ids
@stack_ids = @stack_ids || Array.new
end
def twitter
@twitter = @twitter || Twitter::Base.new(Twitter::HTTPAuth.new(@username, @password))
end
def friend_ids
@friend_ids = @friend_ids || self.twitter.friend_ids
end
def diff_follow
diff_ids = self.friend_ids - self.twitter.follower_ids
diff_ids.each do |id|
self.stack_id(id)
end
end
def stack_id(id)
self.stack_ids.push(id) if !self.friend_ids.index(id) && !self.stack_ids.index(id)
end
def stack_follow
self.stack_ids.each do |id|
self.twitter.friendship_create(id, true)
puts "follow: #{id}"
sleep(1)
end
end
def run str
self.diff_follow if @follow_reply
data = self.crawl
if str == "go"
word = self.markov data
puts "markov word: #{word}"
self.post word
end
self.stack_follow
end
protected
def alert str
puts "#{str} is not found"
puts "exit"
end
def save_last str
file = File.open(@last_file, 'w')
file.puts str
file.close
end
def get_last
begin
f = open(@last_file)
last = f.read.chomp.to_i
f.close
rescue
last = nil
end
return last
end
def save_data data
f = open(@database, 'w')
f.puts Marshal.dump(data)
f.close
end
def get_data
return (File.file? @database) ? Marshal.restore(open(@database, 'r')) : Array.new
end
def post str
if str.split(//u).size <= 140
self.twitter.update(str)
else
puts "over 140"
end
end
def crawl
page = 1
first = true
modify = false
last = self.get_last
mecab = MeCab::Tagger.new("-Owakati")
data = self.get_data
catch :done do
while(true)
results = Twitter::Search.new(@hashtag).page(page).fetch().results
break if results.empty?
puts "Loading #{page} page"
results.each do |result|
next if @exclude_names.index result.from_user
throw :done if result.id <= last
self.stack_id Twitter.user(result.from_user).id if @timeline_follow
puts "new twiit: #{result.text}"
modify = true
save_last result.id if first == true
first = false
mecab.parse('HEAD' + result.text.sub(/#{@hashtag}/, '') + "EOS").split(' ').each_cons(3) do |a|
data.push h = {'head' => a[0], 'middle' => a[1], 'end' => a[2]}
end
end
page += 1
end
end
self.save_data data if modify
return data
end
def markov data
t1 = ''
t2 = ''
data.sort_by{rand}.each do |a|
if a['head'] == 'HEAD'
next if a['middle'] =‾ /^@.*$/
t1 = a['middle']
t2 = a['end']
break
end
end
new_text = t1 + t2
while true
_a = Array.new
data.each do |hash|
_a.push hash if hash['head'] == t1 && hash['middle'] == t2
end
break if _a.size == 0
num = rand(_a.size)
new_text = new_text + _a[num]['end']
break if _a[num]['end'] == "EOS"
t1 = _a[num]['middle']
t2 = _a[num]['end']
end
word = new_text.gsub(/EOS$/, " #{@hashtag}")
return word
end
end
main = MyTwitter.new
main.run ARGV.shift
database: 'data'
last: 'last'
exclude_names: []
twitter:
username: "username"
password: "password"
hashtag: "#hashtag"
follow_reply: false
timeline_follow: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment