wow-api-characterinfo/toon-info.py

79 lines
2.4 KiB
Python

import http.server
from oauthlib.oauth2 import WebApplicationClient
import webbrowser
import re
import subprocess
import ast
import json
import csv
from datetime import datetime
token_file = open(".oauthtoken", "r")
oauth_token = token_file.readline()
def json_print(dict_in):
print(json.dumps(dict_in, indent=2))
def oauth_api_curl(url):
command = ["curl", "-sH", "Authorization: Bearer {}".format(
oauth_token), url]
result = subprocess.run(command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True)
if result.returncode == 0:
return result.stdout
else:
error = result.stderr
print("Error:")
return error
account_str = oauth_api_curl("https://us.api.blizzard.com/profile/user/wow?namespace=profile-us&locale=en_US")
account_eval = ast.literal_eval(account_str)
if type(account_eval) == dict:
characters = account_eval['wow_accounts'][0]['characters']
char_dicts = []
for char in characters:
char_dict = {
'id': char['id'],
'name': char['name'],
'level': char['level'],
'faction': char['faction']['name'],
'race': char['playable_race']['name'],
'class': char['playable_class']['name'],
'realm': char['realm']['name'],
}
char_dicts.append(char_dict)
char_keys = char_dicts[0].keys()
with open('./output/toons-{}.csv'.format(datetime.now().strftime("%Y%m%d-%H%M%S")), 'w', newline='') as output_file:
dict_writer = csv.DictWriter(output_file, char_keys)
dict_writer.writeheader()
dict_writer.writerows(char_dicts)
prot_char_url = char['protected_character']['href']
prot_char_str = oauth_api_curl(prot_char_url)
prot_char_eval = ast.literal_eval(prot_char_str)
if type(prot_char_eval) == dict:
print(prot_char_eval['protected_stats'].keys())
else:
print('eval\'d type:', type(prot_char_eval))
profs_url = "https://us.api.blizzard.com/profile/wow/character/{}/{}/professions?namespace=profile-us&locale=en_US".format(
char['realm']['slug'], char['name'].lower())
profs_str = oauth_api_curl(profs_url)
profs_eval = ast.literal_eval(profs_str)
if type(profs_eval) == dict:
json_print(profs_eval)
else:
print('eval\'d type:', type(profs_eval))
else:
print('eval\'d type:', type(account_eval))