cinch-bgg 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/cinch-bgg.gemspec +1 -1
- data/lib/cinch/plugins/bgg.rb +98 -18
- metadata +1 -1
data/cinch-bgg.gemspec
CHANGED
data/lib/cinch/plugins/bgg.rb
CHANGED
@@ -18,7 +18,12 @@ module Cinch
|
|
18
18
|
|
19
19
|
match /link_bgg (.+)/i, method: :link_to_bgg
|
20
20
|
|
21
|
-
match /whohas (.+)/i,
|
21
|
+
match /whohas (.+)/i, method: :who_has_game
|
22
|
+
match /whotrading (.+)/i, method: :who_is_trading_game
|
23
|
+
match /whowants (.+)/i, method: :who_wants_game
|
24
|
+
match /whorated (.+)/i, method: :who_rated_game
|
25
|
+
match /whoplayed (.+)/i, method: :who_played_game
|
26
|
+
|
22
27
|
|
23
28
|
def initialize(*args)
|
24
29
|
super
|
@@ -39,8 +44,8 @@ module Cinch
|
|
39
44
|
unless nick.start_with?("-u ")
|
40
45
|
if self.in_community?(nick)
|
41
46
|
name = self.find_bgg_by_irc(nick)
|
42
|
-
user = search_for_user(name)
|
43
|
-
|
47
|
+
user = search_for_user(m, name)
|
48
|
+
self.show_user_details(m, user)
|
44
49
|
else
|
45
50
|
m.reply "There's no \"#{nick}\" in the community. To link yourself and join the community, \"!link_bgg bgg_username\". To search by actual bgg username, \"!bgguser -u bgg_username\".", true
|
46
51
|
end
|
@@ -48,16 +53,21 @@ module Cinch
|
|
48
53
|
end
|
49
54
|
|
50
55
|
def bggself(m)
|
51
|
-
self.bgguser(m.user.nick)
|
56
|
+
self.bgguser(m, m.user.nick)
|
52
57
|
end
|
53
58
|
|
54
59
|
def bggactualuser(m, nick)
|
55
60
|
user = search_for_user(nick)
|
56
|
-
|
61
|
+
self.show_user_details(m, user)
|
62
|
+
end
|
63
|
+
|
64
|
+
def show_user_details(m, user)
|
65
|
+
top5 = (user.top_games.empty? ? "" : "- Top 5: #{user.top_games.first(5).join(", ")} ")
|
66
|
+
m.reply "#{user.name} - Collection: #{user.owned.size} #{top5}- http://boardgamegeek.com/user/#{user.name}", true
|
57
67
|
end
|
58
68
|
|
59
69
|
def link_to_bgg(m, username)
|
60
|
-
@community[m.user.nick] = username
|
70
|
+
@community[m.user.nick.downcase] = username
|
61
71
|
File.open("#{@data_dir}#{USERS_FILE}", 'w') do |file|
|
62
72
|
@community.each do |irc_nick, bgg_name|
|
63
73
|
file.write("#{irc_nick},#{bgg_name}\n")
|
@@ -67,13 +77,38 @@ module Cinch
|
|
67
77
|
end
|
68
78
|
|
69
79
|
def who_has_game(m, title)
|
80
|
+
self.who_what_a_game(m, title, :owned, "Owning")
|
81
|
+
end
|
82
|
+
|
83
|
+
def who_is_trading_game(m, title)
|
84
|
+
self.who_what_a_game(m, title, :trading, "Trading")
|
85
|
+
end
|
86
|
+
|
87
|
+
def who_wants_game(m, title)
|
88
|
+
self.who_what_a_game(m, title, :wanted, "Wants")
|
89
|
+
end
|
90
|
+
|
91
|
+
def who_rated_game(m, title)
|
92
|
+
self.who_what_a_game(m, title, :rated, "Rated", "rating")
|
93
|
+
end
|
94
|
+
|
95
|
+
def who_played_game(m, title)
|
96
|
+
self.who_what_a_game(m, title, :played, "Played", "plays")
|
97
|
+
end
|
98
|
+
|
99
|
+
def who_what_a_game(m, title, action, string, with_number_info = nil)
|
70
100
|
game = search_bgg(m, title)
|
71
101
|
unless game.nil?
|
72
102
|
community = @community.dup
|
73
|
-
community.each{ |irc, bgg| community[irc] = search_for_user(bgg, { :id => game.id, :use_cache => true }) }
|
103
|
+
community.each{ |irc, bgg| community[irc] = search_for_user(m, bgg, { :id => game.id, :use_cache => true }) }
|
74
104
|
|
75
|
-
community.keep_if{ |irc, user| user.
|
76
|
-
|
105
|
+
community.keep_if{ |irc, user| user.send(action).include? game.id.to_s }
|
106
|
+
user_info = []
|
107
|
+
community.each do |irc, user|
|
108
|
+
number_info = with_number_info.nil? ? "" : " (#{user.send(action)[game.id.to_s][with_number_info].to_s})"
|
109
|
+
user_info << "#{self.dehighlight_nick(irc)}#{number_info}"
|
110
|
+
end
|
111
|
+
m.reply "#{string} \"#{game.name}\": #{user_info.join(", ")}", true
|
77
112
|
end
|
78
113
|
end
|
79
114
|
|
@@ -83,9 +118,9 @@ module Cinch
|
|
83
118
|
protected
|
84
119
|
|
85
120
|
def search_bgg(m, search_string)
|
86
|
-
search_results_xml = Nokogiri::XML(open("http://boardgamegeek.com/xmlapi2/search?query=#{search_string.gsub(" ", "%20")}&type=boardgame&exact=1").read)
|
121
|
+
search_results_xml = Nokogiri::XML(self.connect_to_bgg(m){ open("http://boardgamegeek.com/xmlapi2/search?query=#{search_string.gsub(" ", "%20")}&type=boardgame&exact=1") }.read)
|
87
122
|
if search_results_xml.css('items')[0]['total'] == "0"
|
88
|
-
search_results_xml = Nokogiri::XML(open("http://boardgamegeek.com/xmlapi2/search?query=#{search_string.gsub(" ", "%20")}&type=boardgame").read)
|
123
|
+
search_results_xml = Nokogiri::XML(self.connect_to_bgg(m){ open("http://boardgamegeek.com/xmlapi2/search?query=#{search_string.gsub(" ", "%20")}&type=boardgame") }.read)
|
89
124
|
end
|
90
125
|
search_results = search_results_xml.css('item').map { |i| i['id'].to_i }
|
91
126
|
|
@@ -113,12 +148,12 @@ module Cinch
|
|
113
148
|
Nokogiri::XML(File.open("#{@data_dir}#{GAMES_SUBDIR}/#{game_id}.xml"))
|
114
149
|
end
|
115
150
|
|
116
|
-
def search_for_user(name, collection_options = {})
|
151
|
+
def search_for_user(m, name, collection_options = {})
|
117
152
|
use_cache = collection_options[:use_cache] || false
|
118
153
|
game_id = collection_options[:id] || nil
|
119
154
|
|
120
155
|
search_game_id_str = (game_id.nil? ? "" : "&id=#{game_id}")
|
121
|
-
user_xml = Nokogiri::XML(open("http://boardgamegeek.com/xmlapi2/user?name=#{name}&hot=1&top=1#{search_game_id_str}").read)
|
156
|
+
user_xml = Nokogiri::XML(self.connect_to_bgg(m){ open("http://boardgamegeek.com/xmlapi2/user?name=#{name}&hot=1&top=1#{search_game_id_str}")}.read)
|
122
157
|
collection_xml = get_collection_data_for_user(name, use_cache)
|
123
158
|
User.new(name, user_xml, collection_xml)
|
124
159
|
end
|
@@ -127,7 +162,7 @@ module Cinch
|
|
127
162
|
file_url = "#{@data_dir}#{USERS_SUBDIR}/#{name}.xml"
|
128
163
|
unless using_cache
|
129
164
|
open(file_url, "w") do |file|
|
130
|
-
open("http://boardgamegeek.com/xmlapi2/collection?username=#{name}&
|
165
|
+
open("http://boardgamegeek.com/xmlapi2/collection?username=#{name}&stats=1" ) do |uri|
|
131
166
|
file.write(uri.read)
|
132
167
|
end
|
133
168
|
end
|
@@ -146,7 +181,7 @@ module Cinch
|
|
146
181
|
end
|
147
182
|
|
148
183
|
def find_bgg_by_irc(irc_nick)
|
149
|
-
@community[irc_nick]
|
184
|
+
@community[irc_nick.downcase]
|
150
185
|
end
|
151
186
|
|
152
187
|
def in_community?(irc_nick)
|
@@ -157,6 +192,30 @@ module Cinch
|
|
157
192
|
nickname.chars.to_a * 8203.chr('UTF-8')
|
158
193
|
end
|
159
194
|
|
195
|
+
def connect_to_bgg(m)
|
196
|
+
if block_given?
|
197
|
+
begin
|
198
|
+
yield
|
199
|
+
rescue Exception => e
|
200
|
+
case e
|
201
|
+
when Timeout::Error
|
202
|
+
error = 'BGG timeout'
|
203
|
+
when Errno::ECONNREFUSED
|
204
|
+
error = 'BGG connection refused'
|
205
|
+
when Errno::ECONNRESET
|
206
|
+
error = 'BGG connection reset'
|
207
|
+
when Errno::EHOSTUNREACH
|
208
|
+
error = 'BGG host not reachable'
|
209
|
+
else
|
210
|
+
error = "BGG unknown #{e.to_s}"
|
211
|
+
|
212
|
+
end
|
213
|
+
m.reply "#{error}. Please try again.", true
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
|
160
219
|
end
|
161
220
|
|
162
221
|
class Game
|
@@ -200,13 +259,34 @@ module Cinch
|
|
200
259
|
collection_xml.css("items item").each do |g|
|
201
260
|
self.collection[g["objectid"]] = {}
|
202
261
|
self.collection[g["objectid"]]["name"] = g.css("name")[0].content
|
203
|
-
self.collection[g["objectid"]]["
|
204
|
-
self.collection[g["objectid"]]["
|
262
|
+
self.collection[g["objectid"]]["own"] = g.css("status")[0]['own'].to_i
|
263
|
+
self.collection[g["objectid"]]["for_trade"] = g.css("status")[0]['fortrade'].to_i
|
264
|
+
self.collection[g["objectid"]]["want"] = g.css("status")[0]['want'].to_i
|
205
265
|
self.collection[g["objectid"]]["plays"] = g.css("numplays")[0].content.to_i
|
206
|
-
self.collection[g["objectid"]]["
|
266
|
+
self.collection[g["objectid"]]["rating"] = g.css("stats")[0].css("rating")[0]['value']
|
207
267
|
end
|
208
268
|
end
|
209
269
|
|
270
|
+
def owned
|
271
|
+
self.collection.select{ |id, game| game["own"] == 1 }
|
272
|
+
end
|
273
|
+
|
274
|
+
def trading
|
275
|
+
self.collection.select{ |id, game| game["for_trade"] == 1 }
|
276
|
+
end
|
277
|
+
|
278
|
+
def wanted
|
279
|
+
self.collection.select{ |id, game| game["want"] == 1 }
|
280
|
+
end
|
281
|
+
|
282
|
+
def rated
|
283
|
+
self.collection.reject{ |id, game| game["rating"] == "N/A" }
|
284
|
+
end
|
285
|
+
|
286
|
+
def played
|
287
|
+
self.collection.select{ |id, game| game["plays"] > 0 }
|
288
|
+
end
|
289
|
+
|
210
290
|
end
|
211
291
|
end
|
212
292
|
end
|