From c1b56efb28c3a7ba5f198e266255a6c47b7c2699 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 15 May 2015 22:39:23 -0700 Subject: [PATCH] Add a cron job that runs at 5am to refresh the corpus --- bots.rb | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/bots.rb b/bots.rb index 041a778..4faf75b 100644 --- a/bots.rb +++ b/bots.rb @@ -1,5 +1,7 @@ +require 'fileutils' require 'twitter_ebooks' + # Information about a particular Twitter user we know class UserInfo attr_reader :username @@ -14,6 +16,7 @@ class UserInfo end end + class ERWEbooksBot < Ebooks::Bot attr_accessor :original, :model, :model_path @@ -31,10 +34,16 @@ class ERWEbooksBot < Ebooks::Bot def on_startup load_model! - scheduler.cron '0 0 * * *' do - # Each day at midnight, post a single tweet + scheduler.cron '23 6-23 * * *' do + # Post a tweet 23 minutes after every fourth hour. tweet(model.make_statement) end + + scheduler.cron '0 5 * * *' do + # Every day at 5am update the corpus + archive! + load_model! + end end def on_message(dm) @@ -118,13 +127,18 @@ class ERWEbooksBot < Ebooks::Bot private def load_model! - return if @model - @model_path ||= "model/#{original}.model" - log "Loading model #{model_path}" @model = Ebooks::Model.load(model_path) end + + private + def archive! + archive_path = "model/#{original}.json" + log "Archiving tweets #{archive_path}" + Ebooks::Archive.new(@original, archive_path).sync + Ebooks::Model.consume(archive_path).save(model_path) + end end ERWEbooksBot.new("erw_ebooks") do |bot|