from urllib.request import urlretrieve
from pathlib import Path

import gender_guesser.detector as gender
from bs4 import BeautifulSoup as Soup

TMP = Path('/tmp')
PYCON_HTML = TMP / "pycon2019.html"
PYCON_PAGE = ('https://bites-data.s3.us-east-2.amazonaws.com/'
              'pycon2019.html')

if not PYCON_HTML.exists():
    urlretrieve(PYCON_PAGE, PYCON_HTML)


def _get_soup(html=PYCON_HTML):
    return Soup(html.read_text(encoding="utf-8"), "html.parser")


def get_pycon_speaker_first_names(soup=None):
    """Parse the PYCON_HTML using BeautifulSoup, extracting all
       speakers (class "speaker"). Note that some items contain
       multiple speakers so you need to extract them.
       Return a list of first names
    """
    soup = _get_soup()
    
    first_names = []
    
    for name in soup.find_all('span', class_='speaker'):
        for i in name.text.strip().split(', '):
            for j in i.split('/ '):
                full_name = j
                if '.' in full_name.split()[0]:
                    first_names.append(full_name.split()[1])
                    continue
                first_names.append(full_name.split()[0])
            
    return first_names
    pass

def get_percentage_of_female_speakers(first_names):
    """Run gender_guesser on the names returning a percentage
       of female speakers (female and mostly_female),
       rounded to 2 decimal places."""
       
    females = 0
    
    d = gender.Detector()
    
    for name in first_names:
        sex = d.get_gender(name)
        if 'female' in sex:
            females += 1
            
    return round(females/len(first_names)*100, 2)
    pass

if __name__ == '__main__':
    names = get_pycon_speaker_first_names()
    perc = get_percentage_of_female_speakers(names)
    print(perc)