예제 #1
0
    def search_without_cache(self,
                             dep_code,
                             arr_code,
                             departure_date,
                             return_date=None):
        http_headers = self.BASE_HTTP_HEADER.copy()
        http_headers['x-forwarded-for'] = get_fake_ip()
        dd1_str = departure_date.strftime("%Y-%m-%d")
        params = {
            'o1': dep_code,
            'd1': arr_code,
            'dd1': dd1_str,
            'ADT': 1,
            'CHD': 0,
            'inl': 0,
            's': True,
            'mon': True,
            'loy': True,
            'cc': 'CNY',
        }
        if return_date is not None:
            params["dd2"] = return_date.strftime("%Y-%m-%d")

        res = self.http.get(self.BASE_URL, headers=http_headers, params=params)
        if res.status_code == 200:
            return res.text

        if res.status_code in (502, 504):
            return
        else:
            raise NotImplementedError("Unknown code: %s\n%s" %
                                      (res.status_code, res.text))
예제 #2
0
    def search_without_cache(self, dep_code, arr_code, departure_date,
                             return_date=None):
        http_headers = self.BASE_HTTP_HEADER.copy()
        http_headers['x-forwarded-for'] = get_fake_ip()
        dd1_str = departure_date.strftime("%Y-%m-%d")
        params = {
            'o1': dep_code,
            'd1': arr_code,
            'dd1': dd1_str,
            'ADT': 1,
            'CHD': 0,
            'inl': 0,
            's': True,
            'mon': True,
            'loy': True,
            'cc': 'CNY',
        }
        if return_date is not None:
            params["dd2"] = return_date.strftime("%Y-%m-%d")

        res = self.http.get(
            self.BASE_URL,
            headers=http_headers,
            params=params
        )
        if res.status_code == 200:
            return res.text

        if res.status_code in (502, 504):
            return
        else:
            raise NotImplementedError(
                "Unknown code: %s\n%s" % (res.status_code, res.text)
            )
예제 #3
0
    def _action_302(self, dep_code="PEK", arr_code="KUL",
                    departure_date=date(2016, 5, 2), return_date=None):
        ''' query '''
        departure_date_str = departure_date.strftime("%Y-%m-%d")
        if return_date is None:
            return_date_str = ""
        else:
            return_date_str = return_date.strftime("%Y-%m-%d")

        query_params = {
            "MCC": "460",
            "MNC": "01",
            "actionCode": "302",
            "adultPax": "1",
            "appVersion": "3.3.0",
            "arrivalStation": arr_code,
            "childPax": "0",
            "countryCode": "CN",
            "cultureCode": "en-GB",
            "departureDate": departure_date_str,
            "departureStation": dep_code,
            "deviceBrand": "apple",
            "deviceID": self.DEVICE_ID,
            "deviceModel": "iPhone7,2",
            "infantPax": "0",
            "operatingSystem": "ios",
            "osVersion": "9.2",
            "password": _CMP,
            "requestFrom": "0",
            "returnDate": return_date_str,
            "userCurrencyCode": "CNY",
            "userEmail": self.session["useremail"],
            "userSession": self.session["user_session"],
            "username": "******",
        }
        hash_key = self.get_hash_key(query_params)
        query_params["hashKey"] = hash_key

        http_headers = self.BASE_HTTP_HEADER.copy()
        http_headers["x-forwarded-for"] = get_fake_ip()
        res = self.http.post(
            self.BASE_URL,
            headers=http_headers,
            data=query_params,
        )
        if res.status_code != 200:
            raise ValueError(res.status_code)
        d = res.json()
        if d["response"]["responseCode"] != 1000:
            raise ValueError(d)

        if d["data"]["FlightSearch"] == [[]]:
            raise ValueError(d)

        return d["data"]
