This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
page.rb
143 lines (126 loc) · 4.17 KB
/
page.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
require 'rest_client'
require 'json'
require 'nokogiri'
require './database.rb'
require './pubsubhubbub.rb'
require './rsscloud.rb'
require './pipe.rb'
class Page
attr_accessor :url, :callback
# url: The page, note that this is not the feed
# callback: The URL to ping if there is a new post
def initialize(*args)
self.url = args[0].sub("https", "http") if args[0]
self.callback = args[1] if args[1]
if args.length == 2
! self.isFeed? and Database.new.setFeedURL(self.getFeedURL, url)
end
end
def subscribe
pubsubhub, rssCloud = self.getSubscribeOptions
topic = self.getFeedURL
if pubsubhub
Database.new.toRegister(topic)
success = Hub.new(pubsubhub).subscribe(topic)
return success if success
end
if rssCloud
Database.new.toRegister(topic)
success = RssCloud.new(rssCloud).subscribe(topic)
return success if success
end
return false
end
def getFeedURL
if ! @feedURL
begin
@feedURL = Pipe.new.getFeedURL(self.url)
rescue => error
puts "error getting feedUrl: #{error}"
@feedURL = nil
end
end
return @feedURL
end
def isFeed?
page = RestClient.get url
begin
FeedParser::Parser.parse(page)
return true # if feedparser could parse it it is a valid feed
rescue
end
return false
end
def getSubscribeOptions
feedURL = self.getFeedURL
feed = RestClient.get(feedURL)
rssCloudNode = Nokogiri::XML(feed).xpath('/rss/channel/cloud')
begin
pubSubHubNode = Nokogiri::XML(feed).xpath('/rss/channel/atom:link').map do |link|
if link.attr("rel") == "hub"
link
end
end.compact[0]
rescue Nokogiri::XML::XPath::SyntaxError => error
puts "No PuSH-link found in #{feedURL}: #{error}"
pubSubHubNode = nil
end
rssCloud, pubSubHub = nil
# for now, assume that the method for rss/cloud is http/post like on wordpress.com
begin
rssCloud = rssCloudNode.attr("domain").to_s + ":" + rssCloudNode.attr("port").to_s + rssCloudNode.attr("path").to_s
rescue NoMethodError => error
puts error
end
begin
pubSubHub = pubSubHubNode.attr("href").to_s if pubSubHubNode
rescue NoMethodError => error
puts error
end
return pubSubHub, rssCloud
end
def hasUpdate?
begin
if Time.parse(self.getLastUpdate).to_i > Database.new.getLastUpdate(self.url)
return true
end
rescue => error
puts "error checking for update: #{error}"
end
return false
end
def notifySubscribers
begin
newEntries = Pipe.new.getEntriesAfter(self.getFeedURL, Time.at(Database.new.getLastUpdate(self.url)).rfc2822, "json")
rescue => error
puts "error getting new entries: #{error}"
end
Database.new.setLastUpdate(self.url, self.getLastUpdate)
Database.new.getCallbacks(self.url).each do |callback|
begin
RestClient.post callback, {:url => self.url, :newEntries => newEntries}
rescue => error
puts "error notifying #{callback} of #{url}: #{error}"
end
end
end
def getLastUpdate
begin
return Pipe.new.getLastPageUpdate(self.url)
rescue => error
puts "couldn't get page update: #{error}"
begin
return Pipe.new.getLastFeedUpdate(self.url)
rescue => error2
puts "couldn't get feed update2: #{error2}"
end
end
end
def save(success)
Database.new.savePage(self, success)
Database.new.setLastUpdate(self.url, self.getLastUpdate) # older posts aren't new just because we didnt see them before
end
def delete
Database.new.deletePage(self)
end
end