/
twitter2campfire.rb
90 lines (75 loc) · 1.92 KB
/
twitter2campfire.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
require 'rubygems'
require 'tinder'
require 'rio'
require 'hpricot'
require 'ostruct'
require 'time'
require 'htmlentities'
require 'digest/sha1'
class Twitter2Campfire
attr_accessor :feed, :campfire, :room, :cachefile, :options
def initialize(feed,campfire,room, cachefile = 'archived_latest.txt', options = {})
self.feed = feed
self.campfire = campfire
self.room = campfire.find_room_by_name room
self.cachefile = cachefile
self.options = options
end
def raw_feed
@doc ||= Hpricot(rio(feed) > (string ||= ""))
end
def entries
(raw_feed/'entry').map do |e|
OpenStruct.new(
:from => (e/'name').inner_html,
:text => (e/'title').inner_html,
:link => (e/'link[@rel=alternate]').first['href'],
:checksum => Digest::SHA1.hexdigest((e/'title').inner_html),
:date => Time.parse((e/'updated').inner_html),
:twicture => "http://twictur.es/i/#{(e/'id').inner_html.split(':').last}.gif"
)
end
end
def latest_tweet
entries.first
end
def save_latest
f = File.exist?(cachefile)? File.open(cachefile, 'a') : File.new(cachefile, 'w')
f.write("\n#{new_archive_contents}")
end
def checksums
entries.map { |e| e.checksum }.to_a
end
def archived_checksums
archive_file.split("\n")
end
def new_checksums
checksums.flatten.uniq[0,1000]
end
def archive_file
begin
return File.read(cachefile)
#rescue
# ''
end
end
def new_archive_contents
"#{new_checksums.join("\n")}"
end
def posts
entries.reject { |e| archived_checksums.include?(e.checksum) }
end
def coder
HTMLEntities.new
end
def publish_entries
posts.reverse.each do |post|
if options[:twicture]
room.speak post.twicture
else
room.speak "#{coder.decode(post.from)}: #{coder.decode(post.text)} #{post.link}"
end
end
save_latest
end
end