import requests


def get_dqr(ds):
    """
    Queries DQR webservice for the datastream name passed in

    Parameters
    ----------
    ds : str
        ARM datastream name (ie, sgpmetE13.b1).

    """
    # Build URL and call through requests
    url = ''.join(("https://www.archive.arm.gov/dqrws/ARMDQR?datastream=", ds,
                   "&dqrfields=dqrid,starttime,endtime,metric,subject&timeformat=YYYYMMDD.hhmmss",
                   "&searchmetric=incorrect,suspect,missing"))
    r = requests.get(url=url)

    # Run through the returns and compile data
    num = []
    sdate = []
    edate = []
    code = []
    sub = []
    for line in r.iter_lines():
        # filter out keep-alive new lines
        if line:
            decoded_line = line.decode('utf-8')
            result = decoded_line.split('|')
            num.append(result[0])
            sdate.append(result[1])
            edate.append(result[2])
            code.append(result[3])
            sub.append(result[4])

    return {'dqr_num': num, 'sdate': sdate, 'edate': edate, 'code': code, 'subject': sub}