import requests
from bs4 import BeautifulSoup


def get_sra_ids(gsm_ids):
    """
    Get SRA IDs
    Args:
        gsm_ids: the GSM IDs

    Returns:
        the list of SRA IDs
    """
    url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=sra&term={}".format(" or ".join(gsm_ids))
    r = requests.get(url)
    soup = BeautifulSoup(r.text, "xml")
    uid_list = [x.text for x in soup.find_all("Id")][::-1]

    url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=sra&rettype=runinfo&id={}". \
        format(",".join(uid_list))
    r = requests.get(url)
    soup = BeautifulSoup(r.text, "xml")

    return [x.text for x in soup.find_all("Run")]