示例#1
0
def loadroll(fn):
    roll = web.storage()
    roll.id = fn.split('/')[-1].split('.')[0]
    vote = xmltramp.load(fn)
    if vote['bill':]:
        b = vote.bill
        roll.bill_id = 'us/%s/%s%s' % (b('session'), b('type'), b('number'))
    else:
        roll.bill_id = None
    roll.type = str(vote.type)
    roll.question = str(vote.question)
    roll.required = str(vote.required)
    roll.result = str(vote.result)

    try:
        db.insert('roll', seqname=False, **roll)
    except IntegrityError:
        if not db.update('roll',
                         where="id=" + web.sqlquote(roll.id),
                         bill_id=roll.bill_id):
            print "\nMissing bill:", roll.bill_id
            raise NotDone

    with db.transaction():
        db.delete('vote', where="roll_id=$roll.id", vars=locals())
        for voter in vote['voter':]:
            rep = govtrackp(voter('id'))
            if rep:
                db.insert('vote',
                          seqname=False,
                          politician_id=rep,
                          roll_id=roll.id,
                          vote=fixvote(voter('vote')))
            else:
                pass  #@@!--check again after load_everyone
示例#2
0
def loadbill(fn, maplightid=None):
    bill = xmltramp.load(fn)
    d = bill2dict(bill)
    d.maplightid = maplightid
    
    try:
        bill_id = d.id
        db.insert('bill', seqname=False, **d)
    except IntegrityError:
        bill_id = d.pop('id')
        db.update('bill', where="id=" + web.sqlquote(bill_id), **d)
    
    positions = {}
    for vote in bill.actions['vote':]:
        if not vote().get('roll'): continue
        
        rolldoc = '/us/%s/rolls/%s%s-%s.xml' % (
          d.session, vote('where'), vote('datetime')[:4], vote('roll'))
        roll = xmltramp.load(GOVTRACK_CRAWL + rolldoc)
        for voter in roll['voter':]:
            positions[govtrackp(voter('id'))] = fixvote(voter('vote'))

    if None in positions: del positions[None]
    with db.transaction():
        db.delete('position', where='bill_id=$bill_id', vars=locals())
        for p, v in positions.iteritems():
            db.insert('position', seqname=False, 
              bill_id=bill_id, politician_id=p, vote=v)
示例#3
0
 def POST_delete(self, pid):
     with db.transaction():
         title = db.select('petition', what='title', where='id=$pid', vars=locals())[0].title
         db.delete('signatory', where='petition_id=$pid', vars=locals())
         db.delete('petition', where='id=$pid', vars=locals())
     helpers.set_msg('Petition "%s" deleted' % (title))
     raise web.seeother('/')
示例#4
0
def loadroll(fn):
    roll = web.storage()
    roll.id = fn.split('/')[-1].split('.')[0]
    vote = xmltramp.load(fn)
    if vote['bill':]:
        b = vote.bill
        roll.bill_id = 'us/%s/%s%s' % (b('session'), b('type'), b('number'))
    else:
        roll.bill_id = None
    roll.type = str(vote.type)
    roll.question = str(vote.question)
    roll.required = str(vote.required)
    roll.result = str(vote.result)
    
    try:
        db.insert('roll', seqname=False, **roll)
    except IntegrityError:
        if not db.update('roll', where="id=" + web.sqlquote(roll.id), bill_id=roll.bill_id):
            print "\nMissing bill:", roll.bill_id
            raise NotDone
    
    with db.transaction():
        db.delete('vote', where="roll_id=$roll.id", vars=locals())
        for voter in vote['voter':]:
            rep = govtrackp(voter('id'))
            if rep:
                db.insert('vote', seqname=False, 
                  politician_id=rep, roll_id=roll.id, vote=fixvote(voter('vote')))
            else:
                pass #@@!--check again after load_everyone
示例#5
0
def loadbill(fn, maplightid=None):
    bill = xmltramp.load(fn)
    d = bill2dict(bill)
    d.maplightid = maplightid

    try:
        bill_id = d.id
        db.insert('bill', seqname=False, **d)
    except IntegrityError:
        bill_id = d.pop('id')
        db.update('bill', where="id=" + web.sqlquote(bill_id), **d)

    positions = {}
    for vote in bill.actions['vote':]:
        if not vote().get('roll'): continue

        rolldoc = '/us/%s/rolls/%s%s-%s.xml' % (
            d.session, vote('where'), vote('datetime')[:4], vote('roll'))
        roll = xmltramp.load(GOVTRACK_CRAWL + rolldoc)
        for voter in roll['voter':]:
            positions[govtrackp(voter('id'))] = fixvote(voter('vote'))

    if None in positions: del positions[None]
    with db.transaction():
        db.delete('position', where='bill_id=$bill_id', vars=locals())
        for p, v in positions.iteritems():
            db.insert('position',
                      seqname=False,
                      bill_id=bill_id,
                      politician_id=p,
                      vote=v)
