def sunlight_to_models(cls): politicians = congress.legislators(title='Rep') politicians = politicians + congress.legislators(title='Sen') for politician in politicians: new_politician, created_flag = Politician.objects.get_or_create(first_name=politician['firstname'], last_name=politician['lastname'], state=politician['state'], district=politician['district'], party=politician['party'], title=politician['title'], portrait_id=politician['bioguide_id']) if politician['twitter_id']: new_politician.add_twitter(politician['twitter_id']) else: print '%s does not have twitter' % new_politician
def send_notifications(self): sponsors = defaultdict(list) for bill in list(self.db.Bill.find({'processed': False})): sponsors[bill['sponsor_id']].append(bill) for sponsor_id, bills in sponsors.items(): sponsor = congress.legislators(bioguide_id=sponsor_id).next() name = "%s. %s %s" % (sponsor['title'], sponsor['first_name'], sponsor['last_name']) bill_count = len(bills) if bill_count == 1: msg = "%s sponsored a bill" % name else: msg = "%s sponsored %s bills" % (name, bill_count) notification = self.db.Notification() notification.type = '/legislator/sponsor/introduced' notification.message = msg notification.tags = {'and': ['/legislator/sponsor/introduction', '/legislators/%s' % sponsor_id]} notification.payload = { 'type': notification.type, 'legislator': sponsor_id, } if bill_count == 1: notification.payload['app_url'] = '/bills/%s' % bills[0]['bill_id'] else: notification.payload['app_url'] = '/legislators/%s/sponsored' % sponsor_id self.push_notification(notification)
def __init__(self): super(LegislativeScorecard, self).__init__() self.representatives = [] self.senators = [] self.scores = {} self.legislator_metadata = {} # grab bioguides for (varname, chamber) in ((self.senators, 'senate'), (self.representatives, 'house')): i = 0 while True: new_reps = congress.legislators(chamber=chamber, fields=','.join(['bioguide_id',] + LegislativeScorecard.FIELDS_TO_COLLECT), page=i) if new_reps is None: break for nr in new_reps: bioguide = nr['bioguide_id'] varname.append(bioguide) self.legislator_metadata[bioguide] = {} for k in LegislativeScorecard.FIELDS_TO_COLLECT: self.legislator_metadata[bioguide][k] = nr.get(k) i = i + 1 self.reset_scores()
def count_phrase_for_legislator(phrase, legislator_id, start_date, end_date): for cw_record in capitolwords.phrases_by_entity( "legislator", # We're getting all legislators # bioguide=legislator_id, phrase=phrase, # this word start_date=start_date, end_date=end_date, sort="count", # sorted by how much they say )[:]: legislator = congress.legislators( bioguide_id=cw_record['legislator'], # Look up this biogude (unique ID) for every fed. legislator all_legislators="true" # search retired legislators ) if len(legislator) >= 1: # If we were able to find the legislator legislator = legislator[0] # (this is a search, so it's a list) if cw_record['legislator'] == legislator_id: print("%s. %s said %s %s times" % ( legislator['title'], legislator['last_name'], phrase, int(cw_record['count'])) )
def count_phrase_for_legislator(phrase, legislator_id, start_date, end_date): for cw_record in capitolwords.phrases_by_entity( "legislator", # We're getting all legislators # bioguide=legislator_id, phrase=phrase, # this word start_date=start_date, end_date=end_date, sort="count", # sorted by how much they say # sort="legislator", # sorted by who said it )[:]: legislator = congress.legislators( bioguide_id=cw_record["legislator"], # Look up this biogude (unique ID) for every fed. legislator all_legislators="true", # search retired legislators ) if len(legislator) >= 1: # If we were able to find the legislator legislator = legislator[0] # (this is a search, so it's a list) if cw_record["legislator"] == legislator_id: # print("{0}. {1} said {2} {3} times".format( # legislator['title'], # legislator['last_name'], # phrase, # int(cw_record['count'])) # ) return (legislator["title"] + " " + legislator["last_name"], int(cw_record["count"])) # return (legislator['title'] + # ' ' +legislator['last_name'], # int(cw_record['count'])) return ("", 0)
def fetch(self, verbose=False, log=False): """ Compares and updates all Legislator objects against the data contained within the Sunlight Foundation's Congress API. The entire operation is enclosed in a transaction that is only committed if no exceptions are raised. """ if verbose and log: log.write("Fetching Legislators\n") with transaction.commit_manually(): try: for legislator in congress.legislators(): legislator_obj = self.get_or_create(bioguide_id=legislator["bioguide_id"])[0] for name, value in legislator.iteritems(): setattr(legislator_obj, name, value) legislator_obj.save() if verbose and log: log.write("- %s\n" % legislator_obj.fullname) except Exception as e: transaction.rollback() raise e else: transaction.commit()
def get_legistlator(last_name): results = congress.legislators(last_name=last_name) if results: return legistlator.Legistlator(results[0]) else: # TODO: throw exception return False
def getID(first_name,last_name): print "------------------------" print first_name, last_name person = congress.legislators(last_name=last_name,first_name=first_name) print person if not person: print '--------------->not a person' id = '' else: print '----------------is a person' person = person[0] id = str(person['bioguide_id']) return id
def getID(first_name, last_name): print "------------------------" print first_name, last_name person = congress.legislators(last_name=last_name, first_name=first_name) print person if not person: print '--------------->not a person' id = '' else: print '----------------is a person' person = person[0] id = str(person['bioguide_id']) return id
def send_notifications(self): sponsors = defaultdict(list) for bill in list(self.db.Bill.find({'processed': False})): sponsors[bill['sponsor_id']].append(bill) for sponsor_id, bills in sponsors.items(): sponsor = congress.legislators(bioguide_id=sponsor_id).next() name = "%s. %s %s" % (sponsor['title'], sponsor['first_name'], sponsor['last_name']) bill_count = len(bills) if bill_count == 1: msg = "%s sponsored a bill" % name else: msg = "%s sponsored %s bills" % (name, bill_count) notification = self.db.Notification() notification.type = '/legislator/sponsor/introduced' notification.message = msg notification.tags = { 'and': [ '/legislator/sponsor/introduction', '/legislators/%s' % sponsor_id ] } notification.payload = { 'type': notification.type, 'legislator': sponsor_id, } local_dt = delay_until_local() if local_dt: notification.scheduled_for = local_dt notification.scheduled_for_local = True if bill_count == 1: notification.payload[ 'app_url'] = '/bills/%s' % bills[0]['bill_id'] else: notification.payload[ 'app_url'] = '/legislators/%s/sponsored' % sponsor_id self.push_notification(notification)
def index(): input_age = request.args.get('birthday', '') legislators = congress.legislators(birthday__gte=input_age) if input_age else [] return render_template('base.html', legislators=legislators, birthday=input_age)
''' Created on Oct 12, 2013 @author: sungoh ''' import load_db, os from sunlight import congress working_dir = os.path.dirname(__file__) db_name = 'congress_tweets.db' database_dir = 'sqlite:///'+os.path.join(working_dir, db_name) congress_members = congress.legislators() member_database = load_db.LoadCongress(database_dir) member_database.insert_and_update(congress_members) """ twitter_database = load_db.LoadTweets(database_dir) senate_members_twitter = [member['twitter_id'] for member in congress_members if member['chamber'] == 'senate'] for senator_twitter_id in senate_members_twitter: twitter_database.insert_and_update(senator_twitter_id) """