Ejemplo n.º 1
0
def run():
    logs = Logs()

    # API - Fetch the models
    models_url = "make/mazda/models"
    logs.debug("API - Fetching models")
    response = api_get(models_url)
    if response.status_code != 200:
        logs.error("API - Cannot fetch models - Status code: " +
                   response.status_code)  # nopep8
        return logs
    api_json = json.loads(response.content)
    api_models = api_json['models']

    # Mazda - Fetch the models
    logs.debug("Mazda - Fetching models")
    response = requests.get(
        "https://www.mazda.ca/common/apps/assets/json/step1/qc.json?v=1547651328198"
    )  # nopep8
    if response.status_code != 200:
        logs.error("Mazda - Cannot fetch models")
        return logs
    mazda_json = json.loads(response.content)

    # Mazda - Reformat models
    mazda_models = []
    for single_category in mazda_json['categories']:
        for single_model in single_category['carline']:
            for single_year in single_model['modelYear']:
                model = {
                    "name":
                    single_model['carline'][0].upper() +
                    single_model['carline'][1:].lower(),  # nopep8
                    "year":
                    str(single_year['year']),
                    "foreign_id":
                    single_model['carline'] + " " +
                    str(single_year['year']),  # nopep8
                }
                mazda_models.append(model)

    # Mazda - Validate models
    if len(mazda_models) == 0:
        logs.error("Mazda - No models founds")
        return logs

    # Validate each Mazda models
    for mazda_model in mazda_models:
        existing_model = False
        for api_model in api_models:
            if mazda_model['foreign_id'] == api_model['foreign_id']:
                existing_model = True
        # - Create new model
        if not existing_model:
            response = api_post(
                models_url, {
                    "name": mazda_model['name'],
                    "year": mazda_model['year'],
                    "foreign_id": mazda_model['foreign_id'],
                })
            if response.status_code == 200:
                logs.debug("API - Creating - " + mazda_model['name'] + " " +
                           mazda_model['year'])  # nopep8
            else:
                logs.error("API - Cannot create model - " +
                           mazda_model['name'] + " " +
                           mazda_model['year'])  # nopep8

    # Warn about the API models not available anymore
    for api_model in api_models:
        available_model = False
        for mazda_model in mazda_models:
            if api_model['foreign_id'] == mazda_model['foreign_id']:
                available_model = True
        if not available_model:
            logs.warning("Mazda - This model is no longer available - " +
                         api_model['name'] + " " + api_model['year'] + " (" +
                         api_model['foreign_id'] + ")")  # nopep8

    return logs
Ejemplo n.º 2
0
def run():
    logs = Logs()

    # API - Fetch the models
    models_url = "make/hyundai/models"
    logs.debug("API - Fetching models")
    response = api_get(models_url)
    if response.status_code != 200:
        logs.error("API - Cannot fetch models - Status code: " +
                   response.status_code)  # nopep8
        return logs
    api_json = json.loads(response.content)
    api_models = api_json['models']

    # Hyundai - Fetch the models
    response = requests.get(
        "https://www.hyundaicanada.com/fr/hacc/service/showroom/GetShowroomsModelJson?lang=fr"
    )  # nopep8
    if response.status_code != 200:
        logs.error("Hyundai - Cannot fetch models")
        return logs

    # Hyundai - Validate models
    hyundai_json = json.loads(response.content)
    if "models" not in hyundai_json:
        logs.error("Hyundai - No models node found in the JSON")
        return logs

    # Hyundai - Reformat models
    # - An array for each models containing a nested array for each years
    hyundai_models = []
    for models in hyundai_json['models']:
        for single_model in models:
            model = {
                # Name are uppercased
                "name":
                single_model['vehicleName_Fr'][0].upper() +
                single_model['vehicleName_Fr'][1:].lower(),  # nopep8
                "year":
                single_model['vehicleYear'],
                # Hyundai have { and } around the foreign_id
                # - [1:-1] to remove it
                "foreign_id":
                single_model['modelId'][1:-1],
            }
            hyundai_models.append(model)

    # Create new model
    for hyundai_model in hyundai_models:
        existing_model = False
        for api_model in api_models:
            if api_model['foreign_id'] == hyundai_model['foreign_id']:
                existing_model = True
        if not existing_model:
            response = api_post(
                models_url, {
                    "name": hyundai_model['name'],
                    "year": hyundai_model['year'],
                    "foreign_id": hyundai_model['foreign_id'],
                })
            if response.status_code == 200:
                logs.debug("API - Creating - " + hyundai_model['name'] + " " +
                           hyundai_model['year'])  # nopep8
            else:
                logs.error("API - Cannot create model - " +
                           hyundai_model['name'] + " " +
                           hyundai_model['year'])  # nopep8

    # Warn about the API models not available anymore
    for api_model in api_models:
        available_model = False
        for hyundai_model in hyundai_models:
            if api_model['foreign_id'] == hyundai_model['foreign_id']:
                available_model = True
        if not available_model:
            logs.warning("Hyundai - This model is no longer available - " +
                         api_model['name'] + " " + api_model['year'] + " (" +
                         api_model['foreign_id'] + ")")  # nopep8

    return logs
