import requests
from bs4 import BeautifulSoup


def get_text_by_title(title):
    """Get text from Wikisource based on title.

    If the text is split over several wikipages (which is the case with novels)
    then the full text will not be returned, - only the index page.

    Parameters
    ----------
    title : str
        Title of wikipage on Danish Wikisource.

    Returns
    -------
    text : str or None
        The text. Returns none if the page does not exist.

    """
    url = 'https://da.wikisource.org/w/api.php'
    params = {'page': title, 'action': 'parse', 'format': 'json'}
    data = requests.get(url, params=params).json()
    if 'parse' in data:
        text = BeautifulSoup(data['parse']['text']['*'], "lxml").get_text()
    else:
        text = None
    return text