#!/usr/bin/python3 import base64 import chevron import configparser import hashlib import html.parser import io import json import urllib import urllib.request import urllib.parse template_string = r''' TF2 Attribute List

List of Team Fortress 2 attributes:

Page generated for schema dated {{update_time}}. Click here to download this page for offline viewing.

{{#result.attributes}} {{/result.attributes}}
ID Name Description Value Type Class
{{defindex}} {{name}} {{description_string}} {{description_format}} {{attribute_class}}
{{#inline_scripts}} {{/inline_scripts}} ''' def get_schema_overview(params): opts = dict(params.items()) url = "https://api.steampowered.com/IEconItems_440/GetSchemaOverview/v0001/" with urllib.request.urlopen(f"{url}?{urllib.parse.urlencode(opts)}") as r: return { **json.load(r), 'update_time': r.info().get('last-modified') } def inline_scripts(text, render): """ Helper function to parse script elements, download the scripts and inline them. """ class ScriptElementParser(html.parser.HTMLParser): def __init__(self): super(ScriptElementParser, self).__init__() self.scripts = [] self.current = [] def handle_starttag(self, tag, attrs): if tag == 'script': self.current.append(dict(attrs)) def handle_endtag(self, tag): self.scripts.append(self.current.pop()) parser = ScriptElementParser() parser.feed(text) f = io.StringIO() for script in parser.scripts: with urllib.request.urlopen(script['src']) as r: data = r.read() if 'integrity' in script: ct, hash = script['integrity'].split('-', 1) h = hashlib.new(ct) h.update(data) if base64.b64encode(h.digest()).decode('ascii') != hash: raise ValueError(f"{script['src']} failed subresource integrity check {script['integrity']} (got {base64.b64encode(h.digest())})") f.write(f"\n") return f.getvalue() config = configparser.ConfigParser() config.read('config.ini', encoding = "utf8") # tf_schema_overview.json is the WebAPI call to IEconItems_440/GetSchemaOverview with open('attributes.html', 'wt', encoding = 'utf8') as f: f.write(chevron.render(template_string, { **get_schema_overview(config['DEFAULT']), 'inline_scripts': inline_scripts, }))