From 74b1a169c8fb403593129b11f72ed057147af29d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dtalo=20Epif=C3=A2nio?= Date: Tue, 1 Oct 2019 19:41:12 -0300 Subject: [PATCH 01/21] =?UTF-8?q?Adiciona=20configura=C3=A7=C3=A3o=20de=20?= =?UTF-8?q?travis,=20pytest=20no=20requirements.txt=20e=20teste=20inicial?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 9 +++++++++ requirements.txt | 15 ++++++++++++++- tests/test_helloworld.py | 4 ++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 .travis.yml create mode 100644 tests/test_helloworld.py diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ac02dd2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: python +python: + - "3.5" + - "3.6" + - "3.7" +install: + - pip install -r requirements.txt +script: + - pytest \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index aa47e27..9d9459a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,32 +1,45 @@ appdirs==1.4.0 +atomicwrites==1.3.0 +attrs==19.2.0 backports-abc==0.5 beautifulsoup4==4.5.3 bokeh==0.12.4 bs4==0.0.1 +certifi==2019.9.11 +chardet==3.0.4 decorator==4.0.11 +idna==2.7 +importlib-metadata==0.23 ipdb==0.10.2 ipython==5.2.1 ipython-genutils==0.1.0 Jinja2==2.9.5 MarkupSafe==0.23 +more-itertools==7.2.0 numpy==1.12.0 packaging==16.8 pandas==0.19.2 +pathlib2==2.3.5 pexpect==4.2.1 pickleshare==0.7.4 +pluggy==0.13.0 prompt-toolkit==1.0.13 ptyprocess==0.5.1 +py==1.8.0 Pygments==2.2.0 pymongo==3.4.0 pyparsing==2.1.10 pyTelegramBotAPI==2.3.0 +pytest==5.2.0 python-dateutil==2.6.0 pytz==2016.10 PyYAML==3.13 requests==2.20.0 +sentry-sdk==0.7.14 simplegeneric==0.8.1 six==1.10.0 tornado==4.4.2 traitlets==4.3.1 +urllib3==1.24.3 wcwidth==0.1.7 -sentry-sdk==0.7.14 +zipp==0.6.0 diff --git a/tests/test_helloworld.py b/tests/test_helloworld.py new file mode 100644 index 0000000..ea565ec --- /dev/null +++ b/tests/test_helloworld.py @@ -0,0 +1,4 @@ +import pytest + +def test_dummy(): + assert True \ No newline at end of file From bf2e4e1502fee4ffcbecddd5fd7516dbbf52d440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dtalo=20Epif=C3=A2nio?= Date: Tue, 1 Oct 2019 20:16:07 -0300 Subject: [PATCH 02/21] Adiciona nova linha no fim de tests/test_helloworld.py e em .travis.yml. Adiciona apenas o pytest no requirements. Modifica travis para testar apenas python 3.7 --- .travis.yml | 4 +--- requirements.txt | 16 ++-------------- tests/test_helloworld.py | 2 +- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index ac02dd2..9b6f0c9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,7 @@ language: python python: - - "3.5" - - "3.6" - "3.7" install: - pip install -r requirements.txt script: - - pytest \ No newline at end of file + - pytest diff --git a/requirements.txt b/requirements.txt index 9d9459a..4170fa4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,45 +1,33 @@ appdirs==1.4.0 -atomicwrites==1.3.0 -attrs==19.2.0 backports-abc==0.5 beautifulsoup4==4.5.3 bokeh==0.12.4 bs4==0.0.1 -certifi==2019.9.11 -chardet==3.0.4 decorator==4.0.11 -idna==2.7 -importlib-metadata==0.23 ipdb==0.10.2 ipython==5.2.1 ipython-genutils==0.1.0 Jinja2==2.9.5 MarkupSafe==0.23 -more-itertools==7.2.0 numpy==1.12.0 packaging==16.8 pandas==0.19.2 -pathlib2==2.3.5 pexpect==4.2.1 pickleshare==0.7.4 -pluggy==0.13.0 prompt-toolkit==1.0.13 ptyprocess==0.5.1 -py==1.8.0 Pygments==2.2.0 pymongo==3.4.0 pyparsing==2.1.10 pyTelegramBotAPI==2.3.0 -pytest==5.2.0 python-dateutil==2.6.0 pytz==2016.10 PyYAML==3.13 requests==2.20.0 -sentry-sdk==0.7.14 simplegeneric==0.8.1 six==1.10.0 tornado==4.4.2 traitlets==4.3.1 -urllib3==1.24.3 wcwidth==0.1.7 -zipp==0.6.0 +sentry-sdk==0.7.14 +pytest==5.2.0 diff --git a/tests/test_helloworld.py b/tests/test_helloworld.py index ea565ec..a0bf814 100644 --- a/tests/test_helloworld.py +++ b/tests/test_helloworld.py @@ -1,4 +1,4 @@ import pytest def test_dummy(): - assert True \ No newline at end of file + assert True From 5ff8ddec2ac09b5d0519ffd11639b793d2be80f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dtalo=20Epif=C3=A2nio?= Date: Tue, 1 Oct 2019 20:21:02 -0300 Subject: [PATCH 03/21] Adiciona nova linha --- .travis.yml | 1 + tests/test_helloworld.py | 1 + 2 files changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 9b6f0c9..fca3eff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,3 +5,4 @@ install: - pip install -r requirements.txt script: - pytest + diff --git a/tests/test_helloworld.py b/tests/test_helloworld.py index a0bf814..5aa475c 100644 --- a/tests/test_helloworld.py +++ b/tests/test_helloworld.py @@ -2,3 +2,4 @@ def test_dummy(): assert True + From 3d87b26c29bb39c0c9976bd4719d821b319c5261 Mon Sep 17 00:00:00 2001 From: Gabriel RF Date: Fri, 4 Oct 2019 15:48:59 +0000 Subject: [PATCH 04/21] Fix bug when sunyou used as carrier --- apitrackingmore.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apitrackingmore.py b/apitrackingmore.py index 98af956..3184f70 100644 --- a/apitrackingmore.py +++ b/apitrackingmore.py @@ -68,7 +68,10 @@ def formato_obj(json, carrier, code): tabela = json['origin_info']['trackinfo'] mensagem = '' for evento in reversed(tabela): - data = datetime.strptime(evento['Date'], '%Y-%m-%d %H:%M:%S').strftime("%d/%m/%Y %H:%M") + 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") situacao = evento['StatusDescription'] observacao = evento['checkpoint_status'] if 'Import clearance success' in situacao: From 8c26d81ef2deca63e3f654bc21d60d15b9f6628b Mon Sep 17 00:00:00 2001 From: Gabriel RF Date: Fri, 4 Oct 2019 21:01:42 +0000 Subject: [PATCH 05/21] 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 06/21] 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 From 7ac8da2584522d53180fd526a143ad75751343eb Mon Sep 17 00:00:00 2001 From: Marco Rougeth Date: Sun, 6 Oct 2019 15:12:05 +0000 Subject: [PATCH 07/21] Config Python 3.5 build on Travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index fca3eff..d9b31de 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: python python: + - "3.5" - "3.7" install: - pip install -r requirements.txt From 3dd20100da99d7fe9c2fc7101be25063ad9a3547 Mon Sep 17 00:00:00 2001 From: Marco Rougeth Date: Sun, 6 Oct 2019 16:09:56 +0100 Subject: [PATCH 08/21] Move carreirs API files to carriers module --- carriers/__init__.py | 0 apicorreios.py => carriers/correios.py | 0 apigeartrack.py => carriers/geartrack.py | 0 apitrackingmore.py => carriers/trackingmore.py | 2 +- check_update.py | 4 ++-- misc.py | 5 +++-- rastreiobot.py | 2 +- 7 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 carriers/__init__.py rename apicorreios.py => carriers/correios.py (100%) rename apigeartrack.py => carriers/geartrack.py (100%) rename apitrackingmore.py => carriers/trackingmore.py (99%) diff --git a/carriers/__init__.py b/carriers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apicorreios.py b/carriers/correios.py similarity index 100% rename from apicorreios.py rename to carriers/correios.py diff --git a/apigeartrack.py b/carriers/geartrack.py similarity index 100% rename from apigeartrack.py rename to carriers/geartrack.py diff --git a/apitrackingmore.py b/carriers/trackingmore.py similarity index 99% rename from apitrackingmore.py rename to carriers/trackingmore.py index 316754b..c321562 100644 --- a/apitrackingmore.py +++ b/carriers/trackingmore.py @@ -5,7 +5,7 @@ from datetime import datetime from pymongo import ASCENDING, MongoClient -import apigeartrack as geartrack +from carriers import geartrack # https://www.trackingmore.com/api-index.html - Codigos de retorno da API config = configparser.ConfigParser() diff --git a/check_update.py b/check_update.py index 0c69f2f..650b7e5 100644 --- a/check_update.py +++ b/check_update.py @@ -2,8 +2,8 @@ from datetime import date import status from misc import check_type -import apicorreios as correios -import apitrackingmore as trackingmore +from carreirs import correios +from carreirs import trackingmore def check_update(code, max_retries=3): # print('check_update') diff --git a/misc.py b/misc.py index 8625e9a..28e020d 100644 --- a/misc.py +++ b/misc.py @@ -1,9 +1,10 @@ import re -import apicorreios as correios -import apitrackingmore as trackingmore +from carreirs import apicorreios as correios +from carreirs import apitrackingmore as trackingmore from pymongo import MongoClient from telebot import types + client = MongoClient() db = client.rastreiobot diff --git a/rastreiobot.py b/rastreiobot.py index 7da0a40..ab499f1 100644 --- a/rastreiobot.py +++ b/rastreiobot.py @@ -10,7 +10,7 @@ import sentry_sdk import status import telebot -import apicorreios as correios +from carreirs import correios from check_update import check_update from math import ceil from misc import check_type, send_clean_msg, check_package From e3fcd6f9378959de39a0d9aeb7686f5d275fdb05 Mon Sep 17 00:00:00 2001 From: Gabriel RF Date: Sun, 6 Oct 2019 14:45:04 +0000 Subject: [PATCH 09/21] Removed retrial if detect carrier fails --- carriers/trackingmore.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/carriers/trackingmore.py b/carriers/trackingmore.py index c321562..f7e51e5 100644 --- a/carriers/trackingmore.py +++ b/carriers/trackingmore.py @@ -50,7 +50,7 @@ def get_carriers(code): try: carriers.append(cursor['carrier']) return carriers - except KeyError: + except: carriers = trackingmore.detect_carrier_from_code(code) carriers.sort(key=lambda carrier: carrier['code']) return carriers @@ -60,7 +60,7 @@ def get(code, *args, **kwargs): try: carriers = get_carriers(code) except trackingmore.trackingmore.TrackingMoreAPIException as e: - return status.NOT_FOUND_TM + return status.TYPO response_status = status.NOT_FOUND for carrier in carriers: @@ -70,12 +70,12 @@ def get(code, *args, **kwargs): 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 + response_status = status.TYPO 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 + response_status = status.TYPO elif len(tracking_data) >= 10: set_carrier_db(code, carrier) return formato_obj(tracking_data, carrier, code) From f2941efa9bbe5638a4fc02b476e69712ec06276f Mon Sep 17 00:00:00 2001 From: Gabriel RF Date: Sun, 6 Oct 2019 18:22:01 +0000 Subject: [PATCH 10/21] Fix trackingmore waiting package. Thanks to @jtemporal --- carriers/trackingmore.py | 13 +++++++------ rastreiobot.py | 3 ++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/carriers/trackingmore.py b/carriers/trackingmore.py index f7e51e5..b6aa8c3 100644 --- a/carriers/trackingmore.py +++ b/carriers/trackingmore.py @@ -43,16 +43,17 @@ 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 + 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 @@ -60,7 +61,7 @@ def get(code, *args, **kwargs): try: carriers = get_carriers(code) except trackingmore.trackingmore.TrackingMoreAPIException as e: - return status.TYPO + return status.NOT_FOUND_TM response_status = status.NOT_FOUND for carrier in carriers: @@ -70,12 +71,12 @@ def get(code, *args, **kwargs): if e.err_code == 4019 or e.err_code == 4021: response_status = status.OFFLINE elif e.err_code == 4031: - response_status = status.TYPO + 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.TYPO + response_status = status.NOT_FOUND_TM elif len(tracking_data) >= 10: set_carrier_db(code, carrier) return formato_obj(tracking_data, carrier, code) diff --git a/rastreiobot.py b/rastreiobot.py index ab499f1..b5b376a 100644 --- a/rastreiobot.py +++ b/rastreiobot.py @@ -275,7 +275,8 @@ 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): + print(len(msg_split)) + 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', From efbccb926ce85934dc385d409e4dab7c5f316564 Mon Sep 17 00:00:00 2001 From: Gabriel RF Date: Mon, 7 Oct 2019 13:05:08 +0000 Subject: [PATCH 11/21] Added retries --- carriers/correios.py => apicorreios.py | 0 .../trackingmore.py => apitrackingmore.py | 27 ++++++---- carriers/__init__.py | 0 carriers/geartrack.py | 50 ------------------- check_update.py | 4 +- misc.py | 4 +- rastreiobot.py | 4 +- 7 files changed, 22 insertions(+), 67 deletions(-) rename carriers/correios.py => apicorreios.py (100%) rename carriers/trackingmore.py => apitrackingmore.py (84%) delete mode 100644 carriers/__init__.py delete mode 100644 carriers/geartrack.py 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/trackingmore.py b/apitrackingmore.py similarity index 84% rename from carriers/trackingmore.py rename to apitrackingmore.py index b6aa8c3..2fe8139 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() @@ -57,7 +57,7 @@ def get_carriers(code): return carriers -def get(code, *args, **kwargs): +def get(code, retries=0): try: carriers = get_carriers(code) except trackingmore.trackingmore.TrackingMoreAPIException as e: @@ -79,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: @@ -96,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: {}' @@ -110,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])) diff --git a/carriers/__init__.py b/carriers/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/carriers/geartrack.py b/carriers/geartrack.py deleted file mode 100644 index 4e2035b..0000000 --- a/carriers/geartrack.py +++ /dev/null @@ -1,50 +0,0 @@ -import configparser -import json -import requests -import status -import trackingmore -import sys - -config = configparser.ConfigParser() -config.sections() -config.read('bot.conf') - -def getcorreioscode(carrier, code): - url = ('https://geartrack.pt/api/{}?id={}'.format(carrier, code)) - r = requests.get(url) - conteudo = str(r.content.decode('UTF-8')) - a = json.loads(conteudo) - if a['destinyId']: - return(a['destinyId']) - - -def getstatus(code, retries): - carrier = 'cainiao' - code = 'LP00139186175797' - url = ('https://geartrack.pt/api/{}?id={}'.format(carrier, code)) - r = requests.get(url) - conteudo = str(r.content.decode('UTF-8')) - a = json.loads(conteudo) - #print(a) - print(formato_obj(a)) - -def formato_obj(json): - stats = [] - stats.append(str(u'\U0001F4EE') + ' ' + json['id'] + '') - tabela = json['states'] - mensagem = '' - for evento in reversed(tabela): - data = evento['date'] - situacao = evento['state'] - observacao = '' - mensagem = ('Data: {}' + - '\nSituacao: {}' - ).format(data, situacao) - stats.append(mensagem) - if json['destinyId']: - stats.append('Pacote recebido nos Correios.\nCódigo: /' + json['destinyId']) - return stats - - -if __name__ == '__main__': - getstatus(sys.argv[1], 0) 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 b5b376a..c698bd0 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 @@ -324,7 +324,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', From d4131ed49f9faaf58f0756d8f667603a4f9cf0ee Mon Sep 17 00:00:00 2001 From: Gabriel RF Date: Mon, 7 Oct 2019 13:14:10 +0000 Subject: [PATCH 12/21] Tracking more bug fix --- apigeartrack.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ apitrackingmore.py | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 apigeartrack.py diff --git a/apigeartrack.py b/apigeartrack.py new file mode 100644 index 0000000..4e2035b --- /dev/null +++ b/apigeartrack.py @@ -0,0 +1,50 @@ +import configparser +import json +import requests +import status +import trackingmore +import sys + +config = configparser.ConfigParser() +config.sections() +config.read('bot.conf') + +def getcorreioscode(carrier, code): + url = ('https://geartrack.pt/api/{}?id={}'.format(carrier, code)) + r = requests.get(url) + conteudo = str(r.content.decode('UTF-8')) + a = json.loads(conteudo) + if a['destinyId']: + return(a['destinyId']) + + +def getstatus(code, retries): + carrier = 'cainiao' + code = 'LP00139186175797' + url = ('https://geartrack.pt/api/{}?id={}'.format(carrier, code)) + r = requests.get(url) + conteudo = str(r.content.decode('UTF-8')) + a = json.loads(conteudo) + #print(a) + print(formato_obj(a)) + +def formato_obj(json): + stats = [] + stats.append(str(u'\U0001F4EE') + ' ' + json['id'] + '') + tabela = json['states'] + mensagem = '' + for evento in reversed(tabela): + data = evento['date'] + situacao = evento['state'] + observacao = '' + mensagem = ('Data: {}' + + '\nSituacao: {}' + ).format(data, situacao) + stats.append(mensagem) + if json['destinyId']: + stats.append('Pacote recebido nos Correios.\nCódigo: /' + json['destinyId']) + return stats + + +if __name__ == '__main__': + getstatus(sys.argv[1], 0) diff --git a/apitrackingmore.py b/apitrackingmore.py index 2fe8139..3fe9b6f 100644 --- a/apitrackingmore.py +++ b/apitrackingmore.py @@ -50,7 +50,7 @@ def get_carriers(code): if type(cursor['carrier']) is dict: return [cursor['carrier']] return cursor['carrier'] - except: + except KeyError: carriers = trackingmore.detect_carrier_from_code(code) carriers.sort(key=lambda carrier: carrier['code']) set_carrier_db(code, carriers) From 0e84d7a10e076c143265f9304d9661cba3cc0cde Mon Sep 17 00:00:00 2001 From: Marco Rougeth Date: Mon, 7 Oct 2019 15:10:47 +0100 Subject: [PATCH 13/21] Remove Numpy and Pandas from requirements.txt --- requirements.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index dab4e04..228d653 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,9 +9,7 @@ ipython==5.2.1 ipython-genutils==0.1.0 Jinja2==2.9.5 MarkupSafe==0.23 -numpy==1.12.0 packaging==16.8 -pandas==0.19.2 pexpect==4.2.1 pickleshare==0.7.4 prompt-toolkit==1.0.13 From ec921344533545482ab1ce0919ecadf2fcc1aa44 Mon Sep 17 00:00:00 2001 From: Gabriel RF Date: Mon, 7 Oct 2019 16:24:19 +0000 Subject: [PATCH 14/21] Exception fix --- apitrackingmore.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apitrackingmore.py b/apitrackingmore.py index 3fe9b6f..844204e 100644 --- a/apitrackingmore.py +++ b/apitrackingmore.py @@ -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) @@ -50,7 +50,7 @@ def get_carriers(code): if type(cursor['carrier']) is dict: return [cursor['carrier']] return cursor['carrier'] - except KeyError: + except: carriers = trackingmore.detect_carrier_from_code(code) carriers.sort(key=lambda carrier: carrier['code']) set_carrier_db(code, carriers) @@ -117,4 +117,4 @@ def formato_obj(json, carrier, code, retries): if __name__ == '__main__': 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])) From becc9404f06e113e7a993932947326eeaab1f5de Mon Sep 17 00:00:00 2001 From: Gabriel RF Date: Mon, 7 Oct 2019 16:29:27 +0000 Subject: [PATCH 15/21] Removed print --- rastreiobot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/rastreiobot.py b/rastreiobot.py index c698bd0..0fc2565 100644 --- a/rastreiobot.py +++ b/rastreiobot.py @@ -275,7 +275,6 @@ def cmd_pacotes(message): else: message = 'Clique para ver o histórico:\n' + message msg_split = message.split('\n') - print(len(msg_split)) for elem in range(0, len(msg_split)-1, 10): s = '\n' bot.send_message(chatid, From 0f5e8974169a2492ef253a94950e0e75e041d8cb Mon Sep 17 00:00:00 2001 From: Marco Rougeth Date: Mon, 7 Oct 2019 21:47:47 +0000 Subject: [PATCH 16/21] Update apitrackingmore.py --- apitrackingmore.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/apitrackingmore.py b/apitrackingmore.py index 844204e..0110179 100644 --- a/apitrackingmore.py +++ b/apitrackingmore.py @@ -43,17 +43,17 @@ def get_or_create_tracking_item(carrier, code): def get_carriers(code): - cursor = db.rastreiobot.find_one({ + package = db.rastreiobot.find_one({ "code": code }) - try: - 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) + + if package: + carriers = package['carrier'] + return carriers if isisntance(carriers, list) else list(carriers) + + carriers = trackingmore.detect_carrier_from_code(code) + carriers.sort(key=lambda carrier: carrier['code']) + set_carrier_db(code, carriers) return carriers From ba071ac229430bce475853f23c4578b8d4b9494c Mon Sep 17 00:00:00 2001 From: Marco Rougeth Date: Tue, 8 Oct 2019 13:21:10 +0100 Subject: [PATCH 17/21] Fix typo on apitrackingmore.py --- apitrackingmore.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apitrackingmore.py b/apitrackingmore.py index 0110179..6e369b3 100644 --- a/apitrackingmore.py +++ b/apitrackingmore.py @@ -49,7 +49,7 @@ def get_carriers(code): if package: carriers = package['carrier'] - return carriers if isisntance(carriers, list) else list(carriers) + return carriers if isinstance(carriers, list) else list(carriers) carriers = trackingmore.detect_carrier_from_code(code) carriers.sort(key=lambda carrier: carrier['code']) From d50b1517d22ce60cc6c6968d4ccce30ec6aebecf Mon Sep 17 00:00:00 2001 From: Marco Rougeth Date: Tue, 8 Oct 2019 13:26:50 +0100 Subject: [PATCH 18/21] =?UTF-8?q?=20=F0=9F=91=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apitrackingmore.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apitrackingmore.py b/apitrackingmore.py index 6e369b3..9e76469 100644 --- a/apitrackingmore.py +++ b/apitrackingmore.py @@ -49,7 +49,7 @@ def get_carriers(code): if package: carriers = package['carrier'] - return carriers if isinstance(carriers, list) else list(carriers) + return carriers if isinstance(carriers, list) else [carriers] carriers = trackingmore.detect_carrier_from_code(code) carriers.sort(key=lambda carrier: carrier['code']) From cc39d605bcb4367ead45f507a7e053dc21154a84 Mon Sep 17 00:00:00 2001 From: Rafael Gottsfritz Date: Sun, 13 Oct 2019 10:52:32 -0300 Subject: [PATCH 19/21] Remove unnused dependencies --- requirements.txt | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/requirements.txt b/requirements.txt index 228d653..83d6d0e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,31 +1,5 @@ -appdirs==1.4.0 -backports-abc==0.5 beautifulsoup4==4.5.3 -bokeh==0.12.4 -bs4==0.0.1 -decorator==4.0.11 -ipdb==0.10.2 -ipython==5.2.1 -ipython-genutils==0.1.0 -Jinja2==2.9.5 -MarkupSafe==0.23 -packaging==16.8 -pexpect==4.2.1 -pickleshare==0.7.4 -prompt-toolkit==1.0.13 -ptyprocess==0.5.1 -Pygments==2.2.0 pymongo==3.4.0 -pyparsing==2.1.10 -pyTelegramBotAPI==3.6.6 -python-dateutil==2.6.0 -pytz==2016.10 -PyYAML==3.13 +pytest==5.2.0 requests==2.20.0 -simplegeneric==0.8.1 -six==1.10.0 -tornado==4.4.2 -traitlets==4.3.1 -wcwidth==0.1.7 sentry-sdk==0.7.14 -pytest==5.2.0 From 4f02ec2842ffeb0cefbedd63b9b8d6f07b2201a0 Mon Sep 17 00:00:00 2001 From: Rafael Gottsfritz Date: Sun, 13 Oct 2019 10:53:03 -0300 Subject: [PATCH 20/21] Update dependencies --- requirements.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/requirements.txt b/requirements.txt index 83d6d0e..3ab3b5c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ -beautifulsoup4==4.5.3 -pymongo==3.4.0 -pytest==5.2.0 -requests==2.20.0 -sentry-sdk==0.7.14 +beautifulsoup4==4.* +pymongo==3.* +pytest==5.* +requests==2.* +sentry-sdk==0.* From 8ae1770f036727317e677a62213830da4667722e Mon Sep 17 00:00:00 2001 From: Rafael Gottsfritz Date: Sun, 13 Oct 2019 10:53:17 -0300 Subject: [PATCH 21/21] Remove Pipfile --- Pipfile | 42 ------------------------------------------ 1 file changed, 42 deletions(-) delete mode 100644 Pipfile diff --git a/Pipfile b/Pipfile deleted file mode 100644 index f880aae..0000000 --- a/Pipfile +++ /dev/null @@ -1,42 +0,0 @@ -[[source]] -url = "https://pypi.python.org/simple" -verify_ssl = true -name = "pypi" - -[packages] -appdirs = "==1.4.0" -backports-abc = "==0.5" -"beautifulsoup4" = "==4.5.3" -bokeh = "==0.12.4" -"bs4" = "==0.0.1" -decorator = "==4.0.11" -ipdb = "==0.10.2" -ipython = "==5.2.1" -ipython-genutils = "==0.1.0" -"jinja2" = "==2.9.5" -markupsafe = "==0.23" -numpy = "==1.12.0" -pandas = "==0.19.2" -pexpect = "==4.2.1" -pickleshare = "==0.7.4" -prompt-toolkit = "==1.0.13" -ptyprocess = "==0.5.1" -pygments = "==2.2.0" -pymongo = "==3.4.0" -pyparsing = "==2.1.10" -pytelegrambotapi = "==2.3.0" -python-dateutil = "==2.6.0" -pytz = "==2016.10" -pyyaml = "==3.12" -requests = "*" -simplegeneric = "==0.8.1" -six = "==1.10.0" -tornado = "==4.4.2" -traitlets = "==4.3.1" -wcwidth = "==0.1.7" -var-dump = "*" - -[dev-packages] - -[requires] -python_version = "3.6"