Example #1
0
    def parse_lowest_price(html_data):
        htmlparser = html5lib.HTMLParser(
            tree=html5lib.treebuilders.getTreeBuilder("lxml"),
            namespaceHTMLElements=False
        )

        page = htmlparser.parse(html_data)

        table_selector = lxml.cssselect.CSSSelector("table.avail-table")
        fare_selector = lxml.cssselect.CSSSelector("div.avail-fare-price")
        total_price = Decimal(0)
        for table in table_selector(page):
            price_str_list = [
                fare.text.strip()
                for fare in fare_selector(table)
            ]

            if not price_str_list:
                raise ValueError(html_data)

            lowest_price = None
            currency_code = None
            for price_str in price_str_list:
                price_, currency_code_ = price_str.split(" ")
                if currency_code is None:
                    currency_code = currency_code_
                else:
                    assert currency_code == currency_code_
                price_ = Decimal(
                    price_.strip(u"≈ ").replace(",", "")
                )
                if lowest_price is None or price_ < lowest_price:
                    lowest_price = price_

            assert currency_code is not None

            if currency_code != 'CNY':
                lowest_price_in_cny = exchange_to_cny(currency_code, lowest_price)
            else:
                lowest_price_in_cny = lowest_price

            total_price += lowest_price_in_cny
        return total_price
Example #2
0
    def parse_lowest_price(json_data):
        price_list = []
        currency_code = None
        for flight in json_data['outbound']:
            for seat in flight['prices']:
                price = seat['price']
                if currency_code is None:
                    currency_code = price['currencyCode']
                else:
                    assert currency_code == price['currencyCode']
                price_list.append(Decimal(price['value']))
        if not price_list:
            return None

        lowest_price = min(price_list)
        if currency_code != 'CNY':
            lowest_price_in_cny = exchange_to_cny(currency_code, lowest_price)
        else:
            lowest_price_in_cny = lowest_price
        return lowest_price_in_cny
Example #3
0
    def parse_lowest_price(html_data):
        htmlparser = html5lib.HTMLParser(
            tree=html5lib.treebuilders.getTreeBuilder("lxml"),
            namespaceHTMLElements=False)

        page = htmlparser.parse(html_data)

        table_selector = lxml.cssselect.CSSSelector("table.avail-table")
        fare_selector = lxml.cssselect.CSSSelector("div.avail-fare-price")
        total_price = Decimal(0)
        for table in table_selector(page):
            price_str_list = [
                fare.text.strip() for fare in fare_selector(table)
            ]

            if not price_str_list:
                raise ValueError(html_data)

            lowest_price = None
            currency_code = None
            for price_str in price_str_list:
                price_, currency_code_ = price_str.split(" ")
                if currency_code is None:
                    currency_code = currency_code_
                else:
                    assert currency_code == currency_code_
                price_ = Decimal(price_.strip(u"≈ ").replace(",", ""))
                if lowest_price is None or price_ < lowest_price:
                    lowest_price = price_

            assert currency_code is not None

            if currency_code != 'CNY':
                lowest_price_in_cny = exchange_to_cny(currency_code,
                                                      lowest_price)
            else:
                lowest_price_in_cny = lowest_price

            total_price += lowest_price_in_cny
        return total_price