示例#6
0
def load():
    outdb = {}
    done = set()
    with db.transaction():
        db.delete('earmark_sponsor', '1=1')
        db.delete('earmark', '1=1')
        for e in earmarks.parse_file(earmarks.EARMARK_FILE):
            de = dict(e)
            de['id'] = web.intget(de['id'])
            if not de['id'] or de['id'] in done: continue # missing the ID? come on!
            if isinstance(de['house_request'], basestring): continue # CLASSIFIED

            for k in de: de[k] = cleanrow(de[k])
            for x in ['house_member', 'house_state', 'house_party', 'senate_member', 'senate_state', 'senate_party', 'district']:
                de.pop(x)
            
            de['recipient_stem'] = tools.stemcorpname(de['intended_recipient'])
            try:
                db.insert('earmark', seqname=False, **de)
            except:
                pprint(de)
                raise
            done.add(de['id'])
        
    reps_not_found = set()
    for e in earmarks.parse_file(earmarks.EARMARK_FILE):
        for rawRequest, chamber in zip([e.house_request, e.senate_request],[e.house_member, e.senate_member]):
            for rep in chamber:
                if rep.lower() not in lastname2rep:
                    #@@ should work on improving quality
                    reps_not_found.add(rep)
                else:
                    rep = lastname2rep[rep.lower()]
                    if e.id in done: 
                        try:
                            db.insert('earmark_sponsor', seqname=False, earmark_id=e.id, politician_id=rep)
                        except:
                            print "Couldn't add %s as sponsor to earmark %d" %(rep, e.id)
                    outdb.setdefault(rep, {
                      'amt_earmark_requested': 0,
                      'n_earmark_requested': 0,
                      'n_earmark_received': 0,
                      'amt_earmark_received': 0
                    })
                    outdb[rep]['n_earmark_requested'] += 1
                    requested = rawRequest or e.final_amt
                    if not isinstance(requested, float):
                        requested = e.final_amt
                    if requested:
                        outdb[rep]['amt_earmark_requested'] += requested
                    if isinstance(e.final_amt, float) and e.final_amt:
                        outdb[rep]['n_earmark_received'] += 1
                        outdb[rep]['amt_earmark_received'] += e.final_amt
    
    print "Did not find",len(reps_not_found),"reps:", pformat(reps_not_found)
    for rep, d in outdb.iteritems():
        db.update('politician', where='id=$rep', vars=locals(), **d)
示例#7
0
def load_phones(phones):
    with db.transaction():
        db.delete('pol_phones', '1=1')
        for k, v in phones.items():
            polid, city = k
            db.insert('pol_phones',
                      seqname=False,
                      politician_id=polid,
                      city=city,
                      **v)
示例#8
0
文件: bills.py 项目: kragen/watchdog
def main(session):
    done = anydbm.open('.bills', 'c')
    markdone = makemarkdone(done)
        
    db.delete('position', '1=1')
    for fn in sorted(glob.glob(GOVTRACK_CRAWL+'/us/%s/bills/*.xml' % session)):
        print >>sys.stderr,'\r  %-25s    ' % fn,; sys.stderr.flush()
        markdone(loadbill)(fn)
    schema.Position.create_indexes()

    db.delete('vote', '1=1')
    for fn in sorted(glob.glob(GOVTRACK_CRAWL+'/us/%s/rolls/*.xml' % session)):
        print >>sys.stderr,'\r  %-25s    ' % fn,; sys.stderr.flush()
        markdone(loadroll)(fn)
    print >>sys.stderr, '\r' + ' '*72
示例#9
0
def load_wyr():
    with db.transaction():
        db.delete('pol_contacts', where='1=1')
        for pol, data in wyr.iteritems():
                if data['contacttype'] not in types.keys(): 
                    continue

                if data['contacttype'] == 'wyr':
                    contact = 'https://writerep.house.gov/writerep/welcome.shtml'
                else:
                    contact = data['contact']    
            
                d = {'politician_id':pol,
                        'contact':contact,
                        'contacttype': types[data['contacttype']],
                        'captcha': data['captcha']
                       }
                try:
                    db.insert('pol_contacts', seqname=False, **d)
                except:
                    continue
示例#10
0
def load_wyr():
    with db.transaction():
        db.delete('pol_contacts', where='1=1')
        for pol, data in wyr.iteritems():
            if data['contacttype'] not in types.keys():
                continue

            if data['contacttype'] == 'wyr':
                contact = 'https://writerep.house.gov/writerep/welcome.shtml'
            else:
                contact = data['contact']

            d = {
                'politician_id': pol,
                'contact': contact,
                'contacttype': types[data['contacttype']],
                'captcha': data['captcha']
            }
            try:
                db.insert('pol_contacts', seqname=False, **d)
            except:
                continue
示例#11
0
def load_phones(phones):
    with db.transaction():
        db.delete('pol_phones', '1=1')
        for k, v in phones.items():
            polid, city = k
            db.insert('pol_phones', seqname=False, politician_id=polid, city=city, **v)
