Skip to content

Commit

Permalink
Resolve urls.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Sep 9, 2016
1 parent 88dc000 commit b3d887e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
58 changes: 58 additions & 0 deletions config/initializers/05-string.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require "net/http"
require "uri"

class String
def to_line
self.gsub("\n", " ")
Expand Down Expand Up @@ -32,4 +35,59 @@ def url_ext
uri = URI.parse(self)
File.extname(uri.path)
end

def normalize_url
uri = URI.parse(self)
port = uri.port if (uri.scheme == "http" and uri.port != 80) or (uri.scheme == "https" and uri.port != 443)
URI::HTTP.new(uri.scheme.downcase, uri.userinfo, uri.host.downcase, port, uri.registry, uri.path, uri.opaque, uri.query, uri.fragment).to_s
end

def short_host
uri = URI.parse(self)
if uri.host[0..3] == "www."
uri.host[4..-1]
else
uri.host
end
end

def resolve_url
url = self.normalize_url
dest = $redis.hget("urls", url)
return dest if dest

dest = url
catch :done do
5.times do
uri = URI.parse(dest)
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
response = http.head(uri.request_uri)
case response
when Net::HTTPRedirection then
if %w[
://www.youtube.com/das_captcha
://www.nytimes.com/glogin
://www.facebook.com/unsupportedbrowser
://play.spotify.com/error/browser-not-supported.php
://www.linkedin.com/uas/login
://www.theaustralian.com.au/remote/check_cookie.html
].any? { |s| response["location"].include?(s) }
throw :done
end
if response["location"][0] == "/"
uri = URI.parse(url)
dest = uri.scheme + "://" + uri.host + response["location"]
else
dest = response["location"]
end
else
throw :done
end
end
end
end

$redis.hset("urls", url, dest)
dest
end
end
25 changes: 22 additions & 3 deletions views/twitter_feed.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,35 @@
<link href="<%= request.original_url.esc %>" rel="self" />
<link href="https://twitter.com/<%= @username %>" rel="alternate" />
<updated><%= Time.parse(@data[0]["created_at"]) if @data[0] %></updated>
<%- @data.each do |tweet| -%>
<%-
@data.each do |tweet|
if tweet["retweeted_status"]
text = "RT #{tweet["retweeted_status"]["user"]["screen_name"]}: #{tweet["retweeted_status"]["text"]}"
entities = tweet["retweeted_status"]["entities"]["urls"]
else
text = tweet["text"]
entities = tweet["entities"]["urls"]
end
for entity in entities
text.gsub!(entity["url"], entity["expanded_url"])
end
body = text.dup
title = text.dup
URI.extract(-text, %w[http https]) do |url|
dest = url.resolve_url
title.gsub!(url, "[#{dest.short_host}]")
body.gsub!(url, "<a href='#{dest}' title='#{url}' rel='noreferrer'>#{dest}</a>")
end
-%>

<entry>
<id>twitter:tweet:<%= tweet["id_str"] %><%= ":#{params[:cachebuster]}" if params[:cachebuster] %></id>
<title><%= tweet["text"].to_line.esc %></title>
<title><%= title.to_line.esc %></title>
<link href="https://twitter.com/<%= @username %>/status/<%= tweet["id_str"] %>" />
<updated><%= Time.parse(tweet["created_at"]) %></updated>
<author><name><%= @username %></name></author>
<content type="html">
<%= tweet["text"].esc %>
<%= body.esc %>
</content>
</entry>
<%- end -%>
Expand Down

0 comments on commit b3d887e

Please sign in to comment.