コード例 #1
0
def retrieve_one_ballot_from_google_civic_api(text_for_map_search, google_civic_election_id=0):
    # Request json file from Google servers
    logger.info("Loading ballot for one address from voterInfoQuery from Google servers")
    request = requests.get(VOTER_INFO_URL, params={
        "key": GOOGLE_CIVIC_API_KEY,
        "address": text_for_map_search,
        "electionId": google_civic_election_id,
    })
    structured_json = json.loads(request.text)

    # # For internal testing. Write the json retrieved above into a local file
    # with open('/Users/dalemcgrew/PythonProjects/WeVoteServer/'
    #           'import_export_google_civic/import_data/voterInfoQuery_VA_sample.json', 'w') as f:
    #     json.dump(structured_json, f)
    #     f.closed
    #
    # # TEMP - FROM FILE (so we aren't hitting Google Civic API during development)
    # with open("import_export_google_civic/import_data/voterInfoQuery_VA_sample.json") as json_data:
    #     structured_json = json.load(json_data)

    # TODO Add Google Civic API call counter so we can track the number of queries we are doing each day

    # Verify that we got a ballot. (If you use an address in California for an election in New York,
    #  you won't get a ballot for example.)
    success = False
    if 'contests' in structured_json:
        if len(structured_json['contests']) > 0:
            success = True

    results = {
        'success': success,
        'structured_json': structured_json,
    }
    return results
コード例 #2
0
def retrieve_from_google_civic_api_election_query():
    # Request json file from Google servers
    logger.info("Loading json data from Google servers, API call electionQuery")
    request = requests.get(ELECTION_QUERY_URL, params={
        "key": GOOGLE_CIVIC_API_KEY,  # This comes from an environment variable
    })
    return json.loads(request.text)
コード例 #3
0
def retrieve_from_google_civic_api_election_query():
    # Request json file from Google servers
    logger.info("Loading json data from Google servers, API call electionQuery")
    request = requests.get(ELECTION_QUERY_URL, params={
        "key": GOOGLE_CIVIC_API_KEY,  # This comes from an environment variable
    })
    # TODO Add Google Civic API call counter so we can track the number of queries we are doing each day
    return json.loads(request.text)
コード例 #4
0
def retrieve_from_google_civic_api_election_query():
    # Request json file from Google servers
    logger.info(
        "Loading json data from Google servers, API call electionQuery")
    request = requests.get(
        ELECTION_QUERY_URL,
        params={
            "key":
            GOOGLE_CIVIC_API_KEY,  # This comes from an environment variable
        })
    return json.loads(request.text)
コード例 #5
0
def import_voterinfo_from_json(save_to_db):
    """
    Get the json data, and call the sub functions that
    :return:
    """
    load_from_google_servers = False
    if load_from_google_servers:
        # Request json file from Google servers
        logger.info("Loading from Google servers")
        request = requests.get(
            VOTER_INFO_URL,
            params={
                "key":
                GOOGLE_CIVIC_API_KEY,  # This comes from an environment variable
                "address": "254 Hartford Street San Francisco CA",
                "electionId": "2000",
            })
        structured_json = json.loads(request.text)
    else:
        # Load saved json from local file
        logger.info("Loading from local file")

        with open(VOTER_INFO_JSON_FILE) as json_data:
            structured_json = json.load(json_data)

    # Process election and store in local db cache
    # google_civic_election_id = The unique ID of the election containing this contest. (Provided by Google Civic)
    google_civic_election_id = process_election_from_structured_json(
        structured_json['election'], save_to_db)

    # Loop through all pollingLocations and store in local db cache
    process_polling_locations_from_structured_json(
        structured_json['pollingLocations'], save_to_db)

    # Loop through all contests and store in local db cache
    process_contests_from_structured_json(structured_json['contests'],
                                          google_civic_election_id, save_to_db)

    # We ignore "kind", "state", and "normalizedInput"
    # "kind": "civicinfo#voterInfoResponse",

    # "state" has information (including URLs) about the election adminstrators

    # "normalizedInput": {
    #   "line1": "254 hartford st",
    #   "city": "san francisco",
    #   "state": "CA",
    #   "zip": "94114"
    #  },
    return
コード例 #6
0
def import_voterinfo_from_json(save_to_db):
    """
    Get the json data, and call the sub functions that
    :return:
    """
    load_from_google_servers = False
    if load_from_google_servers:
        # Request json file from Google servers
        logger.info("Loading from Google servers")
        request = requests.get(VOTER_INFO_URL, params={
            "key": GOOGLE_CIVIC_API_KEY,  # This comes from an environment variable
            "address": "254 Hartford Street San Francisco CA",
            "electionId": "2000",
        })
        structured_json = json.loads(request.text)
    else:
        # Load saved json from local file
        logger.info("Loading from local file")

        with open(VOTER_INFO_JSON_FILE) as json_data:
            structured_json = json.load(json_data)

    # Process election and store in local db cache
    # google_civic_election_id = The unique ID of the election containing this contest. (Provided by Google Civic)
    google_civic_election_id = process_election_from_structured_json(structured_json['election'], save_to_db)

    # Loop through all pollingLocations and store in local db cache
    process_polling_locations_from_structured_json(structured_json['pollingLocations'], save_to_db)

    # Loop through all contests and store in local db cache
    process_contests_from_structured_json(structured_json['contests'], google_civic_election_id, save_to_db)

    # We ignore "kind", "state", and "normalizedInput"
    # "kind": "civicinfo#voterInfoResponse",

    # "state" has information (including URLs) about the election adminstrators

    # "normalizedInput": {
    #   "line1": "254 hartford st",
    #   "city": "san francisco",
    #   "state": "CA",
    #   "zip": "94114"
    #  },
    return