예제 #4
0
    def _action_205(self):
        '''unknown confirm 1'''
        query_params = {
            "MCC": "460",
            "MNC": "01",
            "actionCode": "205",
            "appVersion": "3.3.0",
            "countryCode": "CN",
            "cultureCode": "en-GB",
            "customerId": self.session["big_member_id"],
            "deviceBrand": "apple",
            "deviceID": self.DEVICE_ID,
            "deviceModel": "iPhone7,2",
            "operatingSystem": "ios",
            "osVersion": "9.2",
            "password": _CMP,
            "ssoTicket": self.session["sso_ticket"],
            "userEmail": self.session["useremail"],
            "username": self.session["username"],
        }

        hash_key = self.get_hash_key(query_params)
        query_params["hashKey"] = hash_key

        http_headers = self.BASE_HTTP_HEADER.copy()
        http_headers["x-forwarded-for"] = get_fake_ip()
        res = self.http.post(
            self.BASE_URL,
            headers=http_headers,
            data=query_params,
        )
        if res.status_code != 200:
            raise ValueError(res.status_code)
        d = res.json()
        if d["response"]["responseCode"] != 1000:
            raise ValueError(d)

        self.session["sso_ticket"] = str(d["data"]["ssoTicket"])
예제 #5
0
    def _action_103(self):
        '''get query session'''
        query_params = {
            "MCC": "460",
            "MNC": "01",
            "actionCode": "103",
            "appVersion": "3.3.0",
            "countryCode": "CN",
            "cultureCode": "en-GB",
            "deviceBrand": "apple",
            "deviceID": self.DEVICE_ID,
            "deviceModel": "iPhone7,2",
            "operatingSystem": "ios",
            "osVersion": "9.2",
            "password": _CMP,
            "requestFrom": "0",
            "requestType": "1",
            "userEmail": self.session["useremail"],
            "username": "******",
        }
        hash_key = self.get_hash_key(query_params)
        query_params["hashKey"] = hash_key

        http_headers = self.BASE_HTTP_HEADER.copy()
        http_headers["x-forwarded-for"] = get_fake_ip()
        res = self.http.post(
            self.BASE_URL,
            headers=http_headers,
            data=query_params,
        )
        if res.status_code != 200:
            raise ValueError(res.status_code)
        d = res.json()
        if d["response"]["responseCode"] != 1000:
            raise ValueError(d)

        self.session["user_session"] = str(d["data"]["userSession"])
예제 #6
0
class Searcher(object):

    BASE_URL = ('https://cebu-booking.ink-global.com'
                '/api/tenants/cebu/availability;ADT=1')
    BASE_HEADER_KEY, BASE_HEADER_VALUE = urllib.quote(BASE_URL, safe="=").split("=")
    BASE_HTTP_HEADER = {
        'content-type': 'application/vnd.inkglobal.flights.v1+json',
        'accept': ('application/json,'
                   'application/vnd.inkglobal.flights.v1+json,'
                   'application/vnd.inkglobal.flights.v1+json'),
        'user-agent': 'iPhone,iPhone',
        'accept-language': 'zh-cn',
        'x-forwarded-for': get_fake_ip(),
        BASE_HEADER_KEY: BASE_HEADER_VALUE
    }
    AIRLINE_NAME = 'CebuPacific'
    FLIGHT_SCHEDULE = set(FLIGHT_SCHEDULE)

    def __init__(self):
        self.http = requests.Session()

    def __del__(self):
        self.http.close()

    @cache("price:cebu_pacific:{dep_code}:{arr_code}:{departure_date}", 60)
    def search(self, dep_code, arr_code, departure_date):
        res = self.http.get(
            self.BASE_URL,
            headers=self.BASE_HTTP_HEADER,
            params={
                'from': dep_code,
                'to': arr_code,
                'departureDate': departure_date.strftime("%Y-%m-%d"),
            }
        )
        return res.json()

    @staticmethod
    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

    def get_lowest_price(self, dep_code='PEK', arr_code='MNL',
                         departure_date=date(2016, 5, 2)):
        json_data = self.search(dep_code, arr_code, departure_date)
        return self.parse_lowest_price(json_data)