import json


def get_citation_data(req, citation_url):
    """Extract the number of citations from an OpenCitations URL request.

    Parameters
    ----------
    req : Requester
        Requester to launch requests from.
    citation_url : str
        URL to collect citation data from.

    Returns
    -------
    n_citations : int
        The number of citations the article has received.
    """

    page = req.request_url(citation_url)
    n_citations = len(json.loads(page.content.decode('utf-8')))

    # If the return is empty, encode as None instead of zero
    #   This is because we don't want to treat missing data as 0 citations
    if n_citations == 0:
        n_citations = None

    return n_citations