Class: Vnews::Feed

Inherits:
Object
  • Object
show all
Defined in:
lib/vnews/feed.rb

Constant Summary collapse

USER_AGENT =

include Autodiscoverer

"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/534.16+ (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4"

Class Method Summary collapse

Class Method Details

.fetch_feed(xml_url, folder = nil) ⇒ Object



32
33
34
35
# File 'lib/vnews/feed.rb', line 32

def self.fetch_feed(xml_url, folder=nil)
  f = self.get_feed(xml_url)
  [xml_url, f, folder]
end

.get_feed(feed_url) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vnews/feed.rb', line 13

def self.get_feed(feed_url)
  response = open(feed_url, "User-Agent" => USER_AGENT)

  xml = response.read
  # puts response.last_modified
  $stderr.print "#{feed_url} -> Found #{response.content_type} #{response.charset}\n"

  charset = response.charset || "UTF-8"

  not_xml = response.content_type !~ /xml/ && xml[0,900] !~ /<?xml|<rss/
  if not_xml
    return nil
  end
  feed_yaml = FeedYamlizer.run(xml, charset)
  feed_yaml
rescue OpenURI::HTTPError, REXML::ParseException, NoMethodError, Timeout::Error
  $stderr.puts "ERROR (#{feed_url}): #{$!} => #{$!.message}"
end

.log(text) ⇒ Object



70
71
72
# File 'lib/vnews/feed.rb', line 70

def self.log(text)
  $stderr.puts text
end

.save_feed(feed_url, f, folder = nil) ⇒ Object

f is the feed hash



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vnews/feed.rb', line 38

def self.save_feed(feed_url, f, folder=nil)
  # if no folder, we're just updating a feed
  if folder
    Vnews.sql_client.insert_feed(f[:meta][:title], feed_url, f[:meta][:link], folder)
  end
  f[:items].each do |item|
    item[:pub_date] ||= Time.now
    if item[:guid].nil? || item[:guid].strip == ''
      item[:guid] = [ f[:meta][:link], item[:link] ].join("+")
    end
    Vnews.sql_client.insert_item item.merge(:feed => feed_url, :feed_title => f[:meta][:title])
    $stderr.print "."
  end
  print "\n"
rescue
  puts "ERROR: dump: #{feed_url.inspect}, #{folder.inspect}: #{f.inspect} "
  raise
end

.update_feed(feed_title) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vnews/feed.rb', line 57

def self.update_feed(feed_title)
  require 'vnews/display'
  feed_title = Vnews::Display.strip_item_count(feed_title)
  puts "Updating feed: #{feed_title}"
  feed_url = Vnews.sql_client.feed_by_title feed_title
  puts "Fetching data from feed url: #{feed_url}"
  # puts "Deleting feed items for #{feed_url}"
  # Vnews.sql_client.delete_feed_items feed_url
  f = Vnews::Feed.get_feed feed_url
  save_feed feed_url, f, nil
  puts "\nFeed updated"
end