-
Notifications
You must be signed in to change notification settings - Fork 1
/
wowtool.rb
executable file
·232 lines (205 loc) · 7.1 KB
/
wowtool.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env ruby
# = World of Warcraft Character Info Tool
#
# Grabs data from the WoW Armory.
require 'rexml/document'
require 'net/http'
require 'optparse'
require 'ostruct'
require 'cgi'
require 'yaml'
# Colorizes text if CLICOLOR is defined in the environment.
def colorize(text, color_code)
ENV.include?("CLICOLOR") ? "#{color_code}#{text}\e[0m" : text
end
def white(text); colorize(text, "\e[37m"); end
def red(text); colorize(text, "\e[31m"); end
def green(text); colorize(text, "\e[32m"); end
def yellow(text); colorize(text, "\e[33m"); end
# End color stuff
module WorldOfWarcraft
class Character
attr_accessor :realm, :battle_group, :cclass, :faction, :gender, :guild
def initialize(level, race, name, realm, battle_group, cclass, faction, gender, guild, title = nil)
@realm = realm
@battle_group = battle_group
@cclass = cclass.downcase.to_sym
@faction = faction.downcase.to_sym
@gender = gender.downcase.to_sym
@guild = guild
@title = title
@level = level.to_i
@race = race
@name = name
end
def inspect
"#{@name} %sis a level #{@level} #{@gender.to_s.capitalize} #{@race} #{@faction.to_s.capitalize} #{@cclass.to_s.capitalize} on #{@realm} in battle group #{@battle_group}." %
[@guild != nil ? "<#{@guild}> " : ""]
end
end
class Realm
attr_accessor :name, :up, :language, :type, :queue
@@languages = [:en, :de, :fr, :es]
def initialize(name, status, type, queue, language=:en)
@name = name
if(status == "Up" || status == true || status == "Realm Up") then
@up = true
else
@up = false
end
@language = language.to_sym
@type = type
if(queue == true || queue == "true") then
@queue = true
else
@queue = false
end
end
def inspect
"#{white(@name.ljust(24, "."))}(#{@language}) #{type.ljust(6)} is #{@up ? green("UP") : red("DOWN")} #{@queue ? yellow("Queue") : "NoQueue"}"
end
end
# Returns a REXML::Document for the specified
# character on the specified realm.
def load_character_xml(realm, name)
http = Net::HTTP.new('eu.wowarmory.com', 80)
path = "/character-sheet.xml?r=#{realm}&n=#{name}"
headers = {
'User-agent' => 'Firefox/2.0.0.1' # Have to specify the firefox UA or blizz gives us html.
}
resp = http.get(path, headers)
xmldoc = REXML::Document.new(resp.body)
end
# Grabs the XML for a certain character on a realm, then whacks it into a class.
# Also optionally provides YAML output.
def show_char_info(realm, char, yaml=false)
xmldoc = load_character_xml(realm, char)
xmldoc.each_element("/page/characterInfo/character") do |char|
theChar = Character.new(
char.each_element("@level")[0].to_s,
char.each_element("@race")[0].to_s,
char.each_element("@name")[0].to_s,
char.each_element("@realm")[0].to_s,
char.each_element("@battleGroup")[0].to_s,
char.each_element("@class")[0].to_s,
char.each_element("@faction")[0].to_s,
char.each_element("@gender")[0].to_s,
char.each_element("@guildName")[0].to_s
)
unless yaml
puts theChar.inspect
else
puts theChar.to_yaml
end
end
end
def show_realm_status(realm = nil, yaml=false)
http = Net::HTTP.new('www.wow-europe.com', 80)
path = "/en/serverstatus/index.xml"
headers = {
'User-agent' => 'Firefox/2.0.0.1'
}
resp = http.get(path, headers)
xmldoc = REXML::Document.new(resp.body)
if(realm == nil) then # Show status for all realms
puts "Status of all realms:"
xmldoc.each_element("/rss/channel/item") do |el|
if(el.get_elements("category[@domain='type']").size > 0) then
r = Realm.new(
CGI::unescapeHTML(el.get_elements("title")[0].get_text.to_s),
CGI::unescapeHTML(el.get_elements("category[@domain='status']")[0].get_text.to_s),
CGI::unescapeHTML(el.get_elements("category[@domain='type']")[0].get_text.to_s),
CGI::unescapeHTML(el.get_elements("category[@domain='queue']")[0].get_text.to_s),
CGI::unescapeHTML(el.get_elements("category[@domain='language']")[0].get_text.to_s)
)
unless yaml then
puts "\t#{r.inspect}"
else
puts r.to_yaml
end
else
# Not a realm element
end
end
else
# We know what realm to check
matches = xmldoc.get_elements("/rss/channel/item[title='#{realm.capitalize}']")
if(matches.size > 0) then
matches.each do |r|
rr = Realm.new(
CGI::unescapeHTML(r.get_elements("title")[0].get_text.to_s),
CGI::unescapeHTML(r.get_elements("category[@domain='status']")[0].get_text.to_s),
CGI::unescapeHTML(r.get_elements("category[@domain='type']")[0].get_text.to_s),
CGI::unescapeHTML(r.get_elements("category[@domain='queue']")[0].get_text.to_s),
CGI::unescapeHTML(r.get_elements("category[@domain='language']")[0].get_text.to_s)
)
unless yaml then
puts "\t#{rr.inspect}"
else
puts rr.to_yaml
end
end
else
puts "No matching realms!"
end
end
end
# Begin option-parsing stuff
options = OpenStruct.new
options.realm = nil
options.char = nil
options.yaml_output = false
options.check_realm_status = false
optp = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options]"
opts.separator ""
opts.separator "Filtering Options:"
opts.on("-r", "--realm REALM", "Filter by REALM (req. for character/guild info)") do |rlm|
options.realm = rlm
end
opts.on("-c", "--character NAME", "Grab details for the character NAME") do |char|
options.mode = :char_info
options.char = char
end
opts.on("-s", "--status", "Display the status of a realm.") do |s|
options.check_realm_status = true
end
opts.separator ""
opts.separator "Common Options:"
opts.on_tail("-y", "--yaml", "Provide yaml output") do |y|
options.yaml_output = true
end
opts.on_tail("-h", "--help", "Show this help message.") do |v|
puts opts
options.show_help = true
# exit
end
end
begin
optp.parse!
rescue Exception => e
puts "Error - #{e}!", "", optp
exit
end
if options.show_help then
exit
elsif(options.realm) then
if(options.char) then
show_char_info(options.realm, options.char, options.yaml_output)
elsif options.check_realm_status
puts "Getting realm status for #{options.realm}."
show_realm_status(options.realm, options.yaml_output)
else
puts "I don't know what you want me to find out about #{options.realm.capitalize}!", "", optp
end
elsif(options.check_realm_status) then
show_realm_status(options.realm, options.yaml_output)
else
if options.char then
puts "You need to choose a realm!", "", optp
exit
end
puts "I don't know what you want me to do!", "", optp
end
# End option-parsing stuff
end