-
Notifications
You must be signed in to change notification settings - Fork 4
/
topics.rb
121 lines (100 loc) · 2.96 KB
/
topics.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
begin
require 'system_timer'
MyTimer = SystemTimer
rescue
require 'timeout'
MyTimer = Timeout
end
# Small fix for feeds with 'xhtml' type content
module Atom
class Content
class Xhtml < Base
def to_xml(nodeonly = true, name = 'content', namespace = nil, namespace_map = Atom::Xml::NamespaceMap.new)
node = XML::Node.new("#{namespace_map.prefix(Atom::NAMESPACE, name)}")
node['type'] = 'xhtml'
# fixed line - FriendFeed send 'xhtml' type content WITHOUT xml_lang :(
node['xml:lang'] = self.xml_lang ? self.xml_lang : "en"
div = XML::Node.new('div')
div['xmlns'] = XHTML
p = XML::Parser.string(to_s)
content = p.parse.root.copy(true)
div << content
node << div
node
end
end
end
end
module WebGlue
class Config
DEBUG = true
FEEDS_DIR = (File.join(File.dirname(__FILE__), 'feeds')).freeze
GIVEUP = 10
CHECK = 300 # check every 5 min
end
class InvalidTopicException < Exception; end
class Topic
attr_reader :entries
def Topic.to_hash(url)
[url].pack("m*").strip!
end
def Topic.to_url(hash)
hash.unpack("m")[0]
end
def Topic.sync(url)
raise InvalidTopicException unless url
feed = nil
begin
MyTimer.timeout(Config::GIVEUP) do
feed = Atom::Feed.load_feed(URI.parse(url))
end
rescue
raise InvalidTopicException
end
raise InvalidTopicException unless feed
return feed
end
def Topic.load_file(hash)
path = File.join(Config::FEEDS_DIR,"#{hash}.yml")
raise InvalidTopicException unless File.exists?(path)
return YAML::load_file(path)
end
def Topic.load_url(url)
raise InvalidTopicException unless url
h = Topic.to_hash(url)
return Topic.load_file(h)
end
def Topic.save!(url, feed)
raise InvalidTopicException unless (url and feed)
h = Topic.to_hash(url)
File.open(File.join(Config::FEEDS_DIR,"#{h}.yml"), "w") do |out|
YAML::dump(feed, out)
end
end
def Topic.diff(url, to_atom = false)
raise InvalidTopicException unless url
begin
old_feed = Topic.load_url(url)
urls = old_feed.entries.collect {|e| e.links.first.href }
rescue InvalidTopicException
urls = []
end
new_feed = nil
begin
MyTimer.timeout(Config::GIVEUP) do
new_feed = Atom::Feed.load_feed(URI.parse(url))
end
rescue Exception => e
raise e.to_s
end
raise InvalidTopicException unless new_feed
Topic.save!(url, new_feed)
new_feed.entries.delete_if {|e| urls.include?(e.links.first.href) }
return nil unless urls.length > 0 # do not send everything first time
return nil unless new_feed.entries.length > 0
return to_atom ? new_feed.to_xml : new_feed.entries
end
def Topic.atom_diff(url)
end
end
end