Completed
Push — master ( 44c083...76de5b )
by Asif
32:09
created

pysmartprice.SmartPrice.sellers()   A

Complexity

Conditions 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
dl 0
loc 10
rs 9.2
1
from pysmartprice.smartparser import(
2
    PriceListParser,
3
    SearchParser,
4
    SellerParser
5
)
6
from pysmartprice.constants import SMARTPRICE_ATTRS
7
8
9
class SmartPrice(object):
10
11
    def parser_results(self, product, **kwargs):
12
        parser = PriceListParser(product, **kwargs)
13
        return parser.price_results
14
15
    def __getattr__(self, attr):
16
        if attr not in SMARTPRICE_ATTRS:
17
            msg = '{} object has no attribute {}'
18
            raise AttributeError(msg.format(self.__class__.__name__, attr))
19
20
        setattr(self, attr, self.parser_results(SMARTPRICE_ATTRS[attr]))
21
        return getattr(self, attr)
22
23
    def search(self, search_key):
24
        params = dict(s=search_key, page=1)
25
        parser = SearchParser('search', **params)
26
        return parser.price_results
27
28
    def sellers(self, product):
29
        search_res = self.search(product)
30
        products = [
31
            res for res in search_res if product.lower() in res.title.lower()]
32
33
        for product in products:
34
            seller_parser = SellerParser(product.url)
35
            setattr(product, 'sellers', seller_parser.result)
36
37
        return products
38