-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-finalpro.py
202 lines (169 loc) · 7.33 KB
/
main-finalpro.py
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
import requests
import csv
import os.path
guildBook = {}
def collectGuildNamesPVP():
# method 1: aggregate through pvp leaderboard
for battleType in [ "2v2", "3v3", "5v5" ]:
r = requests.get("https://us.api.battle.net/wow/leaderboard/"+battleType+"?locale=en_US&apikey=psc5myk6fxjckujqnqz3anfcqsh2s848")
result = r.json()
for row in result["rows"]:
if len(guildBook) >= 500:
break;
lookupPlayer = [ row["name"], row["realmName"] ]
r = requests.get("https://us.api.battle.net/wow/character/"+lookupPlayer[1]+"/"+lookupPlayer[0]+
"?fields=guild&locale=en_US&apikey=psc5myk6fxjckujqnqz3anfcqsh2s848")
result = r.json()
if "guild" in result:
guildBook[ result["guild"]["name"] ] = lookupPlayer[1] #save (guildName, realmName)
#write to csv file
filename = "fp_guildnames_pvp.csv"
with open(filename, "w", newline="", encoding='utf8') as f:
writer = csv.writer(f)
writer.writerow(['guild_name', 'realm_name'])
for key, value in guildBook.items():
writer.writerow( [key, value] )
def scrapePVEGuilds( realmNames ):
for realmName in realmNames:
r = requests.get("https://us.api.battle.net/wow/challenge/"+realmName+"?locale=en_US&apikey=psc5myk6fxjckujqnqz3anfcqsh2s848")
result = r.json()
for row in result["challenge"]:
for group in row["groups"]:
for member in group["members"]:
if len(guildBook) >= 500:
return
if "character" in member and "guild" in member["character"]:
guildBook[ member["character"]["guild"] ] = member["character"]["realm"] #save (guildName, realmName)
def collectGuildNamesPVE():
#1. collect realm names
r = requests.get("https://us.api.battle.net/wow/realm/status?locale=en_US&apikey=psc5myk6fxjckujqnqz3anfcqsh2s848")
result = r.json()
realmNames = []
for row in result["realms"]:
realmNames.append( row["name"] )
#2. search through challenge API
scrapePVEGuilds( realmNames )
#write to csv file
filename = "fp_guildnames_pve.csv"
with open(filename, "w", newline="", encoding='utf8') as f:
writer = csv.writer(f)
writer.writerow(['guild_name', 'realm_name'])
for key, value in guildBook.items():
writer.writerow( [key, value] )
def collectGuildNames():
#collectGuildNamesPVP()
#collectGuildNamesPVE()
guildNamesPVP = {}
guildNamesPVE = {}
filename = "fp_guildnames_pvp.csv"
with open(filename, newline="", encoding='utf8') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
guildNamesPVP[ row[0] ] = row[1]
#if len(guildNamesPVP) >= 2:
# break
filename = "fp_guildnames_pve.csv"
with open(filename, newline="", encoding='utf8') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
guildNamesPVE[ row[0] ] = row[1]
# find common guilds in both PVE and PVP => filter out
keys_a = set(guildNamesPVP.keys())
keys_b = set(guildNamesPVE.keys())
intersection = keys_a & keys_b # '&' operator is used for set intersection
for k in intersection:
guildNamesPVP.pop(k, None)
guildNamesPVE.pop(k, None)
##############################################
getPlayerAchievementsAndGuilds("fp_player_ach_guild_pvp.csv", guildNamesPVP)
getPlayerAchievementsAndGuilds("fp_player_ach_guild_pve.csv", guildNamesPVE)
#getPlayerAcheivementsOfGuilds( "fp_guildnames_ach_pvp.csv", guildNamesPVP )
#getPlayerAcheivementsOfGuilds( "fp_guildnames_ach_pve.csv", guildNamesPVE )
#perhaps I should be filtering out intersections again
def getPlayerAchievementsAndGuilds( filename, guildNamesAndRealms ):
############################################
doneWithGuilds = set()
create = False
if not os.path.isfile(filename):
create = True
else:
# gather already done guilds
with open(filename, newline="", encoding='utf8') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
doneWithGuilds.add( row[2] )
f = open(filename, "a", newline="", encoding='utf8')
writer = csv.writer(f, delimiter=',')
if create is True:
writer.writerow(['player_name', 'realm_name', "guild_name", "achievements"])
f.close()
############################################
# for each guild, get all achievements from all players
i = 0
for guildName, realmName in guildNamesAndRealms.items():
i += 1
if i <= 50:
continue
if guildName in doneWithGuilds:
continue
result = requests.get("https://us.api.battle.net/wow/guild/"+realmName+"/"+guildName+
"?fields=members&locale=en_US&apikey=psc5myk6fxjckujqnqz3anfcqsh2s848").json()
totMembers = 0
achievements = {}
if "members" in result:
totMembers = len( result["members"] )
membersCounted = 0
for member in result["members"]:
charName = member["character"]["name"]
result = requests.get("https://us.api.battle.net/wow/character/"+realmName+"/"+charName+
"?fields=achievements&locale=en_US&apikey=psc5myk6fxjckujqnqz3anfcqsh2s848").json()
if "achievements" in result and "achievementsCompleted" in result["achievements"]:
#append row to csv
f = open(filename, "a", newline="", encoding='utf8')
writer = csv.writer(f, delimiter=',')
writer.writerow( [charName, realmName, guildName, list(result["achievements"]["achievementsCompleted"])] )
f.close()
membersCounted += 1
if membersCounted >= 150: #max member count:150
break
#finished with current guild: add to set
doneWithGuilds.add( guildName )
if i >= 151: #max guild count:50
break
asdf = 0
def getNonFavouarableGuilds():
asdf = 0
def getFavourableGuilds():
# achievements; need to define guilds
# note) player achievements and guild achievements are different
# note) then just use guild members instead?
# note) need to define what is "more favourable" (direction) to who (target)
# ex. need to define who aligns with what vs who clashes with what
asdf = 0
def start():
collectGuildNames()
return
r = requests.get("https://us.api.battle.net/wow/data/character/achievements?locale=en_US&apikey=psc5myk6fxjckujqnqz3anfcqsh2s848")
result = r.json()
#1. Analyse player
lookupPlayer = [ "FellSilent", "Rivendare" ]
r = requests.get("https://us.api.battle.net/wow/character/"+lookupPlayer[1]+"/"+lookupPlayer[0]+
"?fields=guild,achievements,pets,mounts,quests,statistics&locale=en_US&apikey=psc5myk6fxjckujqnqz3anfcqsh2s848")
result = r.json()
asdf = 0
#statistics: pet battles, deaths, quests( total quest complete, daily quests completed )
playerInfo = result
#level
#guild
#class
#faction
#gender
#name
#race
#totalHonorableKills
#achievementPoints
playerGuild = result["guild"]
start()