import requests
import json


def select_etfs(category=None):
    """
    Description
    ----
    Returns all ETFs when no input is given and has the option to give
    a specific set of symbols for the category you provide.

    Input
    ----
    category (string, default is None)
        If filled, gives all data for a specific category.

    Output
    ----
    json_data (dictionary)
        Returns a dictionary with a selection or all data based on the input.
    """
    URL = ("https://raw.githubusercontent.com/JerBouma/FinanceDatabase/master/"
           "Database/ETFs/")

    if category:
        category = category.replace(' ', '%20')
        try:
            json_file = URL + category + '.json'
            request = requests.get(json_file)
            json_data = json.loads(request.text)
        except json.decoder.JSONDecodeError:
            raise ValueError(f"Not able to find any data for {category}.")
    else:
        try:
            json_file = URL + "_ETFs.json"
            request = requests.get(json_file)
            json_data = json.loads(request.text).decode("UTF-8")
        except json.decoder.JSONDecodeError:
            raise ValueError("Not able to find any data.")

    return json_data