-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull.rb
180 lines (137 loc) · 3.87 KB
/
pull.rb
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require 'rubygems'
require 'uri'
require 'mechanize'
require 'nokogiri'
require 'yaml'
require 'ruby-debug'
require_relative 'filter'
module RSS
class Data
def initialize(feed_url)
end
end
class Feed
def initialize(url)
@host = get_host(url)
@feed_id = get_id(url)
@filters = load_filters
@counts = load_count
@url = url
end
def poll
m = Mechanize.new
data = m.get(@url)
data = data.body
result = parse(data)
save_results(result)
# Output the current counts:
File.open(get_counts_path,"w") {|f| f << @counts.to_yaml}
end
private
def save_results(results)
puts results
results_file = File.join(get_project_path, "results.yaml")
begin
old_results = File.open(results_file).read
old_results = YAML.load(old_results)
rescue Errno::ENOENT
puts "New results"
File.open(results_file, "w") {|f| f << results.to_yaml}
return
end
results.keys.each do |filter_name|
if old_results[filter_name].class == Array
old_results[filter_name] += results[filter_name]
else
old_results[filter_name] = results[filter_name]
end
end
File.open(results_file, "w") {|f| f << old_results.to_yaml}
end
def get_project_path
home = %x`echo $HOME`
home.gsub!("\n","")
File.join(home, "rss", @host)
end
def get_counts_path
file_path = get_project_path
File.join(file_path, "#{@feed_id}-counts.yaml")
end
def get_config_path
file_path = get_project_path
File.join(file_path, "#{@feed_id}.yaml")
end
def load_count
begin
config_file = File.open(get_counts_path).read
rescue Errno::ENOENT
puts "Blank count!"
return {}
end
YAML.load(config_file)
end
def get_host(url)
u = URI.parse(url)
u.host
end
def get_id(url)
u = URI.parse(url)
feed_id = u.path
feed_id.gsub!(/^\//,"")
feed_id.gsub!("/","-")
feed_id.gsub!(/\.rss$/, "")
feed_id
end
def load_filters
file_path = get_config_path
puts "File path: #{file_path}"
config_file = File.open(file_path).read
YAML.load(config_file)
end
def parse(data)
doc = Nokogiri::XML(data)
result = {}
items = doc.search("item")
@filters.each do |filter_config|
filter = RSS::Filters::Main.new(filter_config)
puts "="*140
puts "Main filter = #{filter_config[:name]}"
results = []
items.each do |item|
this_result = filter.apply(item)
puts "Got result! Filter => #{filter}, Item: #{item.at('title').inner_text}"
unless this_result.nil?
link = item.at("link").inner_text
# Update count
# -- I'm getting mixed data here, not sure if its going to be useful
# -- but the idea is not to re-search items I've seen before
update_count(link)
results << {:link => link, :data => this_result}
end
end
result[filter_config[:name]] = results
end
result
end
def update_count(link)
# For a given url:
# http://sfbay.craigslist.org/sfc/fuo/2464031888.html
# Once I've seen that listing, I don't need to worry about older ones
key = link.gsub(/\/(\d*)\.html$/, "")
count = $1
puts "Updating count for category: #{key} -> #{count}"
return unless count
if @counts[key].nil?
@counts[key] = count.to_i
else
puts "Increasing? #{(@counts[key] < count.to_i) ? true : false}"
puts "Old max: #{@counts[key]}, New max: #{count}"
@counts[key] = count.to_i
end
end
end
end
if __FILE__ == $PROGRAM_NAME
f = RSS::Feed.new(ARGV[0])
f.poll
end