def formato_obj(json, carrier, code, retries):
    stats = []
    stats.append(str(u'\U0001F4EE') + ' <b>' + json['tracking_number'] + '</b>')
    try:
        tabela = json['origin_info']['trackinfo']
    except KeyError:
        if retries > 0:
            return get(code, retries-1)
        else:
            return status.NOT_FOUND_TM
    for evento in reversed(tabela):
        codigo_novo = None
        print(code)
        print(evento['Date'])
        # try:
        #     data = datetime.strptime(evento['Date'], '%Y-%m-%d %H:%M:%S').strftime("%d/%m/%Y %H:%M")
        # except ValueError:
        #     data = datetime.strptime(evento['Date'], '%Y-%m-%d %H:%M').strftime("%d/%m/%Y %H:%M")
        data = evento['Date']
        situacao = evento['StatusDescription']
        observacao = evento['checkpoint_status']
        try:
            codigo_novo = geartrack.getcorreioscode(carrier['code'], code)
            if codigo_novo:
                carrier = {'code': 'correios', 'name': 'Correios'}
                db.update_package(code, carrier=carrier, code_br=codigo_novo)
                return correios.get(codigo_novo, 3)
        except:
            pass
        mensagem = ('Data: {}'
            '\nSituacao: <b>{}</b>'
            '\nObservação: {}'
        ).format(data, situacao, observacao)
        stats.append(mensagem)
    return stats
Exemple #2
0
def check_update(code, max_retries=3):
    api_type = check_type(code)
    if api_type is trackingmore:
        return trackingmore.get(code, max_retries)
    elif api_type is correios:
        return correios.get(code, max_retries)
    else:
        return status.TYPO
def get(code, retries=0):
    try:
        carriers = get_carriers(code)
    except IndexError:
        return status.TYPO
    except trackingmore.trackingmore.TrackingMoreAPIException as e:
        return status.NOT_FOUND_TM

    response_status = status.NOT_FOUND_TM
    for carrier in carriers:
        try:
            if carrier['code'] == 'correios':
                codigo_novo = db.search_package(code)["code_br"]
                return correios.get(codigo_novo, 3)
        except TypeError:
            pass
        try:
            tracking_data = get_or_create_tracking_item(carrier['code'], code)
            print(tracking_data)
        except trackingmore.trackingmore.TrackingMoreAPIException as e:
            if e.err_code == 4019 or e.err_code == 4021:
                response_status = status.OFFLINE
            elif e.err_code == 4031:
                response_status = status.NOT_FOUND_TM
        else:
            if not tracking_data or 'status' not in tracking_data:
                response_status = status.OFFLINE
            elif tracking_data['status'] == 'notfound':
                response_status = status.NOT_FOUND_TM
                print(tracking_data)
            #elif len(tracking_data) >= 10:
            elif tracking_data['status'] == 'transit':
                db.update_package(code, carrier=carrier)
                return formato_obj(tracking_data, carrier, code, retries)
            elif tracking_data['status'] == 'expired':
                db.update_package(code, carrier=carrier)
                return formato_obj(tracking_data, carrier, code, retries)
            elif tracking_data['status'] == 'delivered':
                db.update_package(code, carrier=carrier)
                return formato_obj(tracking_data, carrier, code, retries)
            elif tracking_data['status'] == 'pickup':
                db.update_package(code, carrier=carrier)
                return formato_obj(tracking_data, carrier, code, retries)

    return response_status