Files
taxcom/taxcom_ip.py

101 lines
5.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json, filecmp, shutil, requests
from datetime import datetime
from pathlib import Path
from time import sleep
from icecream import ic
from socket import create_connection
ic.disable()
def check_server(ip, port):
"""Проверять доступность сервера"""
try:
create_connection((ip, port), timeout=2)
return True
except OSError:
return False
ID = [
('7381440900800903','030c75c2-2f29-11eb-119f-0cc47ab40806','Волгоград_Энгельса'),
('9961440300902043','d4fff292-2f2a-11eb-119f-0cc47ab40806','Волжский_Ленина'),
('7381440900835933','ee3baf4d-d98d-11e6-b191-e83935b036c7','Новороссийск'),
('7381440900836042','147c9228-3d13-11ef-94eb-8a456ecaea20','Крымск_Синева'),
('9961440300902580','33bbe69c-2f2b-11eb-119f-0cc47ab40806','Камышин_Некрасова'),
('7381440900836108','b50bc5a3-12e1-11e5-9265-e83935b036c7','Краснодарривокзальная'),
('9961440300515155','e33636ce-2f29-11eb-119f-0cc47ab40806','Волгоград_Авиаторов'),
('7381440900834264','691807dd-2629-11e9-993e-f06a44526bea','Волгоград_Еременко'),
('9961440300902408','13f3b7e6-2f2a-11eb-119f-0cc47ab40806','Волгоград_Аллея_Героев'),
('7381440800374463','ad06d608-b690-11ed-87bd-0cc47ab40806','Самара_Кирова_виваленде'),
]
while True:
CurDate = datetime.date(datetime.now())
prevDate = datetime.date(datetime.now())
try:
for fn, salepoint, kassa in ID:
ic(fn, salepoint)
headers = {
"Content-Type": "application/json",
"Integrator-ID": "BFFFC47E-38BB-4845-8399-7C2F5AB11A3C"
}
data = {
"Login": "kassa@ip-tr.ru",
"Password": "123456"
}
result = requests.post('https://api-lk-ofd.taxcom.ru/API/v2/Login', headers=headers, data=json.dumps(data), timeout=(20, 240))
sessionToken = result.json()['sessionToken']
headers = {
"Content-Type":"application/json",
"Session-Token": f"{sessionToken}"
}
ic(result.text)
ic(sessionToken)
try:
result = requests.get(f"https://api-lk-ofd.taxcom.ru/API/v2/ShiftList?fn={fn}&begin={prevDate}T00:00:00&end={CurDate}T23:59:59", headers=headers, timeout=(20, 240))
for i in result.json()['records']:
shiftNumber = i['shiftNumber']
fnFactoryNumber = i['fnFactoryNumber']
ic(result.text)
try:
result = requests.get(f'https://api-lk-ofd.taxcom.ru/API/v2/DocumentList?fn={fn}&shift={shiftNumber}&type=3', headers=headers, timeout=(20, 240))
ic(result.text)
cheks = result.json()['records']
except:
ic()
requests.get(f'https://api.telegram.org/bot5374522720:AAGoNUYCEyJ-7-bSAQPT7aV_W2GWcinnkQU/sendMessage?parse_mode=HTML&chat_id=394151541&text=<b>Taxcom:</b>\nЧека нет')
continue
with open('check', 'w', encoding='utf-8') as f:
f.write(str(cheks))
if Path('check').is_file():
if Path(kassa).is_file():
if filecmp.cmp('check', kassa):
print(f'{kassa} -- Новые чеки отсутствуют')
continue
shutil.copy('check', kassa)
Path('check').unlink()
data = {"salepoint": salepoint,
"res": str(cheks)}
data = json.dumps(data, ensure_ascii=False).encode('utf-8')
print(kassa)
if check_server('1c.maverik.ru', 443):
result = requests.post('https://1c.maverik.ru/bso_maverik/hs/exchange/saveTaxcomDocs', data=data, allow_redirects=False, auth=('robot', 's38*Cqy2L*PB'), timeout=(10, 120))
elif check_server('10.15.0.3', 80):
result = requests.post('http://10.15.0.3/bso_maverik/hs/exchange/saveTaxcomDocs', data=data, allow_redirects=False, auth=('robot', 's38*Cqy2L*PB'), timeout=(10, 120))
else:
if Path(kassa).is_file():
Path(kassa).unlink()
print('Ошибка загрузки чеков в 1С.\nСервер не доступен!\nЗагрузка повторится позже.')
sleep(3600)
break
print(result.text)
print('-----------------')
except:
continue
print('#################')
except Exception as e:
requests.get(f'https://api.telegram.org/bot5374522720:AAGoNUYCEyJ-7-bSAQPT7aV_W2GWcinnkQU/sendMessage?parse_mode=HTML&chat_id=394151541&text=<b>Taxcom:</b>\n{e}')
print('#################')
print('#################')
print('#################\n\n')
sleep(120)