|
1
|
|
|
#!/usr/bin/python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
1 |
|
import json |
|
4
|
|
|
|
|
5
|
1 |
|
from kuon.api_response import APIResponse |
|
6
|
1 |
|
from kuon.watcher.adapters.models.item import Item |
|
7
|
1 |
|
from kuon.watcher.adapters.models.search_response import SearchResponse |
|
8
|
1 |
|
from kuon.watcher.adapters.models.sticker import Sticker |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
1 |
|
class SearchResponseParser(object): |
|
12
|
|
|
"""Parser class to parse the response of the search to the unified format""" |
|
13
|
|
|
|
|
14
|
1 |
|
@staticmethod |
|
15
|
1 |
|
def parse(results: dict) -> APIResponse: |
|
16
|
|
|
"""Parse the item model |
|
17
|
|
|
|
|
18
|
|
|
:type results: dict |
|
19
|
|
|
:return: |
|
20
|
|
|
""" |
|
21
|
|
|
success = results['status'] == 'success' and 'items' in results['data'] |
|
22
|
|
|
response = SearchResponse(success=success) |
|
23
|
|
|
|
|
24
|
|
|
if success: |
|
25
|
|
|
for item in results['data']['items']: |
|
26
|
|
|
wear = item['float_value'] |
|
27
|
|
|
if wear is None: |
|
28
|
|
|
wear = -1.0 |
|
29
|
|
|
|
|
30
|
|
|
item_model = Item( |
|
31
|
|
|
market_name=item['market_hash_name'], |
|
32
|
|
|
item_id=item['item_id'], |
|
33
|
|
|
app_id=int(item['app_id']), |
|
34
|
|
|
class_id=int(item['class_id']), |
|
35
|
|
|
context_id=int(item['context_id']), |
|
36
|
|
|
instance_id=int(item['instance_id']), |
|
37
|
|
|
price=int(float(item['price']) * 100), |
|
38
|
|
|
wear_value=float(wear), |
|
39
|
|
|
image=item['image'], |
|
40
|
|
|
inspect_link=item['inspect_link'], |
|
41
|
|
|
stickers=SearchResponseParser.get_stickers(item['stickers']) |
|
42
|
|
|
) |
|
43
|
|
|
response.add_item(item_model) |
|
44
|
|
|
return APIResponse(json.dumps(response.__dict__)) |
|
45
|
|
|
|
|
46
|
1 |
|
@staticmethod |
|
47
|
1 |
|
def get_stickers(stickers: list) -> list: |
|
48
|
|
|
"""Parse the sticker value |
|
49
|
|
|
|
|
50
|
|
|
:type stickers: list |
|
51
|
|
|
:return: |
|
52
|
|
|
""" |
|
53
|
|
|
if not stickers: |
|
54
|
|
|
return [] |
|
55
|
|
|
|
|
56
|
|
|
sticker_results = [] |
|
57
|
|
|
for sticker in stickers: |
|
58
|
|
|
wear = sticker['wear_value'] |
|
59
|
|
|
if wear is None: |
|
60
|
|
|
wear = -1.0 |
|
61
|
|
|
|
|
62
|
|
|
sticker_results.append(Sticker( |
|
63
|
|
|
name=sticker['name'], |
|
64
|
|
|
image=sticker['url'], |
|
65
|
|
|
wear_value=float(wear) |
|
66
|
|
|
)) |
|
67
|
|
|
|
|
68
|
|
|
return sticker_results |
|
69
|
|
|
|