File size: 872 Bytes
1237d50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""Basic utilities module"""
import requests
import csv
import re


def request_ct(url):
    """Performs a get request that provides a (somewhat) useful error message."""
    try:
        response = requests.get(url)
    except ImportError:
        raise ImportError(
            "Couldn't retrieve the data, check your search expression or try again later."
        )
    else:
        return response


def json_handler(url):
    """Returns request in JSON (dict) format"""
    return request_ct(url).json()


def csv_handler(url):
    """Returns request in CSV (list of records) format"""

    response = request_ct(url)
    decoded_content = response.content.decode("utf-8")

    split_by_blank = re.split(r"\n\s*\n", decoded_content)  # Extracts header info
    cr = csv.reader(split_by_blank[1].splitlines(), delimiter=",")
    records = list(cr)

    return records