import requests
import json

artifactory_url = 'https://art.lmru.tech/artifactory/api/repositories'
headers = {
    'Content-Type': 'application/json'
}

def get_virtual_repo_details(repo_key):
    virtual_repo_url = f'{artifactory_url}/{repo_key}'
    response = requests.get(virtual_repo_url, headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        print(f"Error getting details for virtual repository {repo_key}: {response.status_code} {response.text}")
        return None

response = requests.get(artifactory_url, headers=headers)
if response.status_code == 200:
    repositories = response.json()
    for repo in repositories:
        if repo['type'] == 'VIRTUAL':
            details = get_virtual_repo_details(repo['key'])
            if details and 'repositories' in details:
                repo['repositories'] = details['repositories']
    
    with open('artifactory_repositories.json', 'w') as file:
        json.dump(repositories, file, indent=4)
    print("Repository list successfully retrieved and saved to artifactory_repositories.json")
else:
    print("Error retrieving repository list:", response.status_code, response.text)
