Recent

Author Topic: Convert script python in pascal  (Read 125 times)

laguna

  • Sr. Member
  • ****
  • Posts: 322
Convert script python in pascal
« on: March 29, 2023, 09:42:34 am »
I'm not good in pascal, and I don't know the python language, I ask you for help to convert this script to pascal.
The program logs into the website and downloads files.
Thanks

Code: Python  [Select][+][-]
  1. import requests
  2. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  3. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  4. from clint.textui import colored, puts
  5. from tqdm import *
  6. import re
  7. import time
  8. from datetime import timedelta, datetime, tzinfo, timezone
  9. import sys
  10. import pytz
  11. import json
  12. import os
  13.    
  14. def unixTime():
  15.     dt = datetime.now(tz=pytz.utc)
  16.     return str(int(dt.timestamp() * 1000))
  17.  
  18.  
  19. #start
  20. try:
  21.        
  22.     CF = 'USERNAME'
  23.    
  24.     PIN = '123456789'
  25.  
  26.     Password  = 'PassW0rd'
  27.  
  28.  
  29.     PIVA  = '01234567890'
  30.  
  31.     #ddmmyyyy
  32.     Dal = '01032023'
  33.  
  34.     #ddmmyyyy
  35.     Al = '01032023'
  36.  
  37.  
  38.  
  39.     Tipo = 'ENT'
  40.  
  41.  
  42.  s = requests.Session()
  43.     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'})
  44.     s.headers.update({'Connection': 'keep-alive'})
  45.  
  46.     cookie_obj1 = requests.cookies.create_cookie(domain='ivaservizi.agenziaentrate.gov.it',name='LFR_SESSION_STATE_20159',value=unixTime())
  47.     s.cookies.set_cookie(cookie_obj1)
  48.     cookie_obj2 = requests.cookies.create_cookie(domain='ivaservizi.agenziaentrate.gov.it',name='LFR_SESSION_STATE_10811916',value=unixTime())
  49.     s.cookies.set_cookie(cookie_obj2)
  50.     r = s.get('https://ivaservizi.agenziaentrate.gov.it/portale/web/guest', verify=False)
  51.  
  52.     if r.status_code == 200:
  53.         puts(colored.yellow('Collegamento alla homepage. Avvio.'))
  54.     else:
  55.         puts(colored.red('Collegamento alla homepage non riuscito: uscita.'))
  56.         sys.exit()
  57.    
  58.     cookieJar = s.cookies
  59.  
  60.     print('Effettuo il login')
  61.     payload = {'_58_saveLastPath': 'false', '_58_redirect' : '', '_58_doActionAfterLogin': 'false', '_58_login': CF , '_58_pin': PIN, '_58_password': Password}    
  62.     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)
  63.     cookieJar = s.cookies
  64.  
  65.     liferay = re.findall(r"Liferay.authToken = '.*';", r.text)[0]
  66.     p_auth = liferay.replace("Liferay.authToken = '","")
  67.     p_auth = p_auth.replace("';", "")
  68.  
  69.     r = s.get('https://ivaservizi.agenziaentrate.gov.it/dp/api?v=' + unixTime())
  70.  
  71.     if r.status_code == 200:
  72.         puts(colored.yellow('Login riuscito.'))
  73.     else:
  74.         puts(colored.red('Login non riuscito: uscita.'))
  75.         sys.exit()
  76.  
  77.     cookieJar = s.cookies
  78.    
  79.     if Tipo == "FOL":
  80.         accesso = "-FOL"
  81.     elif Tipo == "ENT":
  82.         accesso = "-000"
  83.     else:
  84.         puts(colored.red('Tipo incarico deve essere FOL per FiscoOnLine e ENT per Entratel: uscita.'))
  85.         sys.exit()
  86.  
  87.     print('Seleziono il tipo di incarico per la PIVA ' + PIVA)
  88.     payload = {'sceltaincarico': PIVA + accesso, 'tipoincaricante' : 'incDiretto'}    
  89.     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)
  90.  
  91.     if r.status_code == 200:
  92.         puts(colored.yellow('Selezione incarico riuscita.'))
  93.     else:
  94.         puts(colored.red('Selezione incarico non riuscita: uscita.'))
  95.         sys.exit()
  96.  
  97.                            
  98.     print('Aderisco al servizio')
  99.     r = s.get('https://ivaservizi.agenziaentrate.gov.it/ser/api/fatture/v1/ul/me/adesione/stato/')
  100.  
  101.     if r.status_code == 200:
  102.         puts(colored.yellow('Adesione riuscita ai servizi AdE.'))
  103.     else:
  104.         puts(colored.red('Adesione ai servizi AdE non riuscita: uscita.'))
  105.         sys.exit()
  106.  
  107.     cookieJar = s.cookies
  108.  
  109.  
  110.     headers_token = {'x-xss-protection': '1; mode=block',
  111.            'strict-transport-security': 'max-age=16070400; includeSubDomains',
  112.            'x-content-type-options': 'nosniff',
  113.            'x-frame-options': 'deny'}
  114.     r = s.get('https://ivaservizi.agenziaentrate.gov.it/cons/cons-services/sc/tokenB2BCookie/get?v='+unixTime() , headers = headers_token )
  115.  
  116.     if r.status_code == 200:
  117.         puts(colored.yellow('B2B Cookie ottenuto'))
  118.     else:
  119.         puts(colored.red('B2B Cookie non ottenuto: uscita.'))
  120.         sys.exit()
  121.  
  122.     cookieJar = s.cookies
  123.     tokens = r.headers
  124.  
  125.     xb2bcookie = r.headers.get('x-b2bcookie')
  126.     xtoken = r.headers.get('x-token')
  127.  
  128.  
  129.     s.headers.update({'Host': 'ivaservizi.agenziaentrate.gov.it'})
  130.     s.headers.update({'Referer': 'https://ivaservizi.agenziaentrate.gov.it/cons/cons-web/?v=' + unixTime()})
  131.     s.headers.update({'Accept': 'application/json, text/plain, */*'})
  132.     s.headers.update({'Accept-Encoding': 'gzip, deflate, br'})
  133.     s.headers.update({'Accept-Language': 'it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,fr;q=0.6'})
  134.     s.headers.update({'DNT': '1'})
  135.     s.headers.update({'X-XSS-Protection': '1; mode=block'})
  136.     s.headers.update({'Strict-Transport-Security': 'max-age=16070400; includeSubDomains'})
  137.     s.headers.update({'X-Content-Type-Options': 'nosniff'})
  138.     s.headers.update({'X-Frame-Options': 'deny'})
  139.     s.headers.update({'x-b2bcookie': xb2bcookie})
  140.     s.headers.update({'x-token': xtoken})
  141.  
  142.     headers = {'Host': 'ivaservizi.agenziaentrate.gov.it',
  143.            'referer': 'https://ivaservizi.agenziaentrate.gov.it/cons/cons-web/?v=' + unixTime(),
  144.            'accept': 'application/json, text/plain, */*',
  145.            'accept-encoding': 'gzip, deflate, br',
  146.            'accept-language': 'it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,fr;q=0.6',
  147.            'DNT': '1',
  148.            'x-xss-protection': '1; mode=block',
  149.            'strict-transport-security': 'max-age=16070400; includeSubDomains',
  150.            'x-content-type-options': 'nosniff',
  151.            'x-frame-options': 'deny',
  152.            'x-b2bcookie': xb2bcookie,
  153.            'x-token': xtoken,
  154.            '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'}
  155.  
  156.  
  157.     cookieJar = s.cookies
  158.  
  159.     print('Scarico la lista (json) delle fatture ricevute.')
  160.     r = s.get('https://ivaservizi.agenziaentrate.gov.it/cons/cons-services/rs/fe/ricevute/dal/'+Dal+'/al/'+Al+'/ricerca/ricezione?v=' + unixTime(), headers = headers)
  161.  
  162.     if r.status_code == 200:
  163.         puts(colored.yellow('Lista ottenuta.'))
  164.     else:
  165.         puts(colored.red('Lista non ottenuta: uscita.'))
  166.         sys.exit()
  167.  
  168.  
  169.     with open('fe_ricevute_' + PIVA + '.json', 'wb') as f:
  170.         f.write(r.content)
  171.  
  172.    
  173.     path = r'Ricevute_' + PIVA
  174.     if not os.path.exists(path):
  175.         os.makedirs(path)
  176.     with open('fe_ricevute_'+ PIVA +'.json') as data_file:    
  177.         data = json.load(data_file)
  178.         print('Inizio a scaricare ' + str(data['totaleFatture']) + ' fatture dal ' + data['dataRicercaDa'] + ' al ' + data['dataRicercaA'] + ' per un massimo di ' + str(data['limiteBloccoTotaleFatture']) + ' fatture scaricabili.')
  179.         for fattura in data['fatture']:
  180.             fatturaFile = fattura['tipoInvio']+fattura['idFattura']
  181.             with s.get('https://ivaservizi.agenziaentrate.gov.it/cons/cons-services/rs/fatture/file/'+fatturaFile+'?tipoFile=FILE_FATTURA&download=1&v='+unixTime(), headers = headers_token , stream = True) as r:
  182.                 r.raise_for_status()
  183.                 total_size = int(r.headers.get('content-length', 0))
  184.                 d = r.headers['content-disposition']
  185.                 fname = re.findall("filename=(.+)", d)
  186.                 with open(path + '/' + fname[0], 'wb') as f:
  187.                     pbar = tqdm(total=total_size, unit='B', unit_divisor=1024, unit_scale=True, ascii=True)
  188.                     pbar.set_description('Scaricando ' + fname[0])
  189.                     for chunk in r.iter_content(chunk_size=1024):
  190.                         if chunk:  
  191.                             f.write(chunk)
  192.                             pbar.update(len(chunk))
  193.                     pbar.close()
  194.             with s.get('https://ivaservizi.agenziaentrate.gov.it/cons/cons-services/rs/fatture/file/'+fatturaFile+'?tipoFile=FILE_METADATI&download=1&v='+unixTime(), headers = headers_token , stream = True) as r:
  195.                 r.raise_for_status()
  196.                 total_size = int(r.headers.get('content-length', 0))
  197.                 d = r.headers['content-disposition']
  198.                 fname = re.findall("filename=(.+)", d)
  199.                 with open(path + '/' + fname[0], 'wb') as f:
  200.                     pbar = tqdm(total=total_size, unit='B', unit_divisor=1024, unit_scale=True, ascii=True)
  201.                     pbar.set_description('Scaricando ' + fname[0])
  202.                     for chunk in r.iter_content(chunk_size=1024):
  203.                         if chunk:  
  204.                             f.write(chunk)
  205.                             pbar.update(len(chunk))
  206.                     pbar.close()                
  207. except KeyboardInterrupt:
  208.        print("Programma terminato manualmente!")
  209.        sys.exit()
  210. #end            
  211. sys.exit()
  212.  
  213.  

 

TinyPortal © 2005-2018