From 8c26d81ef2deca63e3f654bc21d60d15b9f6628b Mon Sep 17 00:00:00 2001 From: Gabriel RF Date: Fri, 4 Oct 2019 21:01:42 +0000 Subject: [PATCH 1/2] Fixed excessive carriers check --- apitrackingmore.py | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/apitrackingmore.py b/apitrackingmore.py index 3184f70..ff070c1 100644 --- a/apitrackingmore.py +++ b/apitrackingmore.py @@ -3,10 +3,11 @@ import trackingmore import sys from datetime import datetime +from pymongo import ASCENDING, MongoClient import apigeartrack as geartrack -# https://www.trackingmore.com/api-index.html +# https://www.trackingmore.com/api-index.html - Codigos de retorno da API config = configparser.ConfigParser() config.sections() config.read('bot.conf') @@ -14,14 +15,26 @@ key = config['TRACKINGMORE']['key'] trackingmore.set_api_key(key) +client = MongoClient() +db = client.rastreiobot + +def set_carrier_db(code, carrier): + db.rastreiobot.update_one({ + "code": code.upper()}, { + "$set": { + "carrier": carrier + } + }) + def get_or_create_tracking_item(carrier, code): + try: - tracking_data = trackingmore.create_tracking_data(carrier, code) - trackingmore.create_tracking_item(tracking_data) tracking_data = trackingmore.get_tracking_item(carrier, code) except trackingmore.trackingmore.TrackingMoreAPIException as e: - if e.err_code == 4016: # Already exists + if e.err_code == 4031: + tracking_data = trackingmore.create_tracking_data(carrier, code) + trackingmore.create_tracking_item(tracking_data) tracking_data = trackingmore.get_tracking_item(carrier, code) else: raise e @@ -30,8 +43,16 @@ def get_or_create_tracking_item(carrier, code): def get_carriers(code): - carriers = trackingmore.detect_carrier_from_code(code) - carriers.sort(key=lambda carrier: carrier['code']) + carriers = [] + cursor = db.rastreiobot.find_one({ + "code": code + }) + try: + carriers.append(cursor['carrier']) + return(carriers) + except: + carriers = trackingmore.detect_carrier_from_code(code) + carriers.sort(key=lambda carrier: carrier['code']) return carriers @@ -51,12 +72,12 @@ def get(code, *args, **kwargs): elif e.err_code == 4031: response_status = status.NOT_FOUND_TM else: - print(carrier, tracking_data) 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 elif len(tracking_data) >= 10: + set_carrier_db(code, carrier) return formato_obj(tracking_data, carrier, code) return response_status @@ -90,3 +111,4 @@ def formato_obj(json, carrier, code): if __name__ == '__main__': print(get(sys.argv[1], 0)) #get(sys.argv[1], 0) + #print(get_or_set_carrier_db(sys.argv[1])) From a80c0655389a0ae5bcc2d49da5eebd033a6abc58 Mon Sep 17 00:00:00 2001 From: Gabriel RF Date: Fri, 4 Oct 2019 21:40:51 +0000 Subject: [PATCH 2/2] Minor fixes --- apitrackingmore.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apitrackingmore.py b/apitrackingmore.py index ff070c1..316754b 100644 --- a/apitrackingmore.py +++ b/apitrackingmore.py @@ -49,8 +49,8 @@ def get_carriers(code): }) try: carriers.append(cursor['carrier']) - return(carriers) - except: + return carriers + except KeyError: carriers = trackingmore.detect_carrier_from_code(code) carriers.sort(key=lambda carrier: carrier['code']) return carriers