Skip to content

Instantly share code, notes, and snippets.

@rsnorman
Last active December 19, 2016 02:52
Show Gist options
  • Save rsnorman/3f49daa18b380fb5b93036bd79e5e2d8 to your computer and use it in GitHub Desktop.
Save rsnorman/3f49daa18b380fb5b93036bd79e5e2d8 to your computer and use it in GitHub Desktop.
# Get weekly top tracks
albums = WeeklyAnniversaryQuery.new.find_all
# Schedule tweets
# Create task that runs once a week at the beginning to schedule track tweets
albums.each do |album|
# Get Random Datetime
random_day_of_week = (0..6).to_a.sample
random_hour_in_day = (8..20).to_a.sample
random_scheduled_at = Week.current.start + random_day_of_week.days + random_hour_in_day.hours
# Create scheduled tweet entry for top album track
ScheduledTweet.create(type: 'TopSong', scheduled_at: random_scheduled_at, album: album)
end
# Send scheduled tweets
# Create task that runs every ten minutes
scheduled_tweets = ScheduledTweet.where('scheduled_at BETWEEN ? AND ?', Time.current - 5.minutes, Time.current + 5.minutes)
scheduled_tweets.each do |scheduled_tweet|
top_track = TopAlbumTrack.new(album: scheduled_tweet.album).top
tweet = SongTweeter.new(track: top_track).tweet
scheduled_tweet.update(sent: true, twitter_status_id: tweet.id)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment