Skip to content

Commit

Permalink
Add liveStreamingDetails and timezone magic.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Mar 15, 2016
1 parent f61e2c2 commit 6b9651f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
16 changes: 15 additions & 1 deletion app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@
og = OpenGraph.new("https://www.youtube.com/channel/#{channel_id}")
username = og.url.split("/")[-1]
username = og.title if username == channel_id
redirect "/youtube/#{channel_id}/#{username}?eventType=live,upcoming"
url = "/youtube/#{channel_id}/#{username}?eventType=live,upcoming"
url += "&tz=#{params[:tz]}" if params[:tz]
redirect url
else
redirect "https://www.youtube.com/feeds/videos.xml?channel_id=#{channel_id}"
end
Expand All @@ -104,6 +106,7 @@
get "/youtube/:channel_id/:username" do
@channel_id = params[:channel_id]
@username = params[:username]
@tz = params[:tz]

query = { part: "snippet", type: "video", order: "date", channelId: params[:channel_id], maxResults: 50 }
query[:q] = params[:q] if params[:q]
Expand All @@ -119,6 +122,17 @@
@data = response.parsed_response["items"]
end

ids = @data.select { |v| %w[upcoming live].include?(v["snippet"]["liveBroadcastContent"]) }.map { |v| v["id"]["videoId"] }
if ids.any?
request = GoogleParty.get("/youtube/v3/videos", query: { part: "liveStreamingDetails", id: ids.join(",") })
if request.success?
request.parsed_response["items"].each do |data|
i = @data.find_index { |v| v["id"]["videoId"] == data["id"] }
@data[i]["liveStreamingDetails"] = data["liveStreamingDetails"]
end
end
end

content_type :atom
erb :youtube_feed
end
Expand Down
6 changes: 5 additions & 1 deletion config/initializers/05-string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def ustrip
end

def numeric?
self =~ /^\d+$/
/^\d+$/ === self
end

def tz_offset?
/^[-+]?\d+(\.\d+)?$/ === self
end
end
16 changes: 15 additions & 1 deletion config/initializers/05-time.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
class Time
# always output time objects in RFC3339 format
def to_s
self.utc.strftime('%FT%TZ')
self.utc.strftime("%FT%TZ")
end

def readable(tz=nil)
t = self
if tz
if ActiveSupport::TimeZone::MAPPING.flatten.include?(tz)
t = t.in_time_zone(tz)
elsif tz.tz_offset?
# to get this offset from JavaScript, use: -new Date().getTimezoneOffset()/60
# available offsets: ActiveSupport::TimeZone.all.map { |z| z.utc_offset/60/60.0 }.uniq
t = t.in_time_zone(tz.to_f) rescue t
end
end
t.strftime("%F %T %z")
end
end
3 changes: 3 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ $(document).ready(function() {
form = $(this).parents("form");
val = $(this).attr("data-submit-type");
$('<input type="hidden" name="type">').val(val).insertAfter(this);
if (form.attr("action") == "youtube") {
$('<input type="hidden" name="tz">').val(-new Date().getTimezoneOffset()/60).insertAfter(this);
}
form.find("[type=submit]").click();
});
$("[data-download]").click(function() {
Expand Down
14 changes: 11 additions & 3 deletions views/youtube_feed.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@
<title><%= @username %> on YouTube</title>
<icon>https://s.ytimg.com/yts/img/favicon_48-vfl1s0rGh.png</icon>
<link href="<%= request.original_url %>" rel="self" />
<link href="https://youtube.com/channel/<%= @channel_id %>" rel="alternate" />
<link href="https://www.youtube.com/channel/<%= @channel_id %>" rel="alternate" />
<updated><%= @data[0]["snippet"]["publishedAt"] if @data[0] %></updated>
<%- @data.each do |video| -%>

<entry>
<id>youtube:video:<%= video["id"]["videoId"] %><%= ":#{params[:cachebuster]}" if params[:cachebuster] %></id>
<title><%= video["snippet"]["title"].to_line.esc %></title>
<link href="https://youtube.com/watch?v=<%= video["id"]["videoId"] %>" />
<link href="https://www.youtube.com/watch?v=<%= video["id"]["videoId"] %>" />
<updated><%= video["snippet"]["publishedAt"] %></updated>
<author><name><%= @username %></name></author>
<content type="html">
<%= <<-EOF.esc
<iframe width="640" height="360" src="https://www.youtube.com/embed/#{video["id"]["videoId"]}?rel=0" frameborder="0" scrolling="no" allowfullscreen></iframe>
#{
if video["liveStreamingDetails"]
if video["liveStreamingDetails"]["actualStartTime"]
"<p>Live broadcast started at: #{Time.parse(video["liveStreamingDetails"]["actualStartTime"]).readable(@tz)}.</p>"
elsif video["liveStreamingDetails"]["scheduledStartTime"]
"<p>Live broadcast scheduled to start at: #{Time.parse(video["liveStreamingDetails"]["scheduledStartTime"]).readable(@tz)}.</p>"
end
end
}
<p>#{video["snippet"]["description"].gsub("\n","<br>\n")}</p>
EOF
-%>
Expand Down

0 comments on commit 6b9651f

Please sign in to comment.