diff --git a/carriers/correios.py b/apicorreios.py
similarity index 100%
rename from carriers/correios.py
rename to apicorreios.py
diff --git a/carriers/geartrack.py b/apigeartrack.py
similarity index 100%
rename from carriers/geartrack.py
rename to apigeartrack.py
diff --git a/carriers/trackingmore.py b/apitrackingmore.py
similarity index 78%
rename from carriers/trackingmore.py
rename to apitrackingmore.py
index c321562..844204e 100644
--- a/carriers/trackingmore.py
+++ b/apitrackingmore.py
@@ -5,7 +5,7 @@
from datetime import datetime
from pymongo import ASCENDING, MongoClient
-from carriers import geartrack
+import apigeartrack as geartrack
# https://www.trackingmore.com/api-index.html - Codigos de retorno da API
config = configparser.ConfigParser()
@@ -32,7 +32,7 @@ def get_or_create_tracking_item(carrier, code):
try:
tracking_data = trackingmore.get_tracking_item(carrier, code)
except trackingmore.trackingmore.TrackingMoreAPIException as e:
- if e.err_code == 4031:
+ if e.err_code == 4031 or e.err_code == 4017:
tracking_data = trackingmore.create_tracking_data(carrier, code)
trackingmore.create_tracking_item(tracking_data)
tracking_data = trackingmore.get_tracking_item(carrier, code)
@@ -43,20 +43,21 @@ def get_or_create_tracking_item(carrier, code):
def get_carriers(code):
- carriers = []
cursor = db.rastreiobot.find_one({
"code": code
})
try:
- carriers.append(cursor['carrier'])
- return carriers
- except KeyError:
+ if type(cursor['carrier']) is dict:
+ return [cursor['carrier']]
+ return cursor['carrier']
+ except:
carriers = trackingmore.detect_carrier_from_code(code)
carriers.sort(key=lambda carrier: carrier['code'])
+ set_carrier_db(code, carriers)
return carriers
-def get(code, *args, **kwargs):
+def get(code, retries=0):
try:
carriers = get_carriers(code)
except trackingmore.trackingmore.TrackingMoreAPIException as e:
@@ -78,15 +79,21 @@ def get(code, *args, **kwargs):
response_status = status.NOT_FOUND_TM
elif len(tracking_data) >= 10:
set_carrier_db(code, carrier)
- return formato_obj(tracking_data, carrier, code)
+ return formato_obj(tracking_data, carrier, code, retries)
return response_status
-def formato_obj(json, carrier, code):
+def formato_obj(json, carrier, code, retries):
stats = []
stats.append(str(u'\U0001F4EE') + ' ' + json['tracking_number'] + '')
- tabela = json['origin_info']['trackinfo']
+ try:
+ tabela = json['origin_info']['trackinfo']
+ except KeyError:
+ if retries < 0:
+ return get(sys.argv[1], retries-1)
+ else:
+ return status.NOT_FOUND_TM
mensagem = ''
for evento in reversed(tabela):
try:
@@ -95,11 +102,10 @@ def formato_obj(json, carrier, code):
data = datetime.strptime(evento['Date'], '%Y-%m-%d %H:%M').strftime("%d/%m/%Y %H:%M")
situacao = evento['StatusDescription']
observacao = evento['checkpoint_status']
- if 'Import clearance success' in situacao:
- try:
- observacao = '' + geartrack.getcorreioscode(carrier, code) + '
'
- except:
- pass
+ try:
+ observacao = 'Código novo: ' + geartrack.getcorreioscode(carrier, code) + '
'
+ except:
+ pass
mensagem = ('Data: {}' +
'\nSituacao: {}' +
'\nObservação: {}'
@@ -109,6 +115,6 @@ def formato_obj(json, carrier, code):
if __name__ == '__main__':
- print(get(sys.argv[1], 0))
+ print(get(sys.argv[1], retries=3))
#get(sys.argv[1], 0)
- #print(get_or_set_carrier_db(sys.argv[1]))
+ #print(get_carriers(sys.argv[1]))
diff --git a/carriers/__init__.py b/carriers/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/check_update.py b/check_update.py
index 650b7e5..0c69f2f 100644
--- a/check_update.py
+++ b/check_update.py
@@ -2,8 +2,8 @@
from datetime import date
import status
from misc import check_type
-from carreirs import correios
-from carreirs import trackingmore
+import apicorreios as correios
+import apitrackingmore as trackingmore
def check_update(code, max_retries=3):
# print('check_update')
diff --git a/misc.py b/misc.py
index 28e020d..69e41b0 100644
--- a/misc.py
+++ b/misc.py
@@ -1,6 +1,6 @@
import re
-from carreirs import apicorreios as correios
-from carreirs import apitrackingmore as trackingmore
+import apicorreios as correios
+import apitrackingmore as trackingmore
from pymongo import MongoClient
from telebot import types
diff --git a/rastreiobot.py b/rastreiobot.py
index ab499f1..0fc2565 100644
--- a/rastreiobot.py
+++ b/rastreiobot.py
@@ -10,7 +10,7 @@
import sentry_sdk
import status
import telebot
-from carreirs import correios
+import apicorreios as correios
from check_update import check_update
from math import ceil
from misc import check_type, send_clean_msg, check_package
@@ -275,7 +275,7 @@ def cmd_pacotes(message):
else:
message = 'Clique para ver o histórico:\n' + message
msg_split = message.split('\n')
- for elem in range(0, len(msg_split), 10):
+ for elem in range(0, len(msg_split)-1, 10):
s = '\n'
bot.send_message(chatid,
s.join(msg_split[elem:elem+10]), parse_mode='HTML',
@@ -323,7 +323,7 @@ def cmd_concluidos(message):
else:
message = 'Pacotes concluídos nos últimos 30 dias:\n' + message
msg_split = message.split('\n')
- for elem in range(0, len(msg_split), 10):
+ for elem in range(0, len(msg_split)-1, 10):
s = '\n'
bot.send_message(chatid,
s.join(msg_split[elem:elem+10]), parse_mode='HTML',