forked from id774/automaticruby
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimage_source.rb
More file actions
71 lines (65 loc) · 1.77 KB
/
image_source.rb
File metadata and controls
71 lines (65 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
# Name:: Automatic::Plugin::Filter::ImageSource
# Author:: 774 <http://id774.net>
# Created:: Feb 28, 2012
# Updated:: Feb 26, 2014
# Copyright:: Copyright (c) 2012-2014 Automatic Ruby Developers.
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
module Automatic::Plugin
class FilterImageSource
require 'net/http'
require 'kconv'
def initialize(config, pipeline=[])
@config = config
@pipeline = pipeline
end
def run
@return_feeds = []
@pipeline.each {|feeds|
new_feeds = Array.new
unless feeds.nil?
feeds.items.each {|feed|
arr = rewrite_link(feed)
if arr.length > 0
arr.each {|link|
Automatic::Log.puts("info", "Extract Image: #{link}")
hashie = Hashie::Mash.new
hashie.title = 'FilterImageSource'
hashie.link = link
new_feeds << hashie
}
end
}
end
@return_feeds << Automatic::FeedMaker.create_pipeline(new_feeds)
}
@return_feeds
end
private
def rewrite_link(feed)
array = Array.new
feed.description.scan(/<img src="(.*?)"/) {|matched|
array = array | matched
}
if array.length === 0 && feed.link != nil
array = imgs(feed.link)
end
array
end
def imgs(link)
images = Array.new
html = open(link).read
unless html.nil?
doc = Nokogiri::HTML(html)
(doc/:img).each {|img|
image = img[:src]
unless /^http/ =~ image
image = link.sub(/\/([^\/]+)$/, image.sub(/^\./,''))
end
images << image
}
end
images
end
end
end