-
Notifications
You must be signed in to change notification settings - Fork 1
/
personal_info.py
115 lines (79 loc) · 2.96 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
115
# The code shown below is an excerpt from:
# https://github.com/woctezuma/steam-market/blob/master/personal_info.py
# Reference: https://www.blakeporterneuro.com/learning-python-project-3-scrapping-data-from-steams-community-market/
import json
def get_steam_cookie_file_name():
steam_cookie_file_name = 'personal_info.json'
return steam_cookie_file_name
def load_steam_cookie_from_disk(file_name_with_personal_info=None):
if file_name_with_personal_info is None:
file_name_with_personal_info = get_steam_cookie_file_name()
try:
with open(file_name_with_personal_info) as f:
cookie = json.load(f)
except FileNotFoundError:
cookie = {}
return cookie
def save_steam_cookie_to_disk(cookie, file_name_with_personal_info=None):
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:
with open(file_name_with_personal_info, 'w') as f:
json.dump(cookie, f)
return is_cookie_to_be_saved
def get_cookie_dict(verbose=False):
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_with_new_values, verbose=False):
cookie = original_cookie
for field in dict_with_new_values:
current_value = cookie[field]
new_value = dict_with_new_values[field]
if new_value != current_value:
print(
'Updating value for cookie field {} from {} to {}.'.format(
field,
current_value,
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_with_new_values,
fields=None,
file_name_with_personal_info=None,
verbose=False,
):
if fields is None:
fields = ['steamLoginSecure', 'sessionid']
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,
)
is_cookie_to_be_saved = save_steam_cookie_to_disk(
cookie=cookie,
file_name_with_personal_info=file_name_with_personal_info,
)
return cookie
def main():
cookie = get_cookie_dict(verbose=True)
return True
if __name__ == '__main__':
main()