Class: PolyrexFeedReader

Inherits:
Object
  • Object
show all
Defined in:
lib/polyrex-feed-reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(polyrex) ⇒ PolyrexFeedReader

Returns a new instance of PolyrexFeedReader.



27
28
29
30
31
# File 'lib/polyrex-feed-reader.rb', line 27

def initialize(polyrex)

  @polyrex = polyrex

end

Instance Method Details

#feed_countObject



33
34
35
# File 'lib/polyrex-feed-reader.rb', line 33

def feed_count()
  @polyrex.xpath 'count(records/column/records/section/records/feed)'
end

#feeds_to_htmlObject



37
38
39
40
41
42
43
44
45
# File 'lib/polyrex-feed-reader.rb', line 37

def feeds_to_html()

  feeds do |feed, filename|

    next if nothing_new? feed
    puts "transforming %s " % filename
    xsltproc 'dynarex-feed.xsl', File.read(filename), filename.sub(/xml$/,'html')
  end
end

#fetch_feedsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/polyrex-feed-reader.rb', line 47

def fetch_feeds()



  feeds do |feed, filename|

    next if nothing_new? feed
    puts "fetching %s " % feed.rss_url.inspect
    
    rtd = RSStoDynarex.new feed.rss_url
    dynarex = rtd.to_dynarex

    dynarex.save(filename) do |xml| 
      a = xml.lines.to_a
      line1 = a.shift
      a.unshift %Q{<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="dynarex-feed.xsl"?>\n}
      a.unshift W3CENTITIES
      a.unshift line1
      a.join
    end    

  end

end

#refreshObject Also known as: update_doc



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
# File 'lib/polyrex-feed-reader.rb', line 72

def refresh

  @datetimestamp = datetimestamp()

  feeds do |feed, filename|

    d = Dynarex.new filename

    feed.last_accessed = @datetimestamp
    feed.last_modified = @datetimestamp if feed.last_modified.empty?
    feed.xhtml = filename
    feed.url = d.summary[:link]

    items = d.to_h[0..2]

    if feed.records.length > 0 and \
                            items.first[:title] == feed.item[0].title then
      feed.recent = recency(feed.last_modified)
      next 
    end

    puts 'adding : ' + filename.inspect

    feed.recent = 'a_hot'
    feed.records.remove_all
    items.each.with_index do |x, i|

      h = {
        title: x[:title],
        link:  x[:link],
        local_link: "%s#%s" % [filename.sub(/xml$/,'html'),i]
      }

      if i == 0 and feed.important != 'n' then

        raw_desc = CGI.unescapeHTML(x[:description]).gsub(/<\/?[^>]*>/, "")
        desc = raw_desc.length > 300 ? raw_desc[0..296] + ' ...' : raw_desc
        h[:description] = desc
      end

      feed.create.item h
    end

    feed.last_modified = @datetimestamp

  end
end

#save_css(filepath = 'feeds.css') ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/polyrex-feed-reader.rb', line 122

def save_css(filepath='feeds.css')

  lib = File.dirname(__FILE__)
  css_buffer = File.read(lib + '/feeds.css')
  #css_buffer = File.read('feeds.css')

  File.write filepath, css_buffer
end

#save_html(filepath = 'feeds.html') ⇒ Object



131
132
133
# File 'lib/polyrex-feed-reader.rb', line 131

def save_html(filepath='feeds.html')
  xsltproc 'feeds.xsl', @polyrex.to_xml, filepath
end

#save_latestnews(filename = 'latest.html') ⇒ Object



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
# File 'lib/polyrex-feed-reader.rb', line 135

def save_latestnews(filename='latest.html')

  last_modified = @polyrex.summary.last_modified
  e = @polyrex.xpath 'records/column/records/section/records/'\
    + 'feed[summary/last_modified="' + last_modified + '"]'

  dynarex = Dynarex.new 'feeds/feed(source, title, link, description)'

  e.each() do |feed|

    summary = feed.element 'records/item/summary'

    record = {
           source: feed.text('summary/title'),
      source_link: feed.text('summary/url'),
            title: summary.text('title'),
             link: summary.text('link'),
      description: summary.text('description')
    }
    dynarex.create record
  end

  filename = 'latest.xml'
#=begin
  dynarex.save(filename) do |xml| 
    a = xml.lines.to_a
    line1 = a.shift
    a.unshift %Q{<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="latest.xsl"?>\n}
    a.unshift W3CENTITIES
    a.unshift line1
    a.join
  end
#=end

  xsltproc 'latest.xsl', dynarex.to_xml, filename 
end

#save_opml(filepath = 'feeds.opml') ⇒ Object



172
173
174
# File 'lib/polyrex-feed-reader.rb', line 172

def save_opml(filepath='feeds.opml')
  xsltproc 'opml-feeds.xsl', @polyrex.to_xml, filepath
end

#save_sectionsObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/polyrex-feed-reader.rb', line 176

def save_sections()

    @polyrex.records.each do |column|

      column.records.each do |section|

        d = Dynarex.new 'section[title]/feed(source, title, link, description)'
        d.summary[:title] = section.title

        section.records.each.with_index do |feed, i|

          next if feed.item.length < 1
          filename = "%s.html#%s" % \
                            [feed.title.to_s.downcase.gsub(/\s/,'').gsub(/\W/,'_'), i]

          h = {source: feed.title, title: feed.item[0].title, link: filename, \
                  description: feed.item[0].description}
          d.create h
          puts h.inspect
        end

        filename = section.title.to_s.downcase.gsub(/\W/,'') + '.xml'
        d.save filename
      end
    end
end

#save_xml(filepath = 'feeds.xml') ⇒ Object



203
204
205
206
207
208
# File 'lib/polyrex-feed-reader.rb', line 203

def save_xml(filepath='feeds.xml')
  @polyrex.summary.last_modified = @datetimestamp
  @polyrex.summary.feed_count = @polyrex.xpath \
              'count(records/column/records/section/records/feed)'
  @polyrex.save filepath, pretty: true
end