def make_dict_of_ftm_eids(use_pickle=True):
    """Build up a dictionary mapping jurisdiction IDs to candidates in those
    locations
    """
    pickle_location = '/tmp/eid_lists.pkl'
    if use_pickle:
        if os.path.isfile(pickle_location):
            with open(pickle_location, 'r') as f:
                logger.info("Loading pickled candidate list. Read the command "
                            "documentation if this is not desired.")
                return pickle.load(f)
        else:
            logger.info("Unable to find pickle file.")

    candidate_eid_lists = defaultdict(list)

    for courtid, (state, level) in courtid2statelevel.items():
        if level != 'H':
            # We only want high courts.
            continue
        for year in range(1989, 2017):
            url = url_template.format(
                key=settings.FTM_KEY,
                state=state,
                level=leveldict[level],
                year=year,
            )
            logger.info("Getting url at: %s" % url)
            data = requests.get(url).json()

            if data['records'] == ['No Records']:
                logger.info('  No records found in court %s and year %s.' % (
                    courtid,
                    year,
                ))
                continue
            logger.info('  Found %s records in court %s and year %s' % (
                len(data['records']),
                courtid,
                year,
            ))

            for item in data['records']:
                # add an eid, name, year tuple to this court's list
                candidate_eid_lists[courtid].append({
                    'eid':
                    item['Candidate_Entity']['id'],
                    'name':
                    item['Candidate_Entity']['Candidate_Entity'],
                    'total':
                    float(item['Total_$']['Total_$']),
                    'year':
                    year,
                })

    if use_pickle:
        with open(pickle_location, 'w') as f:
            logger.info("Creating pickle file at: %s" % pickle_location)
            pickle.dump(candidate_eid_lists, f)
    return candidate_eid_lists
Exemplo n.º 2
0
def make_dict_of_ftm_eids(use_pickle=True):
    """Build up a dictionary mapping jurisdiction IDs to candidates in those
    locations
    """
    pickle_location = '/tmp/eid_lists.pkl'
    if use_pickle:
        if os.path.isfile(pickle_location):
            with open(pickle_location, 'r') as f:
                print("Loading pickled candidate list. Read the command "
                      "documentation if this is not desired.")
                return pickle.load(f)
        else:
            print("Unable to find pickle file.")

    candidate_eid_lists = defaultdict(list)

    for courtid, (state, level) in courtid2statelevel.items():
        if level != 'H':
            # We only want high courts.
            continue
        for year in range(1989, 2017):
            url = url_template.format(
                key=settings.FTM_KEY,
                state=state,
                level=leveldict[level],
                year=year,
            )
            print("Getting url at: %s" % url)
            data = requests.get(url).json()

            if data['records'] == ['No Records']:
                print('  No records found in court %s and year %s.' % (
                    courtid,
                    year,
                ))
                continue
            print('  Found %s records in court %s and year %s' % (
                len(data['records']),
                courtid,
                year,
            ))

            for item in data['records']:
                # add an eid, name, year tuple to this court's list
                candidate_eid_lists[courtid].append({
                    'eid': item['Candidate_Entity']['id'],
                    'name': item['Candidate_Entity']['Candidate_Entity'],
                    'total': float(item['Total_$']['Total_$']),
                    'year': year,
                })

    if use_pickle:
        with open(pickle_location, 'w') as f:
            print("Creating pickle file at: %s" % pickle_location)
            pickle.dump(candidate_eid_lists, f)
    return candidate_eid_lists
Exemplo n.º 3
0
def make_dict_of_ftm_eids(use_pickle=True):
    """Build up a dictionary mapping jurisdiction IDs to candidates in those
    locations
    """
    pickle_location = "/tmp/eid_lists.pkl"
    if use_pickle:
        if os.path.isfile(pickle_location):
            with open(pickle_location, "r") as f:
                logger.info("Loading pickled candidate list. Read the command "
                            "documentation if this is not desired.")
                return pickle.load(f)
        else:
            logger.info("Unable to find pickle file.")

    candidate_eid_lists = defaultdict(list)

    for courtid, (state, level) in courtid2statelevel.items():
        if level != "H":
            # We only want high courts.
            continue
        for year in range(1989, 2017):
            url = url_template.format(
                key=settings.FTM_KEY,
                state=state,
                level=leveldict[level],
                year=year,
            )
            logger.info(f"Getting url at: {url}")
            data = requests.get(url, timeout=30).json()

            if data["records"] == ["No Records"]:
                logger.info(
                    f"  No records found in court {courtid} and year {year}.")
                continue
            logger.info("  Found %s records in court %s and year %s" %
                        (len(data["records"]), courtid, year))

            for item in data["records"]:
                # add an eid, name, year tuple to this court's list
                candidate_eid_lists[courtid].append({
                    "eid":
                    item["Candidate_Entity"]["id"],
                    "name":
                    item["Candidate_Entity"]["Candidate_Entity"],
                    "total":
                    float(item["Total_$"]["Total_$"]),
                    "year":
                    year,
                })

    if use_pickle:
        with open(pickle_location, "w") as f:
            logger.info(f"Creating pickle file at: {pickle_location}")
            pickle.dump(candidate_eid_lists, f)
    return candidate_eid_lists