#!/usr/local/bin/python3.10
"""
Parse vk user prifile data from open api vk
"""
import sys
import requests
import xml.etree.ElementTree as ET

URL = input("Insert vk profile link: ")
if not URL:
    print('URL is not provided')
    sys.exit(1)

def parse_xml(data):
    p_data = []
    root_node = ET.fromstringlist(data)
    person = root_node[0]
    for child in person:
        tag = child.tag.split('}')[-1]
        keys = child.attrib.keys()
        if list(keys) != []:
            for k in keys:
                p_data.append((f"{tag} {k.split('}')[-1]}", child.attrib[k]))

        else:
            p_data.append((tag, child.text))
    return p_data

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0',
}
response = requests.get(URL, headers=headers)

if response.status_code == 200:
    try:
        pass
        #print(response.text.split('href="/foaf.php?id=')[1])
    except Exception:
        pass

    ID = response.text.split('"/photo')[1].split('_')[0]

    DATA = requests.get(f"https://vk.com/foaf.php?id={ID}").text
    #print(DATA)
    with open('l.log', 'w') as f:
        f.write(DATA)

    data = parse_xml(DATA)
    data.append(("ID", ID))

    for i in data:
        print(i[0], i[1])
