Skip to content

Commit

Permalink
specs
Browse files Browse the repository at this point in the history
  • Loading branch information
pipistrellka committed Mar 29, 2012
1 parent 63591f4 commit b1fec2c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 45 deletions.
3 changes: 1 addition & 2 deletions lib/link_to_wall.rb
@@ -1,3 +1,2 @@
require File.dirname(__FILE__) + '/link_to_wall/link_parse.rb'

include RMagick
require "RMagick"
64 changes: 21 additions & 43 deletions lib/link_to_wall/link_parse.rb
@@ -1,54 +1,38 @@
# coding: utf-8
module LinkParse
#require "RMagick"
#require 'uri'
#require "timeout"
#require 'mechanize_encoding_hook'

def self.get_html(url, tmp = false)
@tmp = tmp
@agent = Mechanize.new
#@agent.post_connect_hooks << MechanizeEncodingHook.new

uri = URI.parse(url)
@params = {:host => uri.host}
raise StandardError, "Неправильный адрес ссылки" if @params[:host].nil?
#если ссылка на картинку
if url =~ /.*\.(jpe*g)|(gif)/i
if url =~ /.*\.(jpe*g)|(gif)$/i
img = Magick::Image.from_blob(@agent.get_file(url)).first
save_image(url, img)
@params.merge!({:subject => url })
else
@page = @agent.get(url) rescue raise(StandardError, 'Данная страница не отвечает')
get_info()
get_icon() if tmp
#@link.video.sub!(/\/e\//, "/v/") unless @link.video.nil?
domen = @params[:host] =~ /.*\..*\.[\w]*/ ? @params[:host].scan(/.*\.(.*\.[\w]*)/).first.first : @params[:host]
get_icon()
# если нужен предпросмотр берем все картинки
if tmp
@params.merge!({:tmp_pictures => [] })
get_images(/#{domen}.*\.(jpe*g)|(php)|(ashx)|(gif)$/i, 120,1000, true)
get_images(/\.(jpe*g)|(php)|(ashx)$/i, 70,1000, true) unless @params.key?("picture")
# get_images(/.*\.(jpe*g)|(png)|(php)|(ashx)|(gif)/i, 120,1000, true) #if @params[:tmp_pictures].empty?
pid = fork do
#TODO так не работает
#exec("#{Rails.root}/syncs.sh #{Rails.public}/all_image/thumbs")
exec("#{RAILS_ROOT}/syncs.sh #{RAILS_ROOT}/public/all_image/thumbs")
end
Process.detach(pid)
sleep 10
get_images(/\.(jpe*g)|(php)|(ashx)|(gif)$/i, 70,1000, true)
else
domen = @params[:host] =~ /.*\..*\.[\w]*/ ? @params[:host].scan(/.*\.(.*\.[\w]*)/).first.first : @params[:host]
#ищем картинку
return @params if @params.key?("picture")
get_images(/#{domen}.*\.(jpe*g)|(php)|(ashx)$/i, 120,700)
get_images(/#{domen}.*\.(jpe*g)|(php)|(ashx)|(gif)$/i, 120,700) unless @params.key?("picture")
get_images(/\.(jpe*g)|(php)|(ashx)$/i, 120,700) unless @params.key?("picture")
get_images(/\.(jpe*g)|(php)|(ashx)$/i, 30,1000) unless @params.key?("picture")
get_images(/#{domen}.*\.(jpe*g)|(php)|(ashx)|(gif)$/i, 120,700) unless @params.key?(:picture)
get_images(/\.(jpe*g)|(php)|(ashx)$/i, 120,700) unless @params.key?(:picture)
get_images(/\.(jpe*g)|(php)|(ashx)$/i, 30,1000) unless @params.key?(:picture)
end
return @params
end
rescue => err
puts err
puts err.backtrace
raise StandardError, err
end

Expand Down Expand Up @@ -78,20 +62,16 @@ def self.get_info()
mech = Mechanize.new
img = Magick::Image.from_blob(mech.get_file(img_url)).first
# если картинка определена через og:image, то дальще не ищем не ищем
# TODO картинки youtube
save_image(img_url, img) if img
end
@params.merge!({:subject => subject || @page.title, :body => body,:video => video})
rescue => err
puts err
puts err.backtrace
raise StandardError, err
end

def self.get_icon
mech = Mechanize.new
ico = mech.get_file("http://#{@params[:host]}/favicon.ico")
puts "url for ico => #{ico}"
raise StandardError, "havn't icon" if ico == ""
@params.merge!({:icon => "http://#{@params[:host]}/favicon.ico"})
rescue
Expand All @@ -113,16 +93,13 @@ def self.get_icon
# сохраняем картинки удовлетворяющие условию
#*/
def self.get_images(reg, min_size, max_size, tmp=false)
puts reg, "******"
mech = Mechanize.new
urls = @page.images.map do |image|
image.url rescue next
image.url rescue next
end
urls.uniq!
urls.each do |img_url|
# img_url = image.url rescue next
if img_url =~ reg && img_url !~ /(banner)|(user)/
puts "url for image => #{img_url}"
if img_url.to_s =~ reg && img_url !~ /(banner)|(user)/
begin
Timeout.timeout(5) do
@img = Magick::Image.from_blob(mech.get_file(img_url)).first
Expand All @@ -137,27 +114,28 @@ def self.get_images(reg, min_size, max_size, tmp=false)
end
next
end
# img = Magick::Image.from_blob(mech.get_file(img_url)).first rescue next
# проверяем на размер, если меньше 700 не считаем главной картинкой
end
end
end
end



def self.save_image(img_url, img, tmp=false)
# сохраняем название, удаляем лишние хвосты
name = Time.now.to_i.to_s + img_url.split('/').last.scan(/(.*\.[\w]+)[?.]*/).first.first
name = Time.now.to_i.to_s + img_url.to_s.split('/').last.scan(/(.*\.[\w]+)[?.]*/).first.first
thumb = tmp ? img : img.resize_to_fit(90, 90)
Dir.mkdir "public/all_image/thumbs" rescue nil
thumb.write "public/all_image/thumbs/#{name}"
until File.exists?("public/all_image/thumbs/#{name}")
sleep 2
Dir.mkdir "public/images/thumbs" rescue nil
begin
thumb.write "public/images/thumbs/#{name}"
until File.exists?("public/images/thumbs/#{name}")
sleep 2
end
rescue
end
if tmp
@params[:tmp_pictures].push "public/all_image/thumbs/#{name}"
@params[:tmp_pictures].push "public/images/thumbs/#{name}"
else
@params.merge!({:picture => "public/all_image/thumbs/#{name}"})
@params.merge!({:picture => "public/images/thumbs/#{name}"})
end
end

Expand Down
38 changes: 38 additions & 0 deletions spec/link_to_wall/link_spec.rb
@@ -0,0 +1,38 @@
require 'spec_helper'

describe LinkParse do
it "should return valid picture" do
params = LinkParse.get_html("http://www.koolinar.ru/recipe/view/103241")
params[:picture].to_s.should match(/public\/images\/thumbs.+r103241_large\.jpg/i)
end

it "should return valid icon" do
params = LinkParse.get_html("http://www.koolinar.ru/recipe/view/103241")
params[:icon].to_s.should match(/http:\/\/www\.koolinar\.ru\/favicon\.ico/i)
end

it "should return valid params picture only picture" do
params = LinkParse.get_html("http://cs305609.userapi.com/u131914279/-14/y_a4ed77c4.jpg")
params[:picture].to_s.should match(/public\/images\/thumbs.+y_a4ed77c4\.jpg/i)
end

it "should return valid params subject only picture" do
params = LinkParse.get_html("http://cs305609.userapi.com/u131914279/-14/y_a4ed77c4.jpg")
params[:subject].to_s.should match(/http:\/\/cs305609\.userapi\.com\/u131914279\/-14\/y_a4ed77c4\.jpg/i)
end

it "should return valid params video youtube link" do
params = LinkParse.get_html("http://www.youtube.com/watch?v=f09jdMNK3G4&feature=g-all-f&context=G2900e8dFAAAAAAAADAA")
params[:video].to_s.should match(/http:\/\/www\.youtube\.com\/v\/f09jdMNK3G4\?version=3&autohide=1/i)
end

it "should return valid params picture youtube link" do
params = LinkParse.get_html("http://www.youtube.com/watch?v=f09jdMNK3G4&feature=g-all-f&context=G2900e8dFAAAAAAAADAA")
params[:picture].to_s.should match(/public\/images\/thumbs.+hqdefault\.jpg/i)
end

it "should return valid tmp pictures" do
params = LinkParse.get_html("http://www.koolinar.ru/recipe/view/103241", true)
params[:tmp_pictures].size.should equal 23
end
end
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,7 @@
require 'rubygems'
require 'active_support'
require 'link_to_wall'
require 'mechanize'


ENV["RAILS_ENV"] ||= 'test'

0 comments on commit b1fec2c

Please sign in to comment.