Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
新規投稿: キーワード検索を実装
Browse files Browse the repository at this point in the history
  • Loading branch information
YOSHIDA Hiroki committed Apr 4, 2014
1 parent 02c3338 commit 6c77c0d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
31 changes: 30 additions & 1 deletion app/models/clip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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']
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/base/_header.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/clips/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion app/workers/image_clipper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit 6c77c0d

Please sign in to comment.