Last active
July 26, 2017 17:31
-
-
Save thomasv314/81a5eeccfa1385fe685794f3b56ef07b to your computer and use it in GitHub Desktop.
This file contains 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
# gem install kappa | |
class FeaturedStreamersController < ApplicationController | |
TWITCH_CACHE_TIME = 5.minutes | |
def index | |
@streamers = load_streams | |
if params[:god_force_refresh] == "true" | |
update_stream | |
flash[:notice] = "Updated stream." | |
end | |
end | |
protected | |
def featured_streamers | |
["bigwhitetrain", "skadoodle", "ESL_CSGO", "imcoty", "callmewizz", "rpr_tv", "KYYR4", "NomadicTV"] | |
end | |
def load_streams | |
last_updated = $redis.get("twitch:last_updated").to_i | |
if Time.now - Time.at(last_updated) > TWITCH_CACHE_TIME | |
json = update_stream | |
else | |
json = JSON.parse($redis.get("twitch:data")) | |
end | |
json['streamers'] | |
end | |
def update_stream | |
streamers = Twitch.streams.find(:channel => featured_streamers) # Online featured streamers | |
json = { streamers: [], updated: Time.now.to_i } | |
streamers.each do |stream| | |
json[:streamers].push({ | |
url: stream.url, | |
preview: stream.preview_url.gsub("http://", "https://"), | |
display_name: stream.user.display_name, | |
game: stream.game_name, | |
viewer_count: stream.viewer_count | |
}) | |
end | |
$redis.set("twitch:last_updated", Time.now.to_i) | |
$redis.set("twitch:data", json.to_json) | |
json.with_indifferent_access | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment