def process_bill(bill_uri): """ Load a bill into the database, sending out alerts for new actions. For each bill: - get the complete JSON representation, creating or updating an entry in the database - loop through actions, adding new ones to the database (see below on how to make actions unique) - fetch and add/update bill subjects - fetch and add/update bill cosponsors """ try: nyt_bill = nyt.fetch(bill_uri) except ValueError: # sometimes there's bad JSON # this should be logged print '\nBill %s failed to load' % bill_uri return bill_id = nyt_bill['bill'].replace('.', '').lower() try: bill = Bill.objects.get(congress=int(nyt_bill['congress']), number=bill_id) except Bill.DoesNotExist: sponsor = Legislator.objects.get(nyt_uri__iexact=nyt_bill['sponsor_uri']) bill = Bill.objects.create( congress=int(nyt_bill['congress']), number=bill_id, nyt_uri = bill_uri, title = nyt_bill['title'], introduced_date = parse_date(nyt_bill['introduced_date']), sponsor = sponsor, ) set_bill_actions(bill, nyt_bill['actions']) bill.cosponsors = bill._get_cosponsors(save=True) subjects = bill._update_subjects()
def set_bill_actions(bill, actions): """ Given a bill and a list of actions (dict) add new ones to the bill """ NEW_ACTIONS = [] # make a copy before we do anything else # reverse orders so oldest actions are first actions = reversed(list(actions)) # set datetimes to datetime objects # then check for existence in the db for i, action in enumerate(actions): action['datetime'] = parse_date(action['datetime']) action['id'] = u"%s-%s" % (bill.id, i) action['bill'] = bill action['index'] = i a, created = BillAction.objects.get_or_create(**action) if created: NEW_ACTIONS.append(a)