From 73eb9ae6622bf097bb24de9e6e27dc2f3eb2c932 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Thu, 5 Feb 2015 09:58:49 -0600 Subject: [PATCH] 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. --- script/post_to_twitter | 106 +++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 52 deletions(-) diff --git a/script/post_to_twitter b/script/post_to_twitter index 877e5ed..ad82d8b 100755 --- a/script/post_to_twitter +++ b/script/post_to_twitter @@ -7,59 +7,61 @@ 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("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 - end - - tco_status = "\n" + - (s.url.present?? ("X" * Twitter::TCO_LEN) + "\n" : "") + - ("X" * Twitter::TCO_LEN) + - tags - - status = "\n" + - (s.url.present?? s.url + "\n" : "") + - s.short_id_url + - tags - - left_len = Twitter::MAX_TWEET_LEN - tco_status.length - - title = s.title - if title.match(/^([dm] |@)/i) - # prevent these tweets from activating twitter shortcuts - # https://dev.twitter.com/docs/faq#tweeting - title = "- #{title}" - end - - if title.bytesize > left_len - status = title[0, left_len - 3] + "..." + status - else - status = title + status - end - - res = Twitter.oauth_request("/1.1/statuses/update.json", :post, - { "status" => status }) - - begin - if res["id_str"].match(/\d+/) - s.update_column("twitter_id", res["id_str"]) - else - raise - end - rescue => e - puts "failed posting story #{s.id} (#{status.inspect}): #{e.inspect}\n" + - "#{res.inspect}" - exit - end +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 - Keystore.put(LAST_STORY_KEY, s.id) - sleep 2 + if x > 0 + sleep 2 + end + + tags = "" + s.sorted_taggings.each do |tagging| + tags += ' #' + tagging.tag.tag + end + + tco_status = "\n" + + (s.url.present?? ("X" * Twitter::TCO_LEN) + "\n" : "") + + ("X" * Twitter::TCO_LEN) + + tags + + status = "\n" + + (s.url.present?? s.url + "\n" : "") + + s.short_id_url + + tags + + left_len = Twitter::MAX_TWEET_LEN - tco_status.length + + title = s.title + if title.match(/^([dm] |@)/i) + # prevent these tweets from activating twitter shortcuts + # https://dev.twitter.com/docs/faq#tweeting + title = "- #{title}" + end + + if title.bytesize > left_len + status = title[0, left_len - 3] + "..." + status + else + status = title + status + end + + res = Twitter.oauth_request("/1.1/statuses/update.json", :post, + { "status" => status }) + + begin + if res["id_str"].match(/\d+/) + s.update_column("twitter_id", res["id_str"]) + else + raise + end + rescue => e + puts "failed posting story #{s.id} (#{status.inspect}): #{e.inspect}\n" + + "#{res.inspect}" + exit + end end