forked from claudiopizzillo/FeCscraper
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfec_corrispettivi_invio.py
197 lines (175 loc) · 12.2 KB
/
fec_corrispettivi_invio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
## Crapanzano Salvatore (Corrispettivi Fattura)
## Per effettuare l'invio delle richieste corrispettivi sul portale ivaservizi.agenziaentrate.gov.it, inserire il nome del file nella relativa parte del codice
## Vers. 1.0a del 12-03-2021 - non usare il file
## Cartella di lavoro è ./inviocorrispettivi sotto la dir di lancio dello script di invio
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
import re
import sys
import pytz
import json
import os
import base64
import time
from datetime import timedelta, datetime, tzinfo, timezone
def unixTime():
dt = datetime.now(tz=pytz.utc)
return str(int(dt.timestamp() * 1000))
print('ATTENZIONE: Verranno inviate le richieste corrispettivi con estenzione xml contenute nella sotto-cartella ./invioCORRISPETTIVI')
print('per il cliente impostato nel percorso di lancio del programma!')
time.sleep(5)
now = datetime.now() # current date and time
profilo = 1
CF = sys.argv[1]
PIN = sys.argv[2]
Password = sys.argv[3]
cfstudio = sys.argv[4]
cfcliente = sys.argv[5]
pivadiretta = sys.argv[6]
for root, dirs, files in os.walk("./inviocorrispettivi"):
for file in files:
print("Elenco file Corrispettivi da inviare: ", files)
ext = [".xml", "XML"]
wait = input("Premi Enter per continuare.")
s = requests.Session()
s.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'})
s.headers.update({'Connection': 'keep-alive'})
cookie_obj1 = requests.cookies.create_cookie(domain='ivaservizi.agenziaentrate.gov.it',name='LFR_SESSION_STATE_20159',value='expired')
s.cookies.set_cookie(cookie_obj1)
cookie_obj2 = requests.cookies.create_cookie(domain='ivaservizi.agenziaentrate.gov.it',name='LFR_SESSION_STATE_10811916',value=unixTime())
s.cookies.set_cookie(cookie_obj2)
r = s.get('https://ivaservizi.agenziaentrate.gov.it/portale/web/guest', verify=False)
print('Collegamento alla homepage')
cookieJar = s.cookies
print('Effettuo il login')
payload = {'_58_saveLastPath': 'false', '_58_redirect' : '', '_58_doActionAfterLogin': 'false', '_58_login': CF , '_58_pin': PIN, '_58_password': Password}
r = s.post('https://ivaservizi.agenziaentrate.gov.it/portale/home?p_p_id=58&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_pos=3&p_p_col_count=4&_58_struts_action=%2Flogin%2Flogin', data=payload)
cookieJar = s.cookies
liferay = re.findall(r"Liferay.authToken = '.*';", r.text)[0]
p_auth = liferay.replace("Liferay.authToken = '","")
p_auth = p_auth.replace("';", "")
r = s.get('https://ivaservizi.agenziaentrate.gov.it/dp/api?v=' + unixTime())
cookieJar = s.cookies
print('Seleziono il tipo di incarico')
# payload = {'sceltaincarico': cfstudio + '-000', 'tipoincaricante' : 'incDiretto'}
# r = s.post('https://ivaservizi.agenziaentrate.gov.it/portale/scelta-utenza-lavoro?p_auth='+ p_auth + '&p_p_id=SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet_javax.portlet.action=incarichiAction', data=payload)
if profilo == 1:
# Delega Diretta
payload = {'cf_inserito': cfcliente};
r = s.post('https://ivaservizi.agenziaentrate.gov.it/portale/scelta-utenza-lavoro?p_auth='+ p_auth + '&p_p_id=SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet_javax.portlet.action=delegaDirettaAction', data=payload);
payload = {'cf_inserito': cfcliente, 'sceltapiva' : pivadiretta};
r = s.post('https://ivaservizi.agenziaentrate.gov.it/portale/scelta-utenza-lavoro?p_auth='+ p_auth + '&p_p_id=SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet_javax.portlet.action=delegaDirettaAction', data=payload);
# Me stesso
elif profilo == 2:
payload = {'sceltaincarico': cfstudio + '-000', 'tipoincaricante' : 'incDiretto'};
r = s.post('https://ivaservizi.agenziaentrate.gov.it/portale/scelta-utenza-lavoro?p_auth='+ p_auth + '&p_p_id=SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet_javax.portlet.action=meStessoAction', data=payload)
payload = {'sceltaincarico': cfstudio + '-000', 'tipoincaricante' : 'incDiretto', 'sceltapiva' : pivadiretta};
r = s.post('https://ivaservizi.agenziaentrate.gov.it/portale/scelta-utenza-lavoro?p_auth='+ p_auth + '&p_p_id=SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet_javax.portlet.action=meStessoAction', data=payload);
# Login per STUDIO ASSOCIATO
else:
payload = {'sceltaincarico': cfstudio + '-000', 'tipoincaricante' : 'incDelega', 'cf_inserito': cfcliente};
r = s.post('https://ivaservizi.agenziaentrate.gov.it/portale/scelta-utenza-lavoro?p_auth='+ p_auth + '&p_p_id=SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet_javax.portlet.action=incarichiAction', data=payload);
payload = {'sceltaincarico': cfstudio + '-000', 'tipoincaricante' : 'incDelega', 'cf_inserito': cfcliente, 'sceltapiva' : pivadiretta};
r = s.post('https://ivaservizi.agenziaentrate.gov.it/portale/scelta-utenza-lavoro?p_auth='+ p_auth + '&p_p_id=SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_SceltaUtenzaLavoro_WAR_SceltaUtenzaLavoroportlet_javax.portlet.action=incarichiAction', data=payload);
print('Aderisco al servizio')
r = s.get('https://ivaservizi.agenziaentrate.gov.it/ser/api/fatture/v1/ul/me/adesione/stato/')
cookieJar = s.cookies
headers_token = {'x-xss-protection': '1; mode=block',
'strict-transport-security': 'max-age=16070400; includeSubDomains',
'x-content-type-options': 'nosniff',
'x-frame-options': 'deny'}
r = s.get('https://ivaservizi.agenziaentrate.gov.it/cons/cons-services/sc/tokenB2BCookie/get?v='+unixTime() , headers = headers_token )
cookieJar = s.cookies
tokens = r.headers
xb2bcookie = r.headers.get('x-b2bcookie')
xtoken = r.headers.get('x-token')
s.headers.update({'Host': 'ivaservizi.agenziaentrate.gov.it'})
s.headers.update({'Referer': 'https://ivaservizi.agenziaentrate.gov.it/cons/cons-web/?v=' + unixTime()})
s.headers.update({'Accept': 'application/json, text/plain, */*'})
s.headers.update({'Accept-Encoding': 'gzip, deflate, br'})
s.headers.update({'Accept-Language': 'it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,fr;q=0.6'})
s.headers.update({'DNT': '1'})
s.headers.update({'X-XSS-Protection': '1; mode=block'})
s.headers.update({'Strict-Transport-Security': 'max-age=16070400; includeSubDomains'})
s.headers.update({'X-Content-Type-Options': 'nosniff'})
s.headers.update({'X-Frame-Options': 'deny'})
s.headers.update({'x-b2bcookie': xb2bcookie})
s.headers.update({'x-token': xtoken})
headers = {'Host': 'ivaservizi.agenziaentrate.gov.it',
'referer': 'https://ivaservizi.agenziaentrate.gov.it/cons/cons-web/?v=' + unixTime(),
'accept': 'application/json, text/plain, */*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,fr;q=0.6',
'DNT': '1',
'x-xss-protection': '1; mode=block',
'strict-transport-security': 'max-age=16070400; includeSubDomains',
'x-content-type-options': 'nosniff',
'x-frame-options': 'deny',
'x-b2bcookie': xb2bcookie,
'x-token': xtoken,
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'}
print('Accetto le condizioni')
r = s.get('https://ivaservizi.agenziaentrate.gov.it/cons/cons-services/rs/disclaimer/accetta?v='+unixTime() , headers = headers_token )
cookieJar = s.cookies
#print('controllo file')
#r = s.get('https://ivaservizi.agenziaentrate.gov.it/ser/fatturewizard/html/partials/supporto/controllo.html?v='+now.strftime("%Y-%m-%d"), headers = headers)
#seleziona file
## nome_file='IT02081640845_00001.xml'
nr_file = 0
for root, dirs, files in os.walk("./inviocorrispettivi"):
for file in files:
print("Elenco file Corrispettivi da inviare: ", files)
ext = [".xml", "XML"]
if file.endswith(tuple(ext)):
nome_file = file
# print("Percorso", os.path.join(dirs))
print("Il nome file da inviare è", nome_file)
with open('./inviocorrispettivi/' + nome_file, "rb") as f:
headers_file= { 'Host': 'ivaservizi.agenziaentrate.gov.it',
'Cache-Control': 'max-age=0',
'sec-ch-ua': '"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-User': '?1',
'Sec-Fetch-Dest': 'empty',
'Upgrade-Insecure-Requests': '1',
'accept': 'application/json, text/plain, */*',
'accept-encoding': 'gzip, deflate',
'accept-language': 'it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,fr;q=0.6',
'Origin': 'https://ivaservizi.agenziaentrate.gov.it',
'x-frame-options': 'deny',
'x-b2bcookie': xb2bcookie,
'x-token': xtoken,
'X-XSS-Protection': '1; mode=block',
'Strict-Transport-Security': 'max-age=16070400; includeSubDomains',
'Content-Type': 'application/xml;charset=utf-8',
'connection': 'keep-alive',
'X-Frame-Options': 'deny',
'x-nome-file': nome_file,
'X-Content-Type-Options': 'nosniff',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'}
print("Contenuto", f)
r = s.post('https://ivaservizi.agenziaentrate.gov.it/cons/mass-services/rs/file/upload?tipoRichiesta=CORR',headers=headers_file, data=f)
cookieJar = s.cookies
print(r.status_code)
if r.status_code == 500:
print('Servizio dell''Agenzia delle Entrate non disponibile')
print('Invio non andanto a buon fine, riprovare più tardi')
print('Disconnessione in corso')
r = s.get('https://ivaservizi.agenziaentrate.gov.it/portale/c/portal/logout')
sys.exit()
if r.status_code == 200:
print('Invio file/files Corrispettivi inviato/i con successo!', nome_file)
print("Codice invio Corrispettivi:", r.text)
conservate = open(cfcliente+ "_corrispettivi.csv", 'a')
conservate.write(nome_file)
conservate.write(",")
conservate.write(r.text)
conservate.write(",")
conservate.write(str(now) + "\n")
conservate.close()
nr_file = nr_file + 1
print("nr. files inviato/i --> ", nr_file)
continue;