-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.rb
183 lines (124 loc) · 4.26 KB
/
lint.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
179
180
181
182
183
# encoding: utf-8
require 'pluto'
Pluto.connect! # try connect w/ DATABASE_URL
include Pluto::Models
fetcher = Fetcher::Worker.new
class Report
def initialize
@buf = StringIO.new
@buf.puts "//// REPORT ////////////////////"
@buf.puts ""
end
def puts( msg )
STDOUT.puts msg # echo to stdout while filling up buffer/report
@buf.puts( msg )
end
def string
@buf.string
end
end
o = Report.new
def find_title_tag( html )
match_data = html.match( /<title[^>]*>.*<\/title>/im )
# note: regex match if no match returns match_data == nil
match_data ? match_data[0] : nil
end
def find_feed_link_tags( html )
links = [] # returns array of strings or empty array
html.scan( /<link[^>]+alternate[^>]+>/im ) do |link|
# note: link is "plain" matched string (not regex match data array)
# from ruby string docu:
# - If the pattern contains no groups, each individual result
# consists of the matched string,
# note: different w/ capture groups ()!!
# note: check if type is
# text/xml - possible? allow for now
# application/rss+xml
# application/atom+xml
# application/rdf+xml
#
# skip alternate stylesheet for example, w/ type text/css etc.
## for now just check if type include xml or rss or atom or rdf
if link =~ /type=['"][^'"]+(rdf|rss|atom|xml)/i
links << link.dup
else
puts "!!! skipping candidate link (alternate) |>#{link}<| - type expected w/ rdf|rss|atom|xml"
end
end
links
end
def find_more_feed_link_tags( html )
links = []
html.scan( /<a[^>]+href[^>]+>/im ) do |link|
## check "plain" anchor <a> links
# e.g.
# <a href="/feed.xml">news</a>
# <a href="/atom.xml">news</a>
# <a href="/rss.xml">news</a>
# <a href="/rss2.xml">news</a>
# <a href="/news.rss">news</a>
# <a href="/news.atom">news</a>
# <a href="/blog.atom">news</a>
## for now just check if type include xml or rss or atom or rdf
if link =~ /href=['"][^'"]*(feed\.xml|atom\.xml|rss\.xml|rss2\.xml|news\.rss|news\.atom|blog\.atom)/i
links << link.dup
else
puts "!!! skipping candidate link (anchor) |>#{link}<|"
end
end
links
end
Feed.order(:id).each do |feed|
o.puts "====== #{feed.url} ======"
o.puts ""
res = fetcher.get( feed.url )
if res.code == '200'
html = res.body
## todo: force utf-8 usage
o.puts "title -- >|#{feed.title}|<"
## check for title
title = find_title_tag( html )
if title
o.puts " -- >|#{title}|<"
else
o.puts "!!! title missing"
end
## check for feed links / autodiscovery
# e.g. <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://www.meetup.com/vienna-rb/events/rss/vienna.rb/" />
o.puts "feed -- >|#{feed.feed_url}|<"
links = find_feed_link_tags( html )
links.each_with_index do |link,index|
o.puts " [#{index+1}] >|#{link}|<"
end
### report - no link
# or more than one link
# or link do not match
if links.size == 0
o.puts "!!! link (alternate/auto discovery) missing"
end
if links.size > 1
o.puts "!!! more than one link (alternate/auto discovery) found"
end
## check for non-auto discovery plain links using a heuristic (e.g. atom.xml, etc.)
links = find_more_feed_link_tags( html )
links.each_with_index do |link,index|
o.puts " ++ [#{index+1}] >|#{link}|<"
end
=begin
## check for meta keywords
# e.g. <meta name="keywords" content="Austria,Vienna,Open Source,rubyist,group,club,event,community,meetup" />
md = html.match( /<meta[^>]+name[^>]+keywords[^>]+>/im )
puts "keywords -- >|#{md[0]}|<" if md
## check for meta description
# e.g. <meta name="description" content="User group for Viennese Ruby developers and Rails aficionados.http://vienna-rb.at" />
md = html.match( /<meta[^>]+name[^>]+description[^>]+>/im )
puts "description -- >|#{md[0]}|<" if md
## check for meta author
# e.g <meta name="author" content="Franz Muster" />
=end
else
o.puts "!!! error: fetching site >#{feed.url}< - #{res.code} #{res.message}"
end
o.puts ""
end # each feed
puts o.string