Completed
Push — master ( 72b331...d4b7d2 )
by Steffen
02:14
created

kuon.watcher.adapters.bitskins.parser.search_response   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 44
dl 0
loc 70
rs 10
c 0
b 0
f 0

2 Methods

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