def load_legislators(): """Take information from Sunlight Fdn csv file; calls to Sunlight API for the first election""" #for first year elected. I have a function to do the API call for me, where do I define function? #need to figure out how to not hardcode my API key! Look at Twitter bot file_open = open("./src/legislators_test.csv") for index, line in enumerate(file_open): #enumerate gives tuple of index of line & the line info. use to keep track of where we are & commmit every so often if index > 0: #skip first line that is a header legislators = line.rstrip().split(",") #checks if member is currently in office, 0 means no. if int(legislators[9]) == 1: crp_id = legislators[20] current_legislator_dict.setdefault(crp_id, True) #fill current members dict. so can only pull those values in our cont. table bioguide_id = legislators[16] first = legislators[1].decode("UTF-8") last = legislators[3].decode("UTF-8") nickname = legislators[5].decode("UTF-8") suffix = legislators[4] title = legislators[0] state = legislators[7] party = legislators[6] if legislators[0] == "Sen": chamber = "Senate" else: chamber = "House" twitter_id = legislators[21] facebook_id = legislators[24] if legislators[0] =="Sen": sen_rank = legislators[8] #only req. for senators district = None else: district = int(legislators[8]) #only for house members sen_rank = None off_website = legislators[13] open_cong_url = legislators[22] pict_link = "https://theunitedstates.io/images/congress/225x275/"+legislators[16]+".jpg" first_elected = get_first_term_year(crp_id, os.environ['SUNLIGHT_API_KEY']) #API call to SLF to get the year temp_legislator_object = Legislator(leg_id=crp_id, bioguide_id=bioguide_id, first=first, last=last, nickname=nickname, suffix=suffix, title=title, state=state, party=party, chamber=chamber, twitter_id=twitter_id, facebook_id=facebook_id, district=district, sen_rank=sen_rank, off_website=off_website, open_cong_url=open_cong_url, pict_link=pict_link, first_elected=first_elected) # object_list = db.session.add(temp_legislator_object) return temp_legislator_object
def test_get_first_term_year(self): assert get_first_term_year("N00007335", os.environ["SUNLIGHT_API_KEY"]) == 1993
def update_legislators(): """ input = file handle from which to look for updates to legislators output = nothing checks for new legislators. they can be 1) filling empty seat or 2) replacing an occupied seat """ currently_loaded = db.session.query(Legislator.leg_id).all() #list of tuples => [(leg_id (known as crp_id in sunlight data,), (leg_id,)] loaded_ids = {tup[0]: True for tup in currently_loaded} # use this to keep track of currently serving members, we can then check against ones in db. # if its in our db but not in this list, then we need to delete from our db current_mem_ids = {} apikey = os.environ['SUNLIGHT_API_KEY'] query_params = { 'apikey': apikey, 'per_page': 'all', } endpoint = 'https://congress.api.sunlightfoundation.com/legislators/' # gets all current members of congress. need to find ones we don't already have in our system response = (requests.get(endpoint, params=query_params)).json() for entry in response['results']: crp_id = entry['crp_id'] bioguide_id = entry['bioguide_id'] first = entry['first_name'] last = entry['last_name'] nickname = entry['nickname'] suffix = entry['name_suffix'] title = entry['title'] state = entry['state'] party = entry['party'] chamber = entry['chamber'].capitalize() twitter_id = entry.get('twitter_id', None) facebook_id = entry.get('facebook_id', None) if chamber =="Senate": sen_rank = entry['state_rank'].capitalize() district = None else: district = int(entry['district']) #only for house members sen_rank = None off_website = entry['website'] open_cong_url = None pict_link = "https://theunitedstates.io/images/congress/225x275/"+bioguide_id+".jpg" first_elected = get_first_term_year(crp_id, apikey) #API call to SLF to get the year current_mem_ids[crp_id] = True # if the member is not already loaded, load it if crp_id not in loaded_ids: temp_legislator_object = Legislator(leg_id=crp_id, bioguide_id=bioguide_id, first=first, last=last, nickname=nickname, suffix=suffix, title=title, state=state, party=party, chamber=chamber, twitter_id=twitter_id, facebook_id=facebook_id, district=district, sen_rank=sen_rank, off_website=off_website, open_cong_url=open_cong_url, pict_link=pict_link, first_elected=first_elected) db.session.add(temp_legislator_object) # if member is in our db but isn't current member, remove from db for x in loaded_ids.keys(): if x not in current_mem_ids: member = Legislator.query.filter_by(leg_id = x).first() # get all contributions to that member and delete from table contributions = Contrib_leg.query.filter_by(leg_id= x).all() for item in contributions: db.session.delete(item) db.session.delete(member) db.session.commit()