import io import json import pandas as pd import requests from azure.storage.blob import BlobServiceClient from io import BytesIO from zipfile import ZipFile import sys def get_ref_code_art(): pimUrl = "############" username = '############' password = '############' urlSession = "############".format(pimUrl) headers = {'Content-Type': 'application/json'} payload = { 'signin': {'username': username, 'password': password} } response = requests.request("POST", urlSession, headers=headers, data=json.dumps(payload)) auth_response = json.loads(response.text) headers['Authorization'] = 'Bearer ' + auth_response["oauth"]["access_token"] url = "############" response = requests.request("GET", url, headers=headers) print(response.json()) url = "############" payload = json.dumps({ "exportProfileId": "############" }) response = requests.request("POST", url, headers=headers, data=payload) print(response.json()['id']) url = "############" + response.json()['id'] process_response = requests.request("GET", url, headers=headers) while process_response.json()['status'] != 'finished': process_response = requests.request("GET", url, headers=headers) response = requests.request("GET", url, headers=headers) identity = response.json()['files'][0]['id'] url = f"############{identity}/#" response2 = requests.get(url, headers=headers) zip_data = BytesIO(response2.content) # Open the zip file with ZipFile(zip_data, 'r') as zip_ref: for file_name in zip_ref.namelist(): with zip_ref.open(file_name) as file: return pd.read_csv(file, dtype=str, header=0, sep=';')['############'] def verif_code_article(lst_art): if len(lst_art)==0: json_data = {"result": "Success", "Missing": None} else: ref_code_art = get_ref_code_art() cnt = 0 missing = [] for i in lst_art: if i not in set(ref_code_art): cnt += 1 missing.append(i) if cnt > 0: print(cnt) json_data = {"result": "Failure", "Missing": missing} else: json_data = {"result": "Success", "Missing": None} return json_data def get_file_list(wildcard): # Specify the connection string for your Azure Blob Storage account connection_string = "############" # Specify the name of the container container_name = "temp" # Specify the name of the file you want to access wildcard = f'ToVerify/{wildcard}' # Create a BlobServiceClient object blob_service_client = BlobServiceClient.from_connection_string(conn_str=connection_string) # List blobs in the container container_client = blob_service_client.get_container_client(container_name) blobs = container_client.list_blobs(name_starts_with=wildcard) if len(list(blobs)): for blob in blobs: blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob.name) file_contents = blob_client.download_blob(encoding='utf-8').readall() df = pd.read_csv(io.StringIO(file_contents), header=None, delimiter=';', quotechar='"', dtype=str)[4].tolist() return df else: return [] articles = get_file_list('DTM') res = verif_code_article(articles) print(len(res)) if len(res)>1: with open("outputs.json", "w") as json_file: json_file.write(json.dumps(res)) else: with open("outputs.json", "w") as json_file: json_file.write(json.dumps('Problem'))