-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rb
161 lines (120 loc) · 4.21 KB
/
config.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
# encoding: utf-8
class PlanetConfig
attr_accessor :title
attr_accessor :author
attr_accessor :email
attr_accessor :feeds
def update
###
## try to fetch feed and get link for site/blog
worker = Fetcher::Worker.new
@feeds.each do |feed|
begin
res = worker.get_response( feed.feed )
puts "#{res.code} #{res.message}"
if res.code != '200' # Note: res.code (HTTP status code) is a string
puts "*** sorry; failed to fetch feed"
feed.errors = "failed to fetch feed - HTTP #{res.code} #{res.message}"
next
end
xml = res.body
xml = xml.force_encoding( Encoding::UTF_8 )
synd = FeedUtils::Parser.parse( xml )
puts " format: #{synd.format}"
puts " title: #{synd.title}"
puts " url: #{synd.url}"
feed.link = synd.url # ## use/change to auto_link - why? why not??
feed.format = synd.format
feed.generator = synd.generator # check version and uri if present? add too?
feed.generator << " @version=#{synd.generator_version}" if synd.generator_version
feed.generator << " @uri=#{synd.generator_uri}" if synd.generator_uri
feed.auto_title = synd.title
rescue Exception => e
puts "*** sorry; failed to fetch feed - [#{e.class.name}] #{e.to_s}"
## Note: replace newline in multi-line messages e.g.
##
# This is not well formed XML
# Missing end tag for 'link' (got "head")
# Line: 11
# Position: 522
# Last 80 unconsumed characters:
# becomes
# This is not well formed XML | Missing end tag for 'link' (got "head") | Line: 11 | Position: 522 | Last 80 unconsumed characters:
#
feed.errors = "failed to fetch feed - except [#{e.class.name}] #{e.to_s.gsub("\n",' | ')}"
next
end
end
end # method update
def save_file( fn='planet.ini' )
File.open( fn, 'w') do |f|
f.puts "## Planet Pluto Configuration"
f.puts "## auto-generated on #{Time.now}"
f.puts
f.puts "title = #{title}"
f.puts "author = #{author}"
f.puts "email = #{email}"
f.puts
f.puts
feeds.each do |feed|
f.puts "[#{feed.key}]"
f.puts " title = #{feed.title}"
f.puts " feed = #{feed.feed}"
f.puts " link = #{feed.link}" unless feed.link.blank?
f.puts " ## #{feed.format}; #{feed.generator}" unless feed.format.blank?
f.puts " ## #{feed.auto_title}" unless feed.auto_title.blank?
f.puts " ## #{feed.comments}" unless feed.comments.blank?
f.puts " ## *** error: #{feed.errors}" unless feed.errors.blank?
f.puts
end
end
end # method save_file
end # class PlanetConfig
FeedConfig = Struct.new( :key,
:title,
:feed,
:link,
:format,
:generator,
:auto_title,
:errors,
:comments)
class OpmlBuilder
def self.build( xml )
## pp xml
h = Hash.from_xml(xml)
## pp h
planet = PlanetConfig.new
planet.title = h['opml']['head']['title']
planet.author = h['opml']['head']['ownerName']
planet.email = h['opml']['head']['ownerEmail']
planet.feeds = []
h['opml']['body']['outline'].each do |outline|
feed = FeedConfig.new
feed.title = sanitize_title( outline['text'] ) ###outline['title']
feed.feed = outline['xmlUrl']
feed.key = title_to_key( feed.title )
planet.feeds << feed
end
planet
end
end # class OpmlBuilder
def sanitize_title( title )
###
# todo: unescape (all) html entities in title
# e.g. & to &
title = title.gsub( '&', '&' )
title = title.gsub( /["]/, '') # remove double quotes
title
end
def title_to_key( title )
### fix: use textutils.title_to_key ??
key = title.downcase
key = key.gsub( 'ü', 'ue' )
key = key.gsub( 'é', 'e' )
key = key.gsub( /[^a-z0-9]/, '' ) ## for now remove all chars except a-z and 0-9
if key.blank? ## note: might result in null string (use timestamp)
key = "feed#{Time.now.strftime('%Y%m%d%H%M%S%L')}"
end
key
end