import requests


def get_sgg_phases(tournaments):
    """return list of json objects containing
    phase data for each given smash.gg tournament URL
    by querying the sgg API for each phase
    
    input
    ---------
    tournaments: list of json blobs (requests.get().json)
    for smashgg tournament & event:
    e.g., for tournament t and event e at that tournament,
    http://api.smash.gg/tournament/t/event/e?expand[]=groups
    """
    # get IDs of phase groups to query
    ids = []
    for tournament in tournaments:
        for phase_group in tournament['entities']['groups']:
            ids.append(phase_group['id'])

    # query smashgg API for each phase group
    phases = []
    base_url = 'http://api.smash.gg/phase_group/%s\
                ?expand[]=sets&expand[]=seeds'
    for pg_id in ids:
        phases.append(
            requests.get(base_url % pg_id).json()  # append json objects
        )

    return phases