try a bit harder at finding a submitted story's real title

Ignore the title presented by the user unless we couldn't find
anything, but start out by fetching the URL and trying some <meta>
tags first, then <title>, then use the title the user brought.
This commit is contained in:
joshua stein 2015-02-03 11:20:07 -06:00
parent a16150d8a5
commit 8771afc5f5
2 changed files with 30 additions and 6 deletions

View file

@ -90,7 +90,9 @@ class StoriesController < ApplicationController
end
end
if params[:title].present?
# ignore what the user brought unless we need it as a fallback
@story.title = @story.fetched_title(request.remote_ip)
if !@story.title.present? && params[:title].present?
@story.title = params[:title]
end
end

View file

@ -253,12 +253,34 @@ class Story < ActiveRecord::Base
end
def fetched_title(for_remote_ip = nil)
doc = Nokogiri::HTML(fetched_content(for_remote_ip).to_s)
if doc
return doc.at_css("title").try(:text)
else
return ""
title = ""
if !(doc = Nokogiri::HTML(fetched_content(for_remote_ip).to_s))
return title
end
# try <meta property="og:title"> first, it probably won't have the site
# name
begin
title = doc.at_css("meta[property='og:title']").
attributes["content"].text
rescue
end
# then try <meta name="title">
if title.to_s == ""
begin
title = doc.at_css("meta[name='title']").attributes["content"].text
rescue
end
end
# then try plain old <title>
if title.to_s == ""
title = doc.at_css("title").try(:text).to_s
end
return title
end
def generated_markeddown_description