Last active
December 19, 2016 02:52
-
-
Save rsnorman/3f49daa18b380fb5b93036bd79e5e2d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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