post_to_twitter: only post stories once they have reached a vote threshold

This drops storing twitter:last_story_id in Keystore and stores each
story's tweet id, so we can query which stories meet the threshold
and haven't been posted to twitter yet.
This commit is contained in:
joshua stein 2015-02-05 09:58:49 -06:00
parent ba0e1d64d5
commit 73eb9ae662

View file

@ -7,13 +7,19 @@ require File.expand_path('../../config/boot', __FILE__)
require APP_PATH
Rails.application.require_environment!
LAST_STORY_KEY = "twitter:last_story_id"
MIN_STORY_SCORE = 2
last_story_id = (Keystore.value_for(LAST_STORY_KEY) || Story.last.id).to_i
Story.where("is_expired = ? AND #{Story.score_sql} > ? AND " <<
"twitter_id IS NULL AND created_at >= ?", false, MIN_STORY_SCORE,
Time.now - 1.days).order(:id).each_with_index do |s,x|
if s.tags.map(&:tag).include?("meta")
next
end
if x > 0
sleep 2
end
Story.where("id > ? AND is_expired = ?", last_story_id, false).
order(:id).each do |s|
if !s.tags.map(&:tag).include?("meta")
tags = ""
s.sorted_taggings.each do |tagging|
tags += ' #' + tagging.tag.tag
@ -58,8 +64,4 @@ order(:id).each do |s|
"#{res.inspect}"
exit
end
end
Keystore.put(LAST_STORY_KEY, s.id)
sleep 2
end