Esempio n. 1
0
 def get_state_legid(self, legid):
     # try first to get from cache
     key = self.KEY_OPENSTATES.format(id=legid)
     leg = self.cache_get(key, None)
     
     if not leg:
         # or lookup from openstates and save
         leg = pyopenstates.get_legislator(legid)
         leg['cache_key'] = key
         self.cache_set(key, leg)
     return leg
Esempio n. 2
0
 def get_state_legid(self, legid):
     # try first to get from cache
     key = self.KEY_OPENSTATES.format(id=legid)
     leg = self.cache_get(key, None)
     
     if not leg:
         # or lookup from openstates and save
         leg = pyopenstates.get_legislator(legid)
         leg['cache_key'] = key
         self.cache_set(key, leg)
     return leg
Esempio n. 3
0
    def query(self, type='active', term_name=None):
        """
        Obtains raw data of legislators, defaults to active legislators from the latest term
        Args:
            term_name: term name as it comes from OpenStates API
            type: Either 'all' or 'active'

        Returns:
            String transformed
        """
        Tables.query(self)

        if type == 'all':
            if term_name is None:
                metadata = Metadata()
                term_name = metadata.latest_term_name
            legislators = pyopenstates.search_legislators(state=config.STATE,
                                                          term=term_name,
                                                          fields='id')
        else:  # 'active'
            legislators = pyopenstates.search_legislators(
                state=config.STATE,
                active='true',  # default
                fields='id')

        self.raw_dictionary = map(
            lambda dic: pyopenstates.get_legislator(
                dic['id'],
                fields=[
                    'id',
                    'full_name',
                    'url',
                    'roles',
                    # 'old_roles',
                    'party',
                    'district',
                    'chamber',
                    'offices',
                    'email'
                ]),
            legislators)
Esempio n. 4
0
def check_political_data_cache(key, cache=cache):
    adapter = adapt_by_key(key)
    adapted_key, adapter_suffix = adapter.key(key)
    cached_obj = cache.get(adapted_key)

    if not cached_obj:
        # some keys may not be in our local cache
        # but may be available over external APIs
        if adapted_key.startswith("us_state:openstates"):
            leg_id = key.split(':')[-1]
            leg = pyopenstates.get_legislator(leg_id)
            leg['cache_key'] = key
            cache.set(key, leg)
            cached_obj = leg

    if type(cached_obj) is list:
        data = adapter.target(cached_obj[0])
        offices = adapter.offices(cached_obj[0])
    elif type(cached_obj) is dict:
        data = adapter.target(cached_obj)
        offices = adapter.offices(cached_obj)
    else:
        current_app.logger.error('Target.check_political_data_cache got unknown cached_obj type %s for key %s' % (type(cached_obj), key))
        # do it live
        if cached_obj:
            data = cached_obj
        else:
            data = {}
        try:
            offices = cached_obj.get('offices', [])
        except AttributeError:
            offices = []

    data['uid'] = adapted_key
    data['offices'] = offices
    return data
Esempio n. 5
0
 def testLegislatorDetails(self):
     """Legislator details"""
     _id = "DCL000012"
     full_name = "Marion Barry"
     self.assertEqual(
         pyopenstates.get_legislator(_id)["full_name"], full_name)
        for no in vote['no_votes']:
            if no['leg_id']:
                legislators.add(no['leg_id'])
                voteRecord[no['leg_id']] = 'N'

        for other in vote['other_votes']:
            if other['leg_id']:
                legislators.add(other['leg_id'])
                voteRecord[other['leg_id']] = 'NV'
        votes.append(voteRecord)

# Fetches information on each senator (name, party, district ..etc)
senators = []
for leg in legislators:
    try:
        fullLeg = pyopenstates.get_legislator(leg)
        party, district = None, None
        try:
            district = fullLeg['roles'][0]['district']
        except Exception as e:
            pass
        try:
            party = fullLeg['party'][:3]
        except Exception as e:
            jsonAsString = str(fullLeg)
            republican = 'Republican' in jsonAsString
            democrat = 'Democrat' in jsonAsString
            indepdent = 'Independent' in jsonAsString
            if republican and not democrat:
                party = 'Rep'
            if not republican and democrat:
Esempio n. 7
0
    def scrape(self):
        state = 'MN'
        session = self.jurisdiction.legislative_sessions[0]
        apiKey = 'd2c0db7e-6a6e-4606-a9b0-83c18e647ff6'
        pyopenstates.set_api_key(apiKey)
        bills_upper = pyopenstates.search_bills(state=state,
                                                chamber="upper",
                                                updated_since="2017-01-01")
        bills_lower = pyopenstates.search_bills(state=state,
                                                chamber="lower",
                                                updated_since="2017-01-01")

        for b in bills_lower:
            number = b['bill_id']
            title = b['title']
            bill_id = b['id']
            dbill = pyopenstates.get_bill(bill_id)
            url = dbill['sources'][0]['url']

            bill = Bill(identifier=number,
                        legislative_session=session['identifier'],
                        title=title,
                        classification=b['type'][0],
                        chamber='upper')
            bill.add_source(url)
            bill.add_identifier(bill_id, scheme='openstatesv1')

            subjects = b['subjects']
            for s in subjects:
                bill.add_subject(s)

            sponsors = dbill['sponsors']
            for sponsor in sponsors:
                if not sponsor['leg_id'] == None:
                    l = pyopenstates.get_legislator(sponsor['leg_id'])
                    full_name = l['full_name'].split(' ')
                    if len(full_name) == 3:
                        full_name.pop(1)
                    full_name = (' ').join(full_name)
                    primary = False
                    if sponsor['type'] == 'primary':
                        primary = True
                    try:
                        bill.add_sponsorship(name=full_name,
                                             classification=sponsor['type'],
                                             entity_type='person',
                                             primary=primary)
                    except:
                        pass

            actions = dbill['actions']
            for act in actions:
                action = act['action']
                actor = act['actor']
                date = tz.localize(datetime.strptime(act['date'], DATE_FORMAT))
                Action_Type = act['type']
                bill.add_action(action, date, chamber=actor)

            action_dates = dbill['action_dates']
            for act in action_dates.items():
                k, v = act[0], act[1]
                if '_' in k:
                    chamber = k.split('_')[1]
                elif k == 'signed':
                    chamber = 'executive'
                else:
                    chamber = None
                k.replace('_', ' ')
                if not v == None and not k in ['first', 'last']:
                    bill.add_action(k, tz.localize(v), chamber=chamber)
            yield bill

        for b in bills_upper:
            number = b['bill_id']
            title = b['title']
            bill_id = b['id']
            dbill = pyopenstates.get_bill(bill_id)
            url = dbill['sources'][0]['url']

            bill = Bill(identifier=number,
                        legislative_session=session['identifier'],
                        title=title,
                        classification=b['type'][0],
                        chamber='upper')
            bill.add_source(url)
            bill.add_identifier(bill_id, scheme='openstatesv1')

            subjects = b['subjects']
            for s in subjects:
                bill.add_subject(s)

            sponsors = dbill['sponsors']
            for sponsor in sponsors:
                if not sponsor['leg_id'] == None:
                    l = pyopenstates.get_legislator(sponsor['leg_id'])
                    full_name = l['full_name'].split(' ')
                    if len(full_name) == 3:
                        full_name.pop(1)
                    full_name = (' ').join(full_name)
                    primary = False
                    if sponsor['type'] == 'primary':
                        primary = True
                    try:
                        bill.add_sponsorship(name=full_name,
                                             classification=sponsor['type'],
                                             entity_type='person',
                                             primary=primary)
                    except:
                        pass

            actions = dbill['actions']
            for act in actions:
                action = act['action']
                actor = act['actor']
                date = tz.localize(datetime.strptime(act['date'], DATE_FORMAT))
                Action_Type = act['type']
                bill.add_action(action, date, chamber=actor)

            action_dates = dbill['action_dates']
            for act in action_dates.items():
                k, v = act[0], act[1]
                if '_' in k:
                    chamber = k.split('_')[1]
                elif k == 'signed':
                    chamber = 'executive'
                else:
                    chamber = None
                k.replace('_', ' ')
                if not v == None and not k in ['first', 'last']:
                    bill.add_action(k, tz.localize(v), chamber=chamber)
            yield bill