-
Notifications
You must be signed in to change notification settings - Fork 0
/
pobutils.py
185 lines (157 loc) · 6.17 KB
/
pobutils.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
import re
import json
import zlib
import base64
import logging
import binascii
import requests
import numpy as np
import pandas as pd
from typing import Optional
from dataclasses import dataclass
import xml.etree.ElementTree as ET
logging.basicConfig(level=logging.INFO)
BUILD_CODE_PATHS = {
'pobb.in': 'https://pobb.in/:id:/raw',
'pastebin.com': 'https://pastebin.com/raw/:id:'
}
DISPLAY_STATS = [
'AverageHit',
'AverageDamage',
'Speed',
'CritChance',
'CritMultiplier',
'CombinedDPS',
'Dex',
'Int',
'Str',
'PowerChargesMax',
'FrenzyChargesMax',
'EnduranceChargesMax',
'TotalEHP',
'Life',
'Armour',
'EnergyShield',
'Evasion',
'FireResist',
'ColdResist',
'LightningResist',
'ChaosResist',
'SpellSuppressionChance'
]
@dataclass
class UniqueItem:
name: str
@dataclass
class ClusterJewel:
size: str
level: int
num_passives: int
small_passives: str
def load_data(file_name: str) -> pd.DataFrame:
try:
df = pd.read_csv(file_name, delimiter=';', parse_dates=['Date'], dtype={'Type': 'category', 'BaseType': 'category', 'Variant': 'category', 'Links': 'category', 'Confidence': 'category'}, usecols=['Date', 'Id', 'Type', 'Name', 'BaseType', 'Variant', 'Links', 'Value', 'Confidence'])
df['Links'] = df['Links'].cat.add_categories('None')
df['Links'].fillna('None', inplace=True)
return df
except FileNotFoundError:
return pd.DataFrame()
def get_pob_code_from_url(url: str) -> Optional[str]:
url_r = re.compile(r'(http(s)?:\/\/)?(www.)?(?P<url_base>\w+\.\w+)\/(?P<paste_id>\w+)')
if not (url and (url_match := url_r.search(url))):
return None
url = BUILD_CODE_PATHS.get(url_match.group('url_base'), '').replace(':id:', url_match.group('paste_id'))
if not url:
return None
resp = requests.get(url)
if resp.status_code != 200:
return None
return resp.text
def read_pob_to_xml(pob_code: str) -> ET.Element:
if not pob_code:
return None
try:
decoded = base64.urlsafe_b64decode(pob_code)
decompressed = zlib.decompress(decoded)
except zlib.error:
logging.error("zlib error converting build code to XML")
return None
except binascii.Error:
logging.error("binascii error converting build code to XML")
return None
else:
return ET.fromstring(decompressed)
def get_uniques_from_xml(root: ET.Element) -> list:
items_xml = root.find('Items')
if not items_xml:
return []
unique = re.compile(r'^Rarity: UNIQUE\n(?P<item_name>[\w \']+)\n')
return [UniqueItem(
name=unique_match.group('item_name')
)
for item in items_xml if item.tag == 'Item' and (unique_match := unique.match(item.text.strip()))]
def get_clusters_from_xml(root: ET.Element) -> list:
items_xml = root.find('Items')
if not items_xml:
return []
cluster = re.compile(r'^Rarity: \w+\s+[\w ]+\s+(?P<size>[\w ]+)\s+(Unique ID: [\w\d]+\s+)?Item Level: (?P<item_level>\d+)\s+LevelReq: \d+\s+Implicits: \d\s+{crafted}Adds (?P<num_passives>\d) Passive Skills\s+{crafted}[\w\d ]+\s+{crafted}(?P<small_passives>(Added Small Passive Skills grant: [\w% \d]+\n)+)')
return [ClusterJewel(
size=cluster_match.group('size'),
num_passives=int(cluster_match.group('num_passives')),
level=int(cluster_match.group('item_level')),
small_passives=cluster_match.group('small_passives').strip().replace('\n', ', ').replace('Added Small Passive Skills grant: ', '')
)
for item in items_xml if item.tag == 'Item' and (cluster_match := cluster.match(item.text.strip()))]
def get_stats_from_xml(root: ET.Element) -> tuple:
stats_root = root.find('Build')
if not stats_root:
return {}, {}
character = {
'level': stats_root.attrib['level'],
'class': stats_root.attrib.get('ascendClassName', stats_root.attrib['className'])
}
display_stats = {}
for stat in stats_root:
if stat.tag != 'PlayerStat':
if stat.tag == 'FullDPSSkill':
if 'FullDPSSkill' not in character:
character['FullDPSSkill'] = []
character['FullDPSSkill'].append((stat.attrib['stat'], float(stat.attrib['value'])))
continue
stat_name = stat.attrib['stat']
stat_value = stat.attrib['value']
if stat_name in DISPLAY_STATS:
display_stats[stat_name] = float(stat_value)
return character, display_stats
def process_cluster_ids(url: str):
# https://poe.ninja/api/data/itemoverview?league=Kalandra&type=ClusterJewel&language=en
# try:
# with open(file_name, 'r', encoding='utf-8') as cluster_info_file:
# cluster_info_dict = json.loads(cluster_info_file.read())
# except IOError as ioe:
# logging.error("Could not read file '%s'", file_name)
# return {}
# except json.JSONDecodeError as jde:
# logging.error("Could not decode JSON at '%s'", file_name)
league_re = re.search("league=(?P<league_name>[A-Za-z]+)", url)
if not league_re:
logging.error("Couldn't parse league name from URL '%s'", url)
return
league = league_re.group('league_name')
resp = requests.get(url)
if resp.status_code != 200:
logging.error("Failed to fetch URL '%s'", url)
return
try:
cluster_info_dict = json.loads(resp.text)
except json.JSONDecodeError as jde:
logging.error("Could not decode JSON from API request")
cluster_id_df = pd.DataFrame([(cluster.get('id'), cluster.get('levelRequired')) for cluster in cluster_info_dict['lines']], columns=['Id', 'ItemLevel'])
cluster_id_df.to_csv(f'data/{league}/{league}.clusterjewels.ids.csv', index=False)
if __name__ == '__main__':
# pob_xml = read_pob_to_xml(get_pob_code_from_url('https://pastebin.com/FEG9g37F'))
# pob_xml = read_pob_to_xml(get_pob_code_from_url('https://pobb.in/BL70qYjBEzI8'))
# print(get_stats_from_xml(pob_xml))
# print(get_clusters_from_xml(pob_xml))
# print(pob_xml)
process_cluster_ids('https://poe.ninja/api/data/itemoverview?league=Ancestor&type=ClusterJewel&language=en')