import requests
from bs4 import BeautifulSoup


def get_titles(part):
    """
    THis function extracts the titles and the descriptions of the Canti's of
    each part (Inferno, Purgatorio, Paradiso). It returns them as a list of str
    """

    ## GET html data
    page = requests.get('https://it.wikisource.org/wiki/Divina_Commedia/' + part)
    soup = BeautifulSoup(page.content, 'html.parser')

    # Take text data
    text = soup.find_all('i')

    ## Extract the Canti name/numbers and their subtitles
    text = [ element.get_text() for element in text ]
    text = [ element.strip() for element in text ]
    text = [ element.replace("’", "'") for element in text ]

    canti_titles_list = []

    ## Pair them
    for i in list(range(0, len(text), 2)):
        canti_titles_list.append(text[i] + '\n' + "[" +text[i+1] + "]")

    return canti_titles_list