Ejemplo n.º 3
0
def run():
    logs = Logs()

    # API - Fetch the models
    models_url = "make/mazda/models"
    logs.debug("API - Fetching models")
    response = api_get(models_url)
    if response.status_code != 200:
        logs.error("API - Cannot fetch models - Status code: " + response.status_code)  # nopep8
        return logs
    api_json = json.loads(response.content)
    api_models = api_json['models']

    # API - Fetch the trims
    for model in api_models:
        logs.debug("API - Fetching trims of " + model['name'] + " " + model['year'])  # nopep8
        trims_url = "model/" + model['id'] + "/trims"
        response = api_get(trims_url)
        if response.status_code != 200:
            logs.error("API - Cannot fetch trims of " + model['slug'] + " - Status code: " + str(response.status_code))  # nopep8
            return logs
        response_json = json.loads(response.content)
        model['trims'] = response_json['trims']

    # Mazda - Fetch the models
    logs.debug("Mazda - Fetching models and trims")
    response = requests.get("https://www.mazda.ca/common/apps/assets/json/step1/qc.json?v=1547651328198")  # nopep8
    if response.status_code != 200:
        logs.error("Mazda - Cannot fetch models")
        return logs
    mazda_json = json.loads(response.content)

    # Mazda - Reformat models and trims
    mazda_models = []
    for single_category in mazda_json['categories']:
        for single_model in single_category['carline']:
            for single_year in single_model['modelYear']:
                model = {
                    "name": single_model['carline'][0].upper() + single_model['carline'][1:].lower(),  # nopep8
                    "year": str(single_year['year']),
                    "foreign_id": single_model['carline'] + " " + str(single_year['year']),  # nopep8
                    "trims": []
                }

                for single_trim in single_year['trim']:
                    trim = {
                        "name": html.unescape(single_trim['trim']) + " " + html.unescape(single_trim['transmission']['fr']).strip(),  # nopep8
                        "nice_name": "",
                        "foreign_id": single_trim['modelCode']
                    }
                    if single_trim['bodystyle']:
                        trim['name'] += " - " + html.unescape(single_trim['bodystyle']).strip()  # nopep8
                    model['trims'].append(trim)

                mazda_models.append(model)

    # Mazda - Validate models
    if len(mazda_models) == 0:
        logs.error("Mazda - No models founds")
        return logs

    # Validate each Mazda models
    for mazda_model in mazda_models:
        for api_model in api_models:
            if mazda_model['foreign_id'] == api_model['foreign_id']:
                # Validate each Mazda trims
                for mazda_trim in mazda_model['trims']:
                    existing_trim = False
                    for api_trim in api_model['trims']:
                        if api_trim['foreign_id'] == mazda_trim['foreign_id']:  # nopep8
                            existing_trim = True
                    if not existing_trim:
                        response = api_post("model/" + api_model['id'] + "/trims", {  # nopep8
                            "name": mazda_trim['name'],
                            "nice_name": mazda_trim['nice_name'],
                            "foreign_id": mazda_trim['foreign_id'],
                        })
                        if response.status_code == 200:
                            logs.debug("API - Creating - " + api_model['name'] + " " + api_model['year'] + " " + mazda_trim['name'])  # nopep8
                        else:
                            logs.error("API - Cannot create trim - " + api_model['name'] + " " + api_model['year'] + " " + mazda_trim['name'])  # nopep8

    # Warn about the API models not available anymore
    for api_model in api_models:
        # Warn about the API models not available anymore
        for api_trim in api_model['trims']:
            available_trim = False
            for mazda_model in mazda_models:
                for mazda_trim in mazda_model['trims']:
                    if api_trim['foreign_id'] == mazda_trim['foreign_id']:
                        available_trim = True
            if not available_trim:
                logs.warning("Mazda - This trim is no longer available - " + api_model['name'] + " " + api_model['year'] + " " + api_trim['nice_name'] + " (" + api_trim['foreign_id'] + ")")  # nopep8

    return logs
