def add_specific_details(self, obj): legid = obj.legid data = openstates.legislator_detail(legid) obj.homepage = data.get('url','') # The primary key isn't actually set to auto-increment # so we're going to recreated starting with the largest # key. This makes it safe run this script over and over # but is mildly offensive pk = OfficialOffices.objects.all().aggregate(Max('office_pk'))['office_pk__max'] or 0 pk += 1 OfficialOffices.objects.filter(office_legid=legid).delete() ## There isn't actually a FK relationship ## to office, so just save it here for office in data['offices']: o = OfficialOffices() o.office_pk = pk o.office_legid=legid o.address=office['address'] o.phone=office['phone'] o.name=office['name'] o.save() pk += 1
def update_active_status(): legislators_in_db = Officials.objects.all() print len(legislators_in_db) for counter, leg in enumerate(legislators_in_db): if counter % 5 == 0: print "Updated Active status of %s Legislators so far" % (counter) openstates_detail = openstates.legislator_detail(leg.legid) leg.active = openstates_detail['active'] leg.save()
def lawmaker(legid): form = RegistrationForm(request.form) # Get lawmaker detail from Sunlight import sunlight import json from sunlight import openstates id_lm_json = openstates.legislator_detail(legid) return render_template("leg_detail.html", legid=legid, id_lm_json=id_lm_json, user=current_user, form=form)
def get_offices(self): leg_info = openstates.legislator_detail(self.legid) try: self.email = leg_info['email'] except: self.email = '' try: self.offices = leg_info['offices'] except: self.offices = ''
def get_offices(legid_to_get): leg_info = openstates.legislator_detail(legid_to_get) try: email = leg_info['email'] except: email = '' try: offices = leg_info['offices'] except: offices = '' return offices, email
def add_addresses_to_db(): legids = get_leg_ids() print "Need to download addresses for %s Legislators" % (len(legids)) counter = 0 for legid_to_get in legids: counter += 1 if counter % 20 == 0: print "Finished gathering addresses for %s Legislators" % (counter) try: leg_info = openstates.legislator_detail(legid_to_get) except: print "Could not get OpenStates information for legid:%s" % (legid_to_get) continue for office in leg_info['offices']: new_office = offices(office_legid=legid_to_get, address=office['address'], phone=office['phone'], name=office['name']) session.add(new_office) session.commit()
def add_addresses_to_db(): OfficialOffices.objects.all().delete() counter = 0 for off in Officials.objects.all(): counter += 1 if counter % 20 == 0: print "Finished gathering addresses for %s Legislators" % (counter) try: leg_info = openstates.legislator_detail(off.legid) except: print "Could not get OpenStates information for legid:%s\nname:%s" % (off.legid, off.fullname) continue for office in leg_info['offices']: new_office = OfficialOffices(office_legid=off.legid, address=office['address'], phone=office['phone'], name=office['name']) new_office.save()
def detail(self, leg_id): self.metadata logging.debug('Fetching legislator %r' % id) return openstates.legislator_detail(leg_id)
def load_legislator(self, lawmaker): data = openstates.legislator_detail(lawmaker['id']) self.stdout.write(u'Loading {0} {1}...'.format( data['first_name'], data['last_name'])) if 'party' in data: party = Party.objects.get(name=data['party']) else: party = None if 'chamber' in data: chamber = Chamber.objects.get( name=u'Texas Senate' if data['chamber'] == 'upper' else u'Texas House' ) else: chamber = None if 'district' in data: if data['district']: district = int(data['district']) else: district = None else: district = None if 'url' in data: profile_url = data['url'] else: profile_url = None if 'offices' in data: for entry in data['offices']: if entry['type'] == 'capitol': capitol_office = entry break else: capitol_office = None for entry in data['offices']: if entry['type'] == 'district': district_office = entry break else: district_office = None if capitol_office: capitol_address = capitol_office['address'] capitol_phone = capitol_office['phone'] if capitol_phone: capitol_phone = capitol_phone.replace('(', '') capitol_phone = capitol_phone.replace(') ', '-') else: capitol_address = None capitol_phone = None if district_office: district_address = district_office['address'] district_phone = district_office['phone'] if district_phone: district_phone = district_phone.replace('(', '') district_phone = district_phone.replace(') ', '-') else: district_address = None district_phone = None if data['id'] in self.tribune_match: match = self.tribune_match[data['id']] tribune_city = match['city'] tribune_slug = match['slug'] tribune_photo = match['photo'] tribune_room = match['room'] if match['room'] else None else: tribune_city = None tribune_slug = None tribune_photo = None tribune_room = None if 'active' in data: active = data['active'] else: active = False legislator, created = Legislator.objects.update_or_create( openstates_id=data['leg_id'], defaults={ 'first_name': data['first_name'], 'middle_name': data['middle_name'], 'last_name': data['last_name'], 'party': party, 'chamber': chamber, 'district': district, 'profile_url': profile_url, 'active': active, 'capitol_address': capitol_address, 'capitol_phone': capitol_phone, 'district_address': district_address, 'district_phone': district_phone, 'tribune_city': tribune_city, 'tribune_slug': tribune_slug, 'tribune_photo': tribune_photo, 'tribune_room': tribune_room, } )
def get_leg_ids(): legids = [] query = session.query(official_info).all() for leg in query: legids.append(leg.legid) return legids def add_addresses_to_db(): legids = get_leg_ids() print "Need to download addresses for %s Legislators" % (len(legids)) counter = 0 for legid_to_get in legids: counter += 1 if counter % 20 == 0: print "Finished gathering addresses for %s Legislators" % (counter) try: leg_info = openstates.legislator_detail(legid_to_get) except: print "Could not get OpenStates information for legid:%s" % (legid_to_get) continue for office in leg_info['offices']: new_office = offices(office_legid=legid_to_get, address=office['address'], phone=office['phone'], name=office['name']) session.add(new_office) session.commit() if __name__ == '__main__': add_addresses_to_db() pp.pprint(openstates.legislator_detail('PAL000001'))