forked from woctezuma/steam-market
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing_utils.py
133 lines (96 loc) · 4.1 KB
/
parsing_utils.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
# Objective: parse all the options to craft 'Booster Packs', available because I own the corresponding games.
#
# Caveat: this relies:
# - either on a previous manual copy-paste of HTML code to data/booster_game_creator.txt
# - or on a previous manual copy-paste of javascript code to data/booster_game_creator_from_javascript.txt
import json
from pathlib import Path
from market_listing import fix_app_name_for_url_query
from utils import get_badge_creation_file_name
def parse_javascript_one_liner(
badges_as_str: str,
verbose: bool = False,
) -> dict[int, dict]:
badge_creation_details = {}
print('Parsing the one-line javascript code displayed with the web browser.')
badges = json.loads(badges_as_str.lstrip().rstrip(','))
for badge in badges:
app_id, next_creation_time = badge['appid'], "available now"
badge_creation_details[app_id] = {
'name': (app_name := badge['name']),
'gem_value': (gem_value := int(badge['price'])),
}
if badge.get('unavailable', False):
next_creation_time = badge['available_at_time']
badge_creation_details[app_id]['next_creation_time'] = next_creation_time
print(
f'Loading the next creation time ({next_creation_time}) for {app_name} ({app_id = }) from the Booster Pack Creator list.',
)
if verbose:
print(f'{app_id:<10}{app_name}\t{gem_value} gems\t{next_creation_time}')
return badge_creation_details
def parse_augmented_steam_drop_down_menu(
lines: list[str],
verbose: bool = False,
) -> dict[int, dict]:
badge_creation_details = {}
print('Parsing the drop-down menu displayed with Augmented Steam.')
for line in lines:
s = line.split()
# e.g. ['<option', 'value="614910"', 'class="available">#monstercakes', '-', '1200', 'Gems</option>']
# Hard-coded parsing
app_id = int(s[1].split('=')[1].strip('"'))
app_name = s[2].split('available">')[1] + ' '
app_name += ' '.join(s[3:-3])
app_name = app_name.strip()
gem_value = int(s[-2])
app_name = fix_app_name_for_url_query(app_name)
badge_creation_details[app_id] = {}
badge_creation_details[app_id]['name'] = app_name
badge_creation_details[app_id]['gem_value'] = gem_value
if verbose:
print(f'{app_id}\t{app_name}\t{gem_value}')
return badge_creation_details
def parse_badge_creation_details(
badge_creation_file_name: str = None,
from_javascript: bool = False,
verbose: bool = False,
) -> dict[int, dict]:
if badge_creation_file_name is None:
badge_creation_file_name = get_badge_creation_file_name(
from_javascript=from_javascript,
)
if not Path(badge_creation_file_name).exists():
badge_creation_file_name = get_badge_creation_file_name(
from_javascript=not from_javascript,
)
with open(badge_creation_file_name, encoding='utf-8') as f:
lines = [line.strip() for line in f.readlines() if line[0] != '#']
if len(lines) > 1:
badge_creation_details = parse_augmented_steam_drop_down_menu(
lines,
verbose=verbose,
)
else:
badges_as_str = lines[0]
badge_creation_details = parse_javascript_one_liner(
badges_as_str,
verbose=verbose,
)
return badge_creation_details
def main() -> bool:
badge_creation_details = parse_badge_creation_details(
get_badge_creation_file_name(from_javascript=False),
)
print(f'#badges = {len(badge_creation_details)}')
badge_creation_details = parse_badge_creation_details(
get_badge_creation_file_name(from_javascript=True),
)
print(f'#badges = {len(badge_creation_details)}')
badge_creation_details = parse_badge_creation_details(from_javascript=False)
print(f'#badges = {len(badge_creation_details)}')
badge_creation_details = parse_badge_creation_details(from_javascript=True)
print(f'#badges = {len(badge_creation_details)}')
return True
if __name__ == '__main__':
main()