-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_factory.py
38 lines (32 loc) · 1.22 KB
/
json_factory.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
# TODO: armor is different. this will need to be adjust for armor.
import json
import re
from bs4 import BeautifulSoup
import requests
from urllib3.exceptions import InsecureRequestWarning
from urllib3 import disable_warnings
from fake_useragent import UserAgent
disable_warnings(InsecureRequestWarning)
from SiteMethods import GetItem
def json_factory(item_id, export_json=False, is_name=False):
ua = str(UserAgent().random)
headers = {'User-Agent': ua}
if is_name:
item = GetItem()
id = item.get_id_by_name(item_id)
url = f"https://www.light.gg/db/items/{id}/"
else:
url = f"https://www.light.gg/db/items/{item_id}/"
html = requests.get(url, headers=headers, verify=False)
soup = BeautifulSoup(html.text, 'html.parser')
scripts = soup.find_all('script')
script = scripts[18].string
match = re.search(r'var rollData = (.*?);', script, re.DOTALL)
rollData_string = match.group(1)
presplit = rollData_string.split('ItemDefs: ')[0].split('Raw:')[1]
presplit = presplit.rstrip().rstrip(',')
data = json.loads(presplit)
if export_json:
with open(f'{item_id}_rollData.json', 'w') as f:
json.dump(data, f, indent=4)
return data