From 6c77c0d2d5994b3af3b53381b38f4624b2bc76e3 Mon Sep 17 00:00:00 2001 From: YOSHIDA Hiroki Date: Sat, 5 Apr 2014 01:20:34 +0900 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E8=A6=8F=E6=8A=95=E7=A8=BF:=20?= =?UTF-8?q?=E3=82=AD=E3=83=BC=E3=83=AF=E3=83=BC=E3=83=89=E6=A4=9C=E7=B4=A2?= =?UTF-8?q?=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/clip.rb | 31 ++++++++++++++++++++++++++++++- app/views/base/_header.html.slim | 2 +- app/views/clips/_form.html.slim | 2 +- app/workers/image_clipper.rb | 10 +++++++++- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/app/models/clip.rb b/app/models/clip.rb index f221643..03e95ee 100644 --- a/app/models/clip.rb +++ b/app/models/clip.rb @@ -116,9 +116,10 @@ def listup_image_urls_from_html return [] if self.origin_html.blank? return [ self.origin_url ] if self.origin_url =~ support_image_types_regexp doc = Hpricot(self.origin_html) - (image_urls_from_image_tags(doc/:img) + image_urls_from_anthor_tags(doc/:a)).uniq + image_urls_from(doc) end + class BetterImageGetter def initialize(url) @uri = URI.parse(url) @@ -160,6 +161,16 @@ def fill_origin_entry private + def image_urls_from(doc) + ( + image_urls_from_image_tags(doc/:img) + + image_urls_from_anthor_tags(doc/:a) + + # Hpricotで、目的のエレメントがテキストとして認識されてしまうので暫定処理 + # 本来あるべきセレクタ: doc/'[@style^="background"]' + image_urls_from_css((doc/'.photo_stage').map(&:children)) + ).uniq + end + def image_urls_from_image_tags(image_tags) image_tags.map do |image_tag| image_url = image_tag.attributes['src'] @@ -176,6 +187,24 @@ def image_urls_from_anthor_tags(anchor_tags) end.compact.uniq end + # for tumblr + def image_urls_from_css(tags) + tags.map do |tag| + # 暫定処置 + # 本来あるべき処理: attribute = tag.attributes['style'] + attribute = tag.join + next if attribute.blank? + + match = attribute.match(/url\(['"]?(.+)['"]?\)/) + next if match.nil? + + image_url = match[1] + next unless image_url =~ support_image_types_regexp + + BetterImageGetter.new(uri_absolute_path(image_url)).exec + end.compact.uniq + end + def support_image_types_regexp /\.(#{Settings.support_image_types.join('|')})$/ end diff --git a/app/views/base/_header.html.slim b/app/views/base/_header.html.slim index 7d7eeca..a99c501 100644 --- a/app/views/base/_header.html.slim +++ b/app/views/base/_header.html.slim @@ -9,7 +9,7 @@ li = form_tag new_clip_path, method: :get do = label_tag :url, 'イラストを投稿:' - = text_field_tag :url, nil, placeholder: 'http://画像URL、ページURL' + = text_field_tag :url, nil, placeholder: 'キーワード or http://ページURL' li = link_to 'ブックマークレットから投稿', bookmarklet_path li.divider li = link_to 'まとめを作成', new_matome_path diff --git a/app/views/clips/_form.html.slim b/app/views/clips/_form.html.slim index 8f038d7..ea80849 100644 --- a/app/views/clips/_form.html.slim +++ b/app/views/clips/_form.html.slim @@ -10,7 +10,7 @@ = form_for @clip, url: { action: :clipping }, remote: true, html: { id: 'load_form' } do |f| h2 あたらしくクリップ h3 1. 画像をセレクト - = f.text_field :origin_url, id: 'loading_url', value: @url, placeholder: 'http://画像URL、ページURL' + = f.text_field :origin_url, id: 'loading_url', value: @url, placeholder: 'キーワード or http://ページURL' = f.submit 'ロード', id: 'load' #container diff --git a/app/workers/image_clipper.rb b/app/workers/image_clipper.rb index dccae4b..63d105b 100644 --- a/app/workers/image_clipper.rb +++ b/app/workers/image_clipper.rb @@ -7,7 +7,10 @@ class << self def perform(user_id, target_url) @user_id = user_id @target_url = target_url - download_html + if @target_url.present? + @target_url = url_for_keyword(@target_url) unless @target_url.match(/^http/) + download_html + end publish end @@ -49,5 +52,10 @@ def create_html_cahce_file(html) def html_cache_file_path File.join(Settings.html_cache_dir, "#{@user_id}.html") end + + def url_for_keyword(keyword) + require 'cgi' + "http://www.tumblr.com/tagged/#{CGI.escape(keyword)}" + end end end