Skip to content

Commit

Permalink
Add filters to Imgur (animated, nsfw, is_album, min_score). Embed gif…
Browse files Browse the repository at this point in the history
…v with iframe.
  • Loading branch information
stefansundin committed May 27, 2017
1 parent ddefd77 commit 3e76ccf
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 26 deletions.
45 changes: 28 additions & 17 deletions app.rb
Expand Up @@ -1045,7 +1045,7 @@
elsif /(?:(?:imgur|reddit)\.com)?\/?r\/(?<subreddit>[a-zA-Z0-9_]+)/ =~ params[:q]
# https://imgur.com/r/aww
# https://www.reddit.com/r/aww
redirect "/imgur/r/#{subreddit}"
redirect "/imgur/r/#{subreddit}#{"?#{params[:type]}" if !params[:type].empty?}"
return
elsif /(?<username>[a-zA-Z0-9]+)\.imgur\.com/ =~ params[:q] and username != "i"
# https://thebookofgray.imgur.com/
Expand Down Expand Up @@ -1079,28 +1079,39 @@
if user_id.nil?
"This image was probably uploaded anonymously. Sorry."
else
redirect "/imgur/#{user_id}/#{username}"
redirect "/imgur/#{user_id}/#{username}#{"?#{params[:type]}" if !params[:type].empty?}"
end
end

get "/imgur/r/:subreddit" do
@subreddit = params[:subreddit]

response = ImgurParty.get("/gallery/r/#{@subreddit}")
get "/imgur/:user_id/:username" do
if params[:user_id] == "r"
@subreddit = params[:username]
response = ImgurParty.get("/gallery/r/#{@subreddit}")
else
@user_id = params[:user_id]
@username = params[:username]
# can't use user_id in this request unfortunately
response = ImgurParty.get("/account/#{@username}/submissions")
end
raise ImgurError.new(response) if !response.success? or response.body.empty?
@data = response.parsed_response["data"]

erb :imgur_feed
end

get "/imgur/:user_id/:username" do
@user_id = params[:user_id]
@username = params[:username]

# can't use user_id in this request unfortunately
response = ImgurParty.get("/account/#{@username}/submissions")
raise ImgurError.new(response) if !response.success?
@data = response.parsed_response["data"]
if params[:animated]
value = params[:animated] == "true"
@data.select! { |image| image["animated"] == value }
end
if params[:nsfw]
value = params[:nsfw] == "true"
@data.select! { |image| image["nsfw"] == value }
end
if params[:is_album]
value = params[:is_album] == "true"
@data.select! { |image| image["is_album"] == value }
end
if params[:min_score]
value = params[:min_score].to_i
@data.select! { |image| image["score"] >= value }
end

erb :imgur_feed
end
Expand Down
32 changes: 23 additions & 9 deletions views/imgur_feed.erb
Expand Up @@ -17,22 +17,36 @@
body = <<-EOF.undent.esc
<a href="#{link}"><img src="#{img_src}"></a>
#{image["description"].to_paragraphs}
#{image["description"].linkify.to_paragraphs}
<p><a href="http://s.imgur.com/a/#{image["id"]}/zip">Download album</a></p>
EOF
else
id = "imgur:image:#{image["id"]}"
title = "#{image["title"]} [#{image["width"]}x#{image["height"]}]"
link = "https://imgur.com/#{image["id"]}"
ext = image["type"].split("/")[1]
img_src = "https://i.imgur.com/#{image["id"]}h.#{ext}"
body = <<-EOF.undent.esc
<a href="#{link}.#{ext}"><img src="#{img_src}"></a>
if image["gifv"]
title = "Gif: #{title}"
img_src = "https://i.imgur.com/#{image["id"]}.gif"
gifv_url = "#{img_src}v"
body = <<-EOF.undent.esc
<iframe width="#{image["width"]}" height="#{image["height"]}" src="#{gifv_url}" frameborder="0" scrolling="no" allowfullscreen></iframe>
<p><a href="#{gifv_url}">Open embed</a></p>
#{image["description"].to_paragraphs}
<p><a href="https://imgur.com/download/#{image["id"]}/#{CGI.escape(image["title"])}">Download</a></p>
<p>Size: #{image["size"].to_filesize} (#{image["width"]}x#{image["height"]})</p>
EOF
#{image["description"].linkify.to_paragraphs}
<p><a href="https://imgur.com/download/#{image["id"]}/#{CGI.escape(image["title"])}">Download</a></p>
<p>Size: #{image["size"].to_filesize} (#{image["width"]}x#{image["height"]})</p>
EOF
else
ext = image["type"].split("/")[1]
img_src = "https://i.imgur.com/#{image["id"]}h.#{ext}"
body = <<-EOF.undent.esc
<a href="#{link}.#{ext}"><img src="#{img_src}"></a>
#{image["description"].linkify.to_paragraphs}
<p><a href="https://imgur.com/download/#{image["id"]}/#{CGI.escape(image["title"])}">Download</a></p>
<p>Size: #{image["size"].to_filesize} (#{image["width"]}x#{image["height"]})</p>
EOF
end
end
-%>

Expand Down
6 changes: 6 additions & 0 deletions views/index.erb
Expand Up @@ -215,6 +215,12 @@
<div class="input-group-addon"><label for="imgur_q">Imgur</label></div>
<input class="form-control" type="search" name="q" id="imgur_q" placeholder="Enter a Imgur username or a url. Or subreddit url." required>
<span class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
<ul class="dropdown-menu dropdown-menu-right">
<li><a data-submit-type="animated=true">Animated only</a></li>
<li><a data-submit-type="animated=false">Pictures only</a></li>
<li><a data-submit-type="is_album=true">Albums only</a></li>
</ul>
<input class="btn btn-primary" type="submit" value="Get RSS Feed">
</span>
</form>
Expand Down

0 comments on commit 3e76ccf

Please sign in to comment.