-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.py
357 lines (306 loc) · 18.2 KB
/
config.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import re
from typing import Dict, Union
from json import loads
from os.path import dirname
class Config(object):
debug: bool = False # Enable debug log
proxy: Dict = {} # Set proxy
mobile_confirmation: bool = False # Need mobile confirmation after list on market
language: str = 'english' # !important the language user preferred
steam_login_secure: str = None # The steam website cookie
steam_id: str = None # Steam id
app_id: int = 753 # The game which want to sell
context_id: str = '6' # The game's context id which want to sell
allow_to_sell_item = {
'enable': True,
'item_type': set() # Type: int
}
disallow_to_sell_item = {
'enable': False,
'item_type': set() # Type: int
}
allow_to_sell_item_detail = {
'enable': False,
'item_detail_type': set() # Type: str
}
disallow_to_sell_item_detail = {
'enable': False,
'item_detail_type': set() # Type: str
}
price_setting = {
'lowest_price': None, # Type: float; The item selling price must above the lowest_price
'highest_price': None, # Type: float; The item selling price must lower than the highest_price
'calculation_formula': None, # Type: str; The calculation formula
'least_sells_hours': 36, # In least_sells_hours must have hours_least_sells
'hours_least_sells': 25, # In least_sells_hours must have hours_least_sells
'least_sell_orders': 20, # The item must have least_sell_orders sell orders now
'least_buy_orders': 0, # The item must have least_buy_orders buy orders now
'normal_card': {
'lowest_price': None, # Type: float; The normal card selling price must above the lowest_price
'highest_price': None # Type: float; The normal card selling price must lower than the highest_price
},
'foil_card': {
'lowest_price': None, # Type: float; The foil card selling price must above the lowest_price
'highest_price': None # Type: float; The foil card selling price must lower than the highest_price
},
'other_item': {
'lowest_price': None, # Type: float; The other items' selling price must above the lowest_price
'highest_price': None # Type: float; The other items' selling price must lower than the highest_price
}
}
__MUST_CONFIG = ( # The params in this set must be configured
'self.steam_login_secure',
'self.steam_id',
"self.price_setting['calculation_formula']",
)
__CONFIG_TYPE = { # The config params type
'debug': (bool,),
'proxy': (str,),
'mobile_confirmation': (bool,),
'language': (str,),
'steam_login_secure': (str,),
'steam_id': (str,),
'app_id': (int,),
'context_id': (int, str),
'allow_to_sell_item': (dict,),
'allow_to_sell_item_value': {
'enable': (bool,),
'item_type': (set,),
'item_type_value': int
},
'disallow_to_sell_item': (dict,),
'disallow_to_sell_item_value': {
'enable': (bool,),
'item_type': (set,),
'item_type_value': int
},
'allow_to_sell_item_detail': (dict,),
'allow_to_sell_item_detail_value': {
'enable': (bool,),
'item_detail_type': (set,),
'item_detail_type_value': str,
},
'disallow_to_sell_item_detail': (dict,),
'disallow_to_sell_item_detail_value': {
'enable': (bool,),
'item_detail_type': (set,),
'item_detail_type_value': str,
},
'price_setting': (dict,),
'price_setting_value': {
'lowest_price': (float, int, type(None)),
'highest_price': (float, int, type(None)),
'calculation_formula': (str,),
'least_sells_hours': (int,),
'hours_least_sells': (int,),
'least_sell_orders': (int,),
'least_buy_orders': (int,),
'normal_card': (dict, type(None)),
'normal_card_value': {
'lowest_price': (float, int, type(None)),
'highest_price': (float, int, type(None))
},
'foil_card': (dict, type(None)),
'foil_card_value': {
'lowest_price': (float, int, type(None)),
'highest_price': (float, int, type(None))
},
'other_item': (dict, type(None)),
'other_item_value': {
'lowest_price': (float, int, type(None)),
'highest_price': (float, int, type(None))
}
}
}
def __init__(self):
data = self.__load_config()
self.__check_config_type(data)
self.__set_config(data)
self.__check_must_config()
def __check_config_type(self, config_data: Dict) -> None:
"""
Check json file parameters type
:param config_data: the raw config dict
:return: None
:raises (ConfigFileErrorException)
"""
if not isinstance(config_data, dict):
raise ConfigFileErrorException('Json is not a dict')
def __check_dict(dict_data: Dict, cmp_dict: Dict):
for k in dict_data.keys():
if cmp_dict.get(k, False):
if cmp_dict.get(k) == set or set in cmp_dict.get(k):
if isinstance(dict_data[k], list):
for data in dict_data[k]:
if not isinstance(data, cmp_dict.get(k + '_value')):
raise ConfigFileErrorException('Key: %s is in a wrong format' % k)
elif isinstance(dict_data[k], cmp_dict.get(k)):
pass
else:
raise ConfigFileErrorException('Key: %s is in a wrong format' % k)
elif cmp_dict.get(k) == dict or dict in cmp_dict.get(k):
if isinstance(dict_data[k], cmp_dict.get(k)):
__check_dict(dict_data[k], cmp_dict.get(k + '_value'))
else:
raise ConfigFileErrorException('Key: %s is in a wrong format' % k)
else:
if not isinstance(dict_data[k], cmp_dict.get(k)):
raise ConfigFileErrorException('Key: %s is in a wrong format' % k)
__check_dict(config_data, self.__CONFIG_TYPE)
def __check_must_config(self) -> None:
"""
Check whether the must config is set or not
:return: None
:raises (KeyNotConfigException)
"""
for config_key in self.__MUST_CONFIG:
if eval(config_key) is None:
raise KeyNotConfigException('The must config not set')
def __set_config(self, config_data: Dict) -> None:
"""
Set the config object
:param config_data: the raw config dict
:return: None
:raises (ConfigFileErrorException)
"""
self.debug: bool = config_data.get('debug', False)
proxy: str = config_data.get('proxy', '')
if proxy != '':
if not re.match(r'http|socks5|https://.*', proxy):
raise ConfigFileErrorException('Key: proxy is in a wrong format')
else:
self.proxy = {
'http': proxy,
'https': proxy
}
else:
self.proxy = {}
self.mobile_confirmation: bool = config_data.get('mobile_confirmation', False)
self.language: str = config_data.get('language', 'english')
self.steam_login_secure: str = config_data.get('steam_login_secure', '').replace('%7C', '|').replace('%7c', '|')
self.steam_id: str = config_data.get('steam_id', '')
if len(self.steam_id) != 17:
raise ConfigFileErrorException('Key: steam_id is in a wrong format')
if self.steam_id != self.steam_login_secure.split('|')[0]:
raise ConfigFileErrorException("Key: steam_id and steam_login_secure can't match")
self.app_id: int = config_data.get('app_id', 753)
self.context_id: str = config_data.get('context_id', 6)
self.allow_to_sell_item = config_data.setdefault('allow_to_sell_item', {'enable': True, 'item_type': set()})
self.allow_to_sell_item['enable'] = config_data.get('allow_to_sell_item').get('enable', True)
self.allow_to_sell_item['item_type'] = set(config_data.get('allow_to_sell_item').get('item_type', set()))
self.disallow_to_sell_item = config_data.setdefault('disallow_to_sell_item', {'enable': False,
'item_type': set()})
self.disallow_to_sell_item['enable'] = config_data.get('disallow_to_sell_item').get('enable', False)
self.disallow_to_sell_item['item_type'] = set(config_data.get('disallow_to_sell_item').get('item_type', set()))
self.allow_to_sell_item_detail = config_data.setdefault('allow_to_sell_item_detail', {'enable': False,
'item_detail_type': set()
})
self.allow_to_sell_item_detail['enable'] = config_data.get('allow_to_sell_item_detail').get('enable', False)
self.allow_to_sell_item_detail['item_detail_type'] = set(config_data.get('allow_to_sell_item_detail')
.get('item_detail_type', set()))
self.disallow_to_sell_item_detail = config_data.setdefault('disallow_to_sell_item_detail',
{'enable': False, 'item_detail_type': set()})
self.disallow_to_sell_item_detail['enable'] = config_data.get('disallow_to_sell_item_detail').get('enable',
False)
self.disallow_to_sell_item_detail['item_detail_type'] = set(config_data.get('disallow_to_sell_item_detail')
.get('item_detail_type', set()))
self.price_setting = config_data.setdefault('price_setting', {'lowest_price': None,
'highest_price': None,
'calculation_formula': None,
'least_sells_hours': 36,
'hours_least_sells': 25,
'least_sell_orders': 20,
'least_buy_orders': None,
'normal_card': {
'lowest_price': None,
'highest_price': None
},
'foil_card': {
'lowest_price': None,
'highest_price': None
},
'other_item': {
'lowest_price': None,
'highest_price': None
}})
def __price_check(price: Union[int, float], key_name: str) -> float:
if price is not None:
if price < 0.0:
raise ConfigFileErrorException("Key: %s isn't correct" % key_name)
else:
return float(price)
self.price_setting['lowest_price'] = __price_check(config_data.get('price_setting').get('lowest_price', None),
'price_setting.lowest_price')
self.price_setting['highest_price'] = __price_check(config_data.get('price_setting').get('highest_price', None),
'price_setting.highest_price')
if self.price_setting['highest_price'] is not None and \
self.price_setting['lowest_price'] is not None and \
self.price_setting['highest_price'] < self.price_setting['lowest_price']:
raise ConfigFileErrorException("Key: price_setting.highest_price or "
"price_setting.lowest_price isn't correct")
self.price_setting['calculation_formula'] = config_data.get('price_setting').get('calculation_formula', None)
def __check_int(num: int, key_name: str) -> None:
if num < 0:
raise ConfigFileErrorException("Key: %s isn't correct" % key_name)
self.price_setting['least_sells_hours'] = config_data.get('price_setting').get('least_sells_hours', 36)
__check_int(self.price_setting['least_sells_hours'], 'price_setting.least_sells_hours')
self.price_setting['hours_least_sells'] = config_data.get('price_setting'
).get('hours_least_sells',
25)
__check_int(self.price_setting['hours_least_sells'],
'price_setting.hours_least_sells')
self.price_setting['least_sell_orders'] = config_data.get('price_setting').get('least_sell_orders', 25)
__check_int(self.price_setting['least_sell_orders'], 'price_setting.least_sell_orders')
self.price_setting['least_buy_orders'] = config_data.get('price_setting').get('least_buy_orders', 0)
__check_int(self.price_setting['least_buy_orders'], 'price_setting.least_buy_orders')
self.price_setting['normal_card'] = config_data.get('price_setting').setdefault('normal_card',
{'lowest_price': None,
'highest_price': None})
self.price_setting['normal_card']['lowest_price'] = __price_check(config_data.get('price_setting')
.get('normal_card')
.get('lowest_price', None),
'price_setting.normal_card.lowest_price')
self.price_setting['normal_card']['highest_price'] = __price_check(config_data.get('price_setting')
.get('normal_card')
.get('highest_price', None),
'price_setting.normal_card.highest_price')
self.price_setting['foil_card'] = config_data.get('price_setting').setdefault('foil_card',
{'lowest_price': None,
'highest_price': None})
self.price_setting['foil_card']['lowest_price'] = __price_check(config_data.get('price_setting')
.get('foil_card')
.get('lowest_price', None),
'price_setting.foil_card.lowest_price')
self.price_setting['foil_card']['highest_price'] = __price_check(config_data.get('price_setting')
.get('foil_card')
.get('highest_price', None),
'price_setting.foil_card.highest_price')
self.price_setting['foil_card'] = config_data.get('price_setting').setdefault('foil_card',
{'lowest_price': None,
'highest_price': None})
self.price_setting['other_item']['lowest_price'] = __price_check(config_data.get('price_setting')
.get('other_item')
.get('lowest_price', None),
'price_setting.other_item.lowest_price')
self.price_setting['other_item']['highest_price'] = __price_check(config_data.get('price_setting')
.get('other_item')
.get('highest_price', None),
'price_setting.other_item.highest_price')
@staticmethod
def __load_config() -> Dict:
"""
Load config file
:return: Config Dict
:rtype Dict
:raises (FileNotFoundError, JSONDecodeError)
"""
path = dirname(__file__) + '/config.json'
f = open(path, 'r', encoding='utf-8')
configs = f.read()
f.close()
return loads(configs)
class ConfigFileErrorException(Exception):
"""The config json is not in a right format"""
class KeyNotConfigException(Exception):
"""The must config not set"""
config = Config()