-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbArmoryRepStalker.rb
147 lines (121 loc) · 3.92 KB
/
sbArmoryRepStalker.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
#!/bin/env ruby
# encoding: utf-8
require 'rubygems'
require 'json'
require 'net/http'
require 'net/https'
require 'uri'
require 'sqlite3'
def insertFaction(id, faction)
query = "select count(*) from faction where id = ?"
retVal = $db.execute(query, id)
if retVal[0][0] == "0"
query = "insert into faction (id, name) values (?, ?);"
$db.execute(query, id, faction)
end
end
def insertCharacter(region, realm, name, guild, comment, url)
p "new character: #{region}/#{realm}/#{name}"
query = "insert into character (id, region, realm, name, guild, comment, url, autoscan) values ((select max(id)+1 from character), ?, ?, ?, ?, ?, ?, 1);"
$db.execute(query, region, realm, name, guild, comment, url)
end
def getCharacter(region, realm, name)
query = "select id,region,realm,name,guild,comment,url from character where region=? and realm=? and name=?;"
result = $db.execute(query, region, realm, name)
if result.nil?
return nil
else
return result[0]
end
end
def getAutoscanCharacters()
query = "select id,region,realm,name,guild,comment,url from character where autoscan=1;"
result = $db.execute(query)
return result
end
def getRepValueToday(characterid, factionid)
query = "select value from reputation where characterid=? and factionid=? and date(date)=date('now')"
result = $db.execute(query, characterid, factionid)
if result.nil?
return nil
else
return result[0]
end
end
def setRepValue(characterid, factionid, value, standing, max)
if getRepValueToday(characterid, factionid).nil?
query = "insert into reputation (characterid, factionid, value, standing, max, date) values (?, ?, ?, ?, ?, datetime('now'));"
$db.execute(query, characterid, factionid, value, standing, max)
print "+"
else
if $upd == 1
query = "update reputation set value=?, standing=?, max=?,date=datetime('now') where characterid=? and factionid=? and date(date)=date('now');"
$db.execute(query, value, standing, max, characterid, factionid)
print "*"
else
print "-"
end
end
end
def getArmoryData(region, realm, name)
base_url = "http://#{region}.battle.net/api/wow/character/#{realm}/#{name}"
useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.13 (KHTML, like Gecko) Chrome/24.0.1290.1 Safari/537.13"
num = 0
while num < 10
url = URI.escape("#{base_url}?fields=reputation,guild")
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.initialize_http_header({"User-Agent" => useragent})
resp = http.request(request)
data = resp.body
result = JSON.parse(data)
if result.has_key? 'Error'
raise "web service error"
end
if result['status'] == "nok"
print "Error: #{result['reason']} for: #{region}/#{realm}/#{name}\n"
else
break
end
print "s"
sleep (10)
num = num + 1
end
return result
end
def processData(data, region)
character = getCharacter(region, data['realm'], data['name'])
if character.nil?
guild = ''
if !data['guild'].nil?
guild = data['guild']['name']
end
insertCharacter(region, data['realm'], data['name'], guild, '', '')
character = getCharacter(region, data['realm'], data['name'])
end
data['reputation'].each { |d|
insertFaction(d['id'], d['name'])
setRepValue(character[0], d['id'], d['value'], d['standing'], d['max'])
#print "#{d['name']} => #{d['standing']} / #{d['value']}\n"
}
end
$db = SQLite3::Database.new( 'wowRep.db' )
data = nil
if ARGV[0] == "allupd"
$upd = 1
end
if ARGV[0] and ARGV[1] and ARGV[2]
data = getArmoryData(ARGV[0], ARGV[1], ARGV[2])
processData(data, ARGV[0])
print " - done\n"
else
if ARGV[0] == "all" or ARGV[0] == "allupd"
getAutoscanCharacters().each { |c|
print "update: #{c[1]}/#{c[2]}/#{c[3]} "
data = getArmoryData(c[1], c[2], c[3])
processData(data, c[1])
print " - done\n"
}
end
end