コード例 #7
0
def retrieve_from_google_civic_api_election_query():
    # Request json file from Google servers
    logger.info("Loading json data from Google servers, API call electionQuery")
    if not positive_value_exists(ELECTION_QUERY_URL):
        results = {
            'success':  False,
            'status':   'ELECTION_QUERY_URL missing from config/environment_variables.json',
        }
        return results

    request = requests.get(ELECTION_QUERY_URL, params={
        "key": GOOGLE_CIVIC_API_KEY,  # This comes from an environment variable
    })
    # Use Google Civic API call counter to track the number of queries we are doing each day
    google_civic_api_counter_manager = GoogleCivicApiCounterManager()
    google_civic_api_counter_manager.create_counter_entry('election')
    structured_json = json.loads(request.text)

    results = {
        'structured_json':  structured_json,
        'success':          True,
        'status':           'structured_json retrieved',
    }
    return results
コード例 #8
0
ファイル: controllers.py プロジェクト: pkaiserui/WeVoteBase
def import_maplight_contest_office_candidates_from_array(
        politicians_running_for_one_contest_array):
    maplight_contest_office_saved = False  # Has the contest these politicians are running for been saved?
    maplight_contest_office_manager = MapLightContestOfficeManager()
    maplight_candidate_manager = MapLightCandidateManager()

    loop_count = 0
    loop_count_limit = 1

    for politician_id in politicians_running_for_one_contest_array:
        one_politician_array = politicians_running_for_one_contest_array[
            politician_id]

        # Save the office_contest so we can link the politicians to it first

        if not maplight_contest_office_saved:
            maplight_contest_office = MapLightContestOffice()
            if 'contest' in one_politician_array:
                maplight_contest_array = one_politician_array['contest']
                if 'office' in maplight_contest_array:
                    maplight_contest_office_array = maplight_contest_array[
                        'office']
                if 'id' in maplight_contest_array:
                    maplight_contest_id = maplight_contest_array['id']
                    maplight_contest_office = \
                        maplight_contest_office_manager.fetch_maplight_contest_office_from_id_maplight(
                            maplight_contest_id)

            # If an internal identifier is found, then we know we have an object
            if maplight_contest_office.id:
                maplight_contest_office_saved = True
                # try:
                #     maplight_contest_office.contest_id = maplight_contest_array['id']
                #     maplight_contest_office.election_date = maplight_contest_array['election_date']
                #     maplight_contest_office.title = maplight_contest_array['title']
                #     maplight_contest_office.type = maplight_contest_array['type']
                #     maplight_contest_office.url = maplight_contest_array['url']
                #     # Save into this db the 'office'?
                #     # Save into this db the 'jurisdiction'?
                #     maplight_contest_office.save()
                #     maplight_contest_office_saved = True
                #
                # except Exception as e:
                #     handle_record_not_saved_exception(e)
            else:
                try:
                    maplight_contest_office = MapLightContestOffice(
                        contest_id=maplight_contest_array['id'],
                        election_date=maplight_contest_array['election_date'],
                        title=maplight_contest_array['title'],
                        type=maplight_contest_array['type'],
                        url=maplight_contest_array['url'],
                    )
                    # Save into this db the 'office'?
                    # Save into this db the 'jurisdiction'?
                    maplight_contest_office.save()
                    maplight_contest_office_saved = True

                except Exception as e:
                    handle_record_not_saved_exception(e, logger=logger)

        maplight_candidate = maplight_candidate_manager.fetch_maplight_candidate_from_candidate_id_maplight(
            one_politician_array['candidate_id'])

        if maplight_candidate.id:
            logger.warn(u"Candidate {display_name} previously saved".format(
                display_name=maplight_candidate.display_name))
        else:
            # Not found in the MapLightCandidate database, so we need to save
            try:
                maplight_candidate = MapLightCandidate()
                maplight_candidate.politician_id = one_politician_array[
                    'politician_id']
                maplight_candidate.candidate_id = one_politician_array[
                    'candidate_id']
                maplight_candidate.display_name = one_politician_array[
                    'display_name']
                maplight_candidate.original_name = one_politician_array[
                    'original_name']
                maplight_candidate.gender = one_politician_array['gender']
                maplight_candidate.first_name = one_politician_array[
                    'first_name']
                maplight_candidate.middle_name = one_politician_array[
                    'middle_name']
                maplight_candidate.last_name = one_politician_array[
                    'last_name']
                maplight_candidate.name_prefix = one_politician_array[
                    'name_prefix']
                maplight_candidate.name_suffix = one_politician_array[
                    'name_suffix']
                maplight_candidate.bio = one_politician_array['bio']
                maplight_candidate.party = one_politician_array['party']
                maplight_candidate.candidate_flags = one_politician_array[
                    'candidate_flags']
                if validate_maplight_date(
                        one_politician_array['last_funding_update']):
                    maplight_candidate.last_funding_update = one_politician_array[
                        'last_funding_update']
                maplight_candidate.roster_name = one_politician_array[
                    'roster_name']
                maplight_candidate.photo = one_politician_array['photo']
                maplight_candidate.url = one_politician_array['url']

                maplight_candidate.save()
                logger.info(u"Candidate {display_name} added".format(
                    display_name=maplight_candidate.display_name))

            except Exception as e:
                handle_record_not_saved_exception(e, logger=logger)