Exemplo n.º 1
0
    def import_metadata(self):
        filename = os.path.join(self.data_dir, 'state_metadata.json')
        self.log("Importing state metadata")

        with open(filename) as f:
            metadata = json.load(f)

        if 'state_metadata' in self.db:
            del self.db['state_metadata']
        self.db['state_metadata'] = metadata

        self.metadata = StateMetadata.get(self.db)
Exemplo n.º 2
0
    def run(self):
        server = Server()
        assert self.state in server
        db = server[self.state]

        # Grab metadata first so exporter can set up directory structure, etc.
        # based on it
        self.saw_metadata(StateMetadata.get(db))

        for bill in Bill.all(db):
            self.saw_bill(bill)

        for legislator in Legislator.all(db):
            self.saw_legislator(legislator)
Exemplo n.º 3
0
def get_bio(state, year, replace=True, verbose=False):
    """
    Get biographical data from Project Vote Smart on state legislators elected
    in the given state during the given year.
    """

    def log(msg):
        if verbose:
            print "%s: %s" % (state, msg)

    # Setup couch connection
    server = Server('http://localhost:5984')
    assert state in server
    db = server[state]

    metadata = StateMetadata.get(db)

    # We expect the passed in year to be an election year, so find
    # the session corresponding to that election
    session = metadata.session_for_election(int(year))
    assert session

    # Get list of officials from votesmart
    candidates = []
    for officeId in [7, 8, 9]:
        try:
            candidates.extend(
                votesmart.candidates.getByOfficeState(officeId, state.upper(),
                                                      electionYear=year))
        except:
            pass
    elected = filter(lambda cand: cand.electionStatus == 'Won', candidates)
    officials = {}
    for official in elected:
        if official.lastName in officials:
            officials[official.lastName].append(official)
        else:
            officials[official.lastName] = [official]

    # Go through CouchDB legislators
    # We grab all legislators for the session following the election year.
    # While this will get some unneeded docs but only involves one (Couch)
    # round trip, while searching separately for each official
    # returned by Vote Smart would involve many round trips.
    for leg in Legislator.for_session(db, session):
        if 'votesmart' in leg._data and not replace:
            # We already have data for this legislator (and don't
            # want to replace it)
            continue

        if leg.last_name in officials:
            for match in officials[leg.last_name]:
                if match.title == metadata.lower_title:
                    chamber = 'lower'
                else:
                    chamber = 'upper'

                if (match.firstName == leg.first_name and
                    chamber == leg.chamber):
                    # Found match
                    log("Getting bio data for %s (%s)" %
                        (leg.full_name, leg.chamber))

                    bio = votesmart.candidatebio.getBio(match.candidateId)
                    leg._data['votesmart'] = bio.__dict__
                    leg.store(db)