Skip to content

Commit

Permalink
Implement caching for Mixcloud.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Jan 15, 2021
1 parent c81905e commit a34f1cb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
38 changes: 29 additions & 9 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -836,20 +836,40 @@
username = params[:q]
end

response = App::Mixcloud.get("/#{username}/")
return [response.code, "Can't find a user with that name."] if response.code == 404
raise(App::MixcloudError, response) if !response.success?
data = response.json
path, _ = App::Cache.cache("mixcloud.user.#{username.downcase}", 24*60*60, 60) do
response = App::Mixcloud.get("/#{username}/")
next "Error: Can't find a user with that name." if response.code == 404
raise(App::MixcloudError, response) if !response.success?
data = response.json
"#{data["username"]}/#{data["name"]}"
end
return [422, "Something went wrong. Try again later."] if path.nil?
return [422, path] if path.start_with?("Error:")

redirect Addressable::URI.new(path: "/mixcloud/#{data["username"]}/#{data["name"]}").normalize.to_s
redirect Addressable::URI.new(path: "/mixcloud/#{path}").normalize.to_s
end

get %r{/mixcloud/(?<username>[^/]+)/(?<user>.+)} do |username, user|
response = App::Mixcloud.get("/#{username}/cloudcasts/")
return [response.code, "That username no longer exist."] if response.code == 404
raise(App::MixcloudError, response) if !response.success?
data, @updated_at = App::Cache.cache("mixcloud.tracks.#{username.downcase}", 4*60*60, 60) do
response = App::Mixcloud.get("/#{username}/cloudcasts/")
next "Error: That username no longer exist." if response.code == 404
raise(App::MixcloudError, response) if !response.success?
response.json["data"].map do |track|
{
"audio_length" => track["audio_length"],
"created_time" => track["created_time"],
"name" => track["name"],
"pictures" => track["pictures"].slice("extra_large", "medium"),
"slug" => track["slug"],
"url" => track["url"],
"user" => track["user"].slice("username", "name"),
}
end.to_json
end
return [422, "Something went wrong. Try again later."] if data.nil?
return [422, data] if data.start_with?("Error:")

@data = response.json["data"]
@data = JSON.parse(data)
@username = @data[0]["user"]["username"] rescue CGI.unescape(username)
@user = @data[0]["user"]["name"] rescue CGI.unescape(user)

Expand Down
2 changes: 1 addition & 1 deletion views/mixcloud.atom.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<icon>https://www.mixcloud.com/favicon.ico</icon>
<link href="<%= request.original_url.esc %>" rel="self" />
<link href="https://www.mixcloud.com/<%= @username %>/" rel="alternate" />
<updated><%= Time.parse(@data[0]["created_time"]) if @data[0] %></updated>
<updated><%= @updated_at %></updated>
<%- @data.each do |track| -%>

<entry>
Expand Down

0 comments on commit a34f1cb

Please sign in to comment.