forked from woctezuma/steam-market
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersonal_info.py
114 lines (81 loc) · 3.01 KB
/
personal_info.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
# Reference: https://www.blakeporterneuro.com/learning-python-project-3-scrapping-data-from-steams-community-market/
from src.json_utils import load_json, save_json
def get_steam_cookie_file_name() -> str:
return "personal_info.json"
def load_steam_cookie_from_disk(
file_name_with_personal_info: str | None = None,
) -> dict[str, str]:
if file_name_with_personal_info is None:
file_name_with_personal_info = get_steam_cookie_file_name()
try:
cookie = load_json(file_name_with_personal_info)
except FileNotFoundError:
cookie = {}
return cookie
def save_steam_cookie_to_disk(
cookie: dict[str, str],
file_name_with_personal_info: str | None = None,
) -> bool:
if file_name_with_personal_info is None:
file_name_with_personal_info = get_steam_cookie_file_name()
is_cookie_to_be_saved = bool(cookie is not None and len(cookie) > 0)
if is_cookie_to_be_saved:
save_json(cookie, file_name_with_personal_info)
return is_cookie_to_be_saved
def get_cookie_dict(verbose: bool = False) -> dict[str, str]:
cookie = load_steam_cookie_from_disk()
if verbose:
for field in cookie:
print(f"{field}: {cookie[field]}")
return cookie
def update_cookie_dict(
original_cookie: dict[str, str],
dict_with_new_values: dict[str, str],
verbose: bool = False,
) -> dict[str, str]:
cookie = original_cookie
for field in dict_with_new_values:
try:
current_value = cookie[field]
except KeyError:
current_value = None
new_value = dict_with_new_values[field]
if new_value != current_value:
print(
f"Updating value for cookie field {field} from {current_value} to {new_value}.",
)
cookie[field] = new_value
if verbose:
for field in cookie:
print(f"{field}: {cookie[field]}")
return cookie
def update_and_save_cookie_to_disk_if_values_changed(
cookie: dict[str, str],
dict_with_new_values: dict[str, str],
fields: list[str] | None = None,
file_name_with_personal_info: str | None = None,
verbose: bool = False,
) -> dict[str, str]:
if fields is None:
fields = ["steamLoginSecure", "sessionid", "steamDidLoginRefresh"]
relevant_fields = set(fields)
relevant_fields = relevant_fields.intersection(cookie.keys())
relevant_fields = relevant_fields.intersection(dict_with_new_values.keys())
is_cookie_to_be_updated = any(
dict_with_new_values[field] != cookie[field] for field in relevant_fields
)
if is_cookie_to_be_updated:
cookie = update_cookie_dict(
original_cookie=cookie,
dict_with_new_values=dict_with_new_values,
verbose=verbose,
)
save_steam_cookie_to_disk(
cookie=cookie,
file_name_with_personal_info=file_name_with_personal_info,
)
return cookie
def main() -> None:
get_cookie_dict(verbose=True)
if __name__ == "__main__":
main()