示例#12
0
        # lob_contribution table
        def insert_contribution(con):
            c = {}
            for z, val in con.items():
                if z == 'amount': val = int(float(val))
                if z in lob_contribution: c[lob_contribution[z]] = val
                if z == 'recipientName': c['politician_id'] = findPol(val)
            db.insert('lob_contribution',
                      seqname=False,
                      filing_id=x['file_id'],
                      **c)

        if 'contributions' in x:
            if isinstance(x['contributions'], list):
                for con in x['contributions']:
                    insert_contribution(con)
            else:
                insert_contribution(x['contributions'])


if __name__ == "__main__":
    with db.transaction():
        print "Deleting old data from lob_* tables."
        db.delete('lob_pac_filings', where='1=1')
        db.delete('lob_contribution', where='1=1')
        db.delete('lob_filing', where='1=1')
        db.delete('lob_pac', where='1=1')
        db.delete('lob_person', where='1=1')
        db.delete('lob_organization', where='1=1')
        load_house_lobbyists()
示例#13
0
def main():
    with db.transaction():
        db.update('politician', votesmartid=None, where='1=1')
        db.delete('congress', where="congress_num='-1'")
        load_votesmart()
示例#14
0
def main():
    with db.transaction():
        db.update('politician', votesmartid=None, where='1=1')
        db.delete('congress', where="congress_num='-1'")
        load_votesmart()
示例#15
0
            else: 
                insert_pac(x['pacs'])

        # lob_contribution table
        def insert_contribution(con):
            c = {}
            for z, val in con.items():
                if z == 'amount': val = int(float(val))
                if z in lob_contribution: c[lob_contribution[z]] = val
                if z == 'recipientName': c['politician_id']=findPol(val)
            db.insert('lob_contribution', seqname=False, filing_id=x['file_id'], **c)
        if 'contributions' in x:
            if isinstance(x['contributions'], list):
                for con in x['contributions']:
                    insert_contribution(con)
            else: 
                insert_contribution(x['contributions'])


if __name__ == "__main__":
    with db.transaction():
        print "Deleting old data from lob_* tables."
        db.delete('lob_pac_filings',where='1=1')
        db.delete('lob_contribution',where='1=1')
        db.delete('lob_filing',where='1=1')
        db.delete('lob_pac',where='1=1')
        db.delete('lob_person',where='1=1')
        db.delete('lob_organization',where='1=1')
        load_house_lobbyists()

示例#16
0
def load_handshakes(handshakes):
    with db.transaction():
        db.delete('handshakes', '1=1')
        for h in handshakes:
            db.insert('handshakes', seqname=False, **h)            
示例#17
0
 def POST_unsign(self, pid):
     i = web.input()
     db.delete('signatory', where='petition_id=$pid and user_id=$i.user_id', vars=locals())
     msg = 'Your signature has been removed for this petition.'
     helpers.set_msg(msg)
     raise web.seeother('/%s' % pid)
示例#18
0
def load():
    outdb = {}
    done = set()
    with db.transaction():
        db.delete('earmark_sponsor', '1=1')
        db.delete('earmark', '1=1')
        for e in earmarks.parse_file(earmarks.EARMARK_FILE):
            de = dict(e)
            de['id'] = web.intget(de['id'])
            if not de['id'] or de['id'] in done:
                continue  # missing the ID? come on!
            if isinstance(de['house_request'], basestring):
                continue  # CLASSIFIED

            for k in de:
                de[k] = cleanrow(de[k])
            for x in [
                    'house_member', 'house_state', 'house_party',
                    'senate_member', 'senate_state', 'senate_party', 'district'
            ]:
                de.pop(x)

            de['recipient_stem'] = tools.stemcorpname(de['intended_recipient'])
            try:
                db.insert('earmark', seqname=False, **de)
            except:
                pprint(de)
                raise
            done.add(de['id'])

    reps_not_found = set()
    for e in earmarks.parse_file(earmarks.EARMARK_FILE):
        for rawRequest, chamber in zip([e.house_request, e.senate_request],
                                       [e.house_member, e.senate_member]):
            for rep in chamber:
                if rep.lower() not in lastname2rep:
                    #@@ should work on improving quality
                    reps_not_found.add(rep)
                else:
                    rep = lastname2rep[rep.lower()]
                    if e.id in done:
                        try:
                            db.insert('earmark_sponsor',
                                      seqname=False,
                                      earmark_id=e.id,
                                      politician_id=rep)
                        except:
                            print "Couldn't add %s as sponsor to earmark %d" % (
                                rep, e.id)
                    outdb.setdefault(
                        rep, {
                            'amt_earmark_requested': 0,
                            'n_earmark_requested': 0,
                            'n_earmark_received': 0,
                            'amt_earmark_received': 0
                        })
                    outdb[rep]['n_earmark_requested'] += 1
                    requested = rawRequest or e.final_amt
                    if not isinstance(requested, float):
                        requested = e.final_amt
                    if requested:
                        outdb[rep]['amt_earmark_requested'] += requested
                    if isinstance(e.final_amt, float) and e.final_amt:
                        outdb[rep]['n_earmark_received'] += 1
                        outdb[rep]['amt_earmark_received'] += e.final_amt

    print "Did not find", len(reps_not_found), "reps:", pformat(reps_not_found)
    for rep, d in outdb.iteritems():
        db.update('politician', where='id=$rep', vars=locals(), **d)