Skip to content

Commit

Permalink
Merge pull request #56 from GabrielRF/tmp
Browse files Browse the repository at this point in the history
Rollback of PR #51
  • Loading branch information
rougeth authored Oct 7, 2019
2 parents 9f9197b + becc940 commit 23dd454
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 24 deletions.
File renamed without changes.
File renamed without changes.
40 changes: 23 additions & 17 deletions carriers/trackingmore.py → apitrackingmore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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') + ' <b>' + json['tracking_number'] + '</b>')
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:
Expand All @@ -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 = '<code>' + geartrack.getcorreioscode(carrier, code) + '</code>'
except:
pass
try:
observacao = 'Código novo: <code>' + geartrack.getcorreioscode(carrier, code) + '</code>'
except:
pass
mensagem = ('Data: {}' +
'\nSituacao: <b>{}</b>' +
'\nObservação: {}'
Expand All @@ -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]))
Empty file removed carriers/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions check_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions misc.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 3 additions & 3 deletions rastreiobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -275,7 +275,7 @@ def cmd_pacotes(message):
else:
message = '<b>Clique para ver o histórico:</b>\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',
Expand Down Expand Up @@ -323,7 +323,7 @@ def cmd_concluidos(message):
else:
message = '<b>Pacotes concluídos nos últimos 30 dias:</b>\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',
Expand Down

0 comments on commit 23dd454

Please sign in to comment.