Skip to content

Commit

Permalink
Merge pull request #50 from GabrielRF/trackingmore
Browse files Browse the repository at this point in the history
Fixed excessive carriers check
  • Loading branch information
rougeth authored Oct 5, 2019
2 parents e292429 + a80c065 commit c120f5c
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions apitrackingmore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,38 @@
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')

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
Expand All @@ -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 KeyError:
carriers = trackingmore.detect_carrier_from_code(code)
carriers.sort(key=lambda carrier: carrier['code'])
return carriers


Expand All @@ -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
Expand Down Expand Up @@ -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]))

0 comments on commit c120f5c

Please sign in to comment.