Ejemplo n.º 4
0
def run():
    logs = Logs()

    # API - Fetch the models
    logs.debug("API - Fetching models\n")
    response = api_get("make/hyundai/models")
    if response.status_code != 200:
        logs.error("API - Cannot fetch models - Status code: " +
                   str(response.status_code))  # nopep8
        return logs
    response_json = json.loads(response.content)
    api_models = response_json['models']

    for model in api_models:
        # API - Fetch the trims
        logs.debug("API - Fetching trims of " + model['name'] + " " +
                   model['year'])  # nopep8
        trims_url = "model/" + model['id'] + "/trims"
        response = api_get(trims_url)
        if response.status_code != 200:
            logs.error("API - Cannot fetch trims of " + model['slug'] +
                       " - Status code: " +
                       str(response.status_code))  # nopep8
            return logs
        response_json = json.loads(response.content)
        api_trims = response_json['trims']

        # Hyundai - Fetch the trims
        model_url = "https://www.hyundaicanada.com/fr/hacc/service/showroom/GetShowroomsModelTrimsJson?modelid={" + model[
            'foreign_id'] + "}&prov=QC&lang=fr"  # nopep8
        hyundai_trims = []
        logs.debug("Hyundai - Fetching trims of " + model['name'] + " " +
                   model['year'])  # nopep8
        response = requests.get(model_url)
        if response.status_code == 200:
            hyundai_json = json.loads(response.content)
            if "trims" in hyundai_json:
                for hyundai_trim in hyundai_json['trims']:
                    trim = {
                        "name": hyundai_trim['trimName'],
                        "nice_name": hyundai_trim['trimName_Fr'],
                        "foreign_id": hyundai_trim['trimId'],
                    }
                    hyundai_trims.append(trim)

        # Hyundai - Validate the trims
        if len(hyundai_trims) == 0:
            logs.warning("Hyundai - No trims found for " + model['name'] +
                         " " + model['year'] + " (" + model['foreign_id'] +
                         ")")  # nopep8

        # Hyundai - Create new trims
        for hyundai_trim in hyundai_trims:
            existing_trim = False
            for api_trim in api_trims:
                if api_trim['foreign_id'] == hyundai_trim['foreign_id']:
                    existing_trim = True
            if not existing_trim:
                response = api_post(
                    trims_url, {
                        "name": hyundai_trim['name'],
                        "nice_name": hyundai_trim['nice_name'],
                        "foreign_id": hyundai_trim['foreign_id'],
                    })
                if response.status_code == 200:
                    logs.debug("API - Creating - " + model['name'] + " " +
                               model['year'] + " " +
                               hyundai_trim['nice_name'])  # nopep8
                else:
                    logs.error("API - Cannot create trim - " + model['name'] +
                               " " + model['year'] + " " +
                               hyundai_model['nice_name'])  # nopep8

        # Warn about the API models not available anymore
        for api_trim in api_trims:
            available_trim = False
            for hyundai_trim in hyundai_trims:
                if api_trim['foreign_id'] == hyundai_trim['foreign_id']:
                    available_trim = True
            if not available_trim:
                logs.warning("Hyundai - This trim is no longer available - " +
                             model['name'] + " " + model['year'] + " " +
                             api_trim['nice_name'] + " (" +
                             api_trim['foreign_id'] + ")")  # nopep8

    return logs
Ejemplo n.º 5
0
def run():
    logs = Logs()

    # API - Fetch the trims
    logs.debug("API - Fetching trims\n")
    response = api_get("make/hyundai/trims")
    if response.status_code != 200:
        logs.error("API - Cannot fetch trims - Status code: " +
                   str(response.status_code))  # nopep8
        return logs
    response_json = json.loads(response.content)
    api_trims = response_json['trims']

    for api_trim in api_trims:
        trim_fullname = api_trim['model']['name'] + " " + api_trim['model'][
            'year'] + " " + api_trim['nice_name']  # nopep8
        logs.debug("API - Fetching prices - " + trim_fullname)  # nopep8

        response = requests.get(
            "https://www.hyundaicanada.com/fr/hacc/service/pricemaster/model/trim/alloptions?trimId="
            + api_trim['foreign_id'] + "&prov=QC&lang=fr")  # nopep8
        if response.status_code != 200:
            logs.warning("Hyundai - Cannot fetch price - " + trim_fullname)
            continue

        hyundai_prices = json.loads(response.content)

        # Get the rebates incentives for each price type per term
        rebates = {"finance": {}, "lease": {}, "cash": {}}
        for rebate in hyundai_prices['rebates']:
            if rebate['rebateSelected']:
                for rebateType in rebate['rebateTypes']:
                    for rebateOption in rebateType['rebateOptions']:
                        if rebateOption['term'] not in rebates[
                                rebateType['type'].lower()]:  # nopep8
                            rebates[rebateType['type'].lower()][
                                rebateOption['term']] = 0  # nopep8

                        # Add the rebate amount (WITHOUT TAXES !!!!!!!) Taxes : QC + CA = 1.14975    # nopep8
                        rebates[rebateType['type'].lower()][
                            rebateOption['term']] += (rebateOption['amount'] /
                                                      1.14975)  # nopep8

        # Get the taxes total
        taxes = 0
        for taxe in hyundai_prices['taxesOrLevies']:
            taxes += taxe['amount']

        # Prepare the JSON
        prices = {
            "msrp": format_price(hyundai_prices['msrp']),
            "delivery": format_price(hyundai_prices['delivery']),
            "taxes": format_price(taxes),
            "data": {
                "cash": {
                    "incentive": "0"
                },
                "finance": [],
                "lease": []
            }
        }

        # Get all purchase options
        for purchaseOption in hyundai_prices['purchaseOptions']:
            price_type = purchaseOption['type'].lower()
            if price_type == "finance" or price_type == "lease" or price_type == "cash":  # nopep8
                for option in purchaseOption['options']:
                    term = option['term']
                    if price_type == "finance":
                        term = 0

                    # Apply the incentive for this term
                    if term in rebates[price_type]:
                        if rebates[price_type][term] > 0:
                            option['incentive'] = rebates[price_type][
                                term]  # nopep8

                    if price_type == "cash":
                        prices['data']['cash']['incentive'] = format_price(
                            option['incentive'])  # nopep8
                    else:
                        price = {
                            "incentive": format_price(option['incentive']),
                            "term": format_price(option['term']),
                            "rate": format_price(option['rate']),
                        }

                        # Lease needs a residual amount
                        if price_type == "lease":
                            price["residual"] = format_price(
                                option['residualAmount16k'] /
                                hyundai_prices['msrp'])  # nopep8

                        prices['data'][price_type].append(price)

        # Push the data
        md5 = create_md5(json.dumps(prices))

        price_url = "trim/" + api_trim['id'] + "/prices"
        logs.debug("API - Pushing price - " + trim_fullname)  # nopep8
        response = api_post(
            price_url, {
                "msrp": prices['msrp'],
                "taxes": prices['taxes'],
                "delivery": prices['delivery'],
                "hash": create_md5(json.dumps(prices)),
                "data": json.dumps(prices['data'])
            })
        if response.status_code != 200:
            logs.error("API - Cannot push price - Status code: " +
                       str(response.status_code))  # nopep8
            return logs
        else:
            api_json = json.loads(response.content)
            logs.debug("API - Response: " + api_json['message'])

    return logs