Skip to content

Commit

Permalink
Add iCalendar endpoint for Twitch.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Apr 26, 2020
1 parent 3ec7f55 commit 51de061
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,20 @@
erb :"twitch.atom"
end

get %r{/twitch/(?<id>\d+)/(?<user>.+)\.ics} do |id, user|
@title = "#{user} on Twitch"

type = %w[all upload archive highlight].pick(params[:type]) || "all"
response = Twitch.get("/videos", query: { user_id: id, type: type })
raise(TwitchError, response) if !response.success?

@data = response.json["data"]
user = @data[0]["user_name"] || CGI.unescape(user)
@alternate_url = Addressable::URI.parse("https://www.twitch.tv/#{user.downcase}").normalize.to_s

erb :"twitch.ics"
end

get %r{/twitch/(?<id>\d+)/(?<user>.+)} do |id, user|
@id = id
@type = "user"
Expand Down
38 changes: 38 additions & 0 deletions views/twitch.ics.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<%- content_type :ics -%>
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//RSSBox//RSSBox <%= ENV["APP_VERSION"] %>//EN
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:<%= @title %>
X-WR-CALDESC:<%= @alternate_url %>
<%-
@data.each do |video|
duration = video["duration"].parse_duration
if video["thumbnail_url"] == ""
# video is currently live
duration += 7200
end
dtstart = Time.parse(video["created_at"])
dtend = dtstart + duration
published_at = Time.parse(video["published_at"])

dtstart = dtstart.strftime("%Y%m%dT%H%M%SZ")
dtend = dtend.strftime("%Y%m%dT%H%M%SZ")
published_at = published_at.strftime("%Y%m%dT%H%M%SZ")
-%>
BEGIN:VEVENT
UID:video-<%= video["id"] %>@twitch.com
DTSTART:<%= dtstart %>
DTEND:<%= dtend %>
DTSTAMP:<%= published_at %>
CREATED:<%= published_at %>
LAST-MODIFIED:<%= published_at %>
SUMMARY:<%= video["title"].to_line %>
LOCATION:https://www.twitch.tv/videos/<%= video["id"] %>
DESCRIPTION:<%= video["description"].gsub("\n", "\\n") %>
END:VEVENT

<%- end -%>
END:VCALENDAR

0 comments on commit 51de061

Please sign in to comment.