示例#1
0
    def import_legislators(self):
        filenames = glob.glob(os.path.join(self.data_dir, 'legislators', '*'))

        for filename in filenames:
            with open(filename) as f:
                legislator = json.load(f)

            if Legislator.for_district_and_session(self.db,
                                                   legislator['chamber'],
                                                   legislator['district'],
                                                   legislator['session']):
                self.log("Legislator already in database: %s (%s %s)" % (
                    legislator['full_name'], legislator['chamber'],
                    legislator['session']))
                continue

            # Look for merge candidates
            for match in Legislator.duplicates(self.db, legislator['chamber'],
                                               legislator['district'],
                                               legislator['full_name']):
                if self.is_adjacent(legislator['session'], match.sessions):
                    # Add on to existing legislator
                    self.log("Merging with existing legislator: %s (%s %s)" % (
                        legislator['full_name'], legislator['chamber'],
                        legislator['session']))
                    match.sessions.append(legislator['session'])
                    match.store(self.db)
                    break
            else:
                # No match. add to db
                self.log("Importing legislator: %s (%s %s)" % (
                    legislator['full_name'], legislator['chamber'],
                    legislator['session']))
                doc_id = 'legislator:%s:%s:%s' % (legislator['session'],
                                                  legislator['chamber'],
                                                  legislator['district'])
                legislator['sessions'] = [legislator['session']]
                del legislator['session']
                self.get(doc_id).update(legislator)
                self.update()

        self.update()
示例#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)
示例#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)