コード例 #1
0
def load_transcript(engine, wp, session, incremental=True):
    url = URL % (wp, session)
    Speech = sl.get_table(engine, 'speech')
    if incremental and sl.find_one(engine, Speech,
        source_url=url, matched=True):
        return True
    if '404 Seite nicht gefunden' in fetch(url):
        return False
    sio = fetch_stream(url)
    if sio is None:
        return False
    log.info("Loading transcript: %s/%s" % (wp, session))
    seq = 0
    parser = SpeechParser(engine, sio)
    for contrib in parser:
        if not len(contrib['text'].strip()):
            continue
        contrib['sitzung'] = session
        contrib['sequence'] = seq
        contrib['wahlperiode'] = wp
        contrib['source_url'] = url
        contrib['matched'] = True
        sl.upsert(engine, Speech, contrib, 
                  unique=['sequence', 'sitzung', 'wahlperiode'])
        seq += 1
    if parser.missing_recon:
        sl.upsert(engine, Speech, {
                    'matched': False,
                    'sitzung': session,
                    'wahlperiode': wp
            }, unique=['sitzung', 'wahlperiode'])

    return True
コード例 #2
0
def load_vote(url, engine, incremental=True):
    Vote = sl.get_table(engine, 'abstimmung')
    if incremental and sl.find_one(engine, Vote, source_url=url):
        log.info("%s is done, skipping.", url)
        return
    fh, path = tempfile.mkstemp('.pdf')
    fo = open(path, 'wb')
    fo.write(fetch(url))
    fo.close()
    xml = pdftoxml(path)
    handle_xml(xml, engine, url)
コード例 #3
0
ファイル: dip.py プロジェクト: csenger/offenesparlament
def scrape_ablauf(url, engine, wahlperiode=17):
    wahlperiode = str(wahlperiode)
    #if not hasattr(tl, 'engine'):
    #    tl.engine = etl_engine()
    #engine = tl.engine
    Ablauf = sl.get_table(engine, 'ablauf')

    key = url.rsplit('/', 1)[-1].split('.')[0]
    a = sl.find_one(engine, Ablauf, key=key, 
                    bt_wahlperiode=wahlperiode)
    if a is not None and a['abgeschlossen']:
        log.info("SKIPPING: %s", a['titel'])
        return
    if a is None:
        a = {}
    a['key'] = key
    doc = inline_xml_from_page(fetch(url))
    if doc is None: 
        raise NoContentException()
    
    a['wahlperiode'] = wahlperiode
    a['bt_wahlperiode'] = wahlperiode
    if doc.findtext("WAHLPERIODE"):
        wahlperiode != doc.findtext("WAHLPERIODE")
    
    a['ablauf_id'] = "%s/%s" % (wahlperiode, key)
    a['typ'] = doc.findtext("VORGANGSTYP")
    a['titel'] = doc.findtext("TITEL")

    if not a['titel'] or not len(a['titel'].strip()):
        raise NoContentException()

    if '\n' in a['titel']:
        t, k = a['titel'].rsplit('\n', 1)
        k = k.strip()
        if k.startswith('KOM') or k.startswith('SEK'):
            a['titel'] = t

    a['initiative'] = doc.findtext("INITIATIVE")
    a['stand'] = doc.findtext("AKTUELLER_STAND")
    a['signatur'] = doc.findtext("SIGNATUR")
    a['gesta_id'] = doc.findtext("GESTA_ORDNUNGSNUMMER")
    a['eu_dok_nr'] = doc.findtext("EU_DOK_NR")
    a['abstrakt'] = doc.findtext("ABSTRAKT")
    a['sachgebiet'] = doc.findtext("SACHGEBIET")
    a['zustimmungsbeduerftig'] = doc.findtext("ZUSTIMMUNGSBEDUERFTIGKEIT")
    a['source_url'] = url
    #a.schlagworte = []
    Schlagwort = sl.get_table(engine, 'schlagwort')
    for sw in doc.findall("SCHLAGWORT"):
        wort = {'wort': sw.text, 'key': key, 'wahlperiode': wahlperiode}
        sl.upsert(engine, Schlagwort, wort, unique=wort.keys())
    log.info("Ablauf %s: %s",key, a['titel'])
    a['titel'] = a['titel'].strip().lstrip('.').strip()
    a = expand_dok_nr(a)
    a['abgeschlossen'] = DIP_ABLAUF_STATES_FINISHED.get(a['stand'], False)
    if 'Originaltext der Frage(n):' in a['abstrakt']:
        _, a['abstrakt'] = a['abstrakt'].split('Originaltext der Frage(n):', 1)

    Referenz = sl.get_table(engine, 'referenz')
    for elem in doc.findall("WICHTIGE_DRUCKSACHE"):
        link = elem.findtext("DRS_LINK")
        hash = None
        if link is not None and '#' in link:
            link, hash = link.rsplit('#', 1)
        dokument = dokument_by_id(elem.findtext("DRS_HERAUSGEBER"), 
                'drs', elem.findtext("DRS_NUMMER"), link=link)
        dokument['text'] = elem.findtext("DRS_TYP")
        dokument['seiten'] = hash
        dokument['wahlperiode'] = wahlperiode
        dokument['ablauf_key'] = key
        sl.upsert(engine, Referenz, dokument, unique=[
            'link', 'wahlperiode', 'ablauf_key', 'seiten'
            ])

    for elem in doc.findall("PLENUM"):
        link = elem.findtext("PLPR_LINK")
        if link is not None and '#' in link:
            link, hash = link.rsplit('#', 1)
        dokument = dokument_by_id(elem.findtext("PLPR_HERAUSGEBER"), 
                'plpr', elem.findtext("PLPR_NUMMER"), link=link)
        dokument['text'] = elem.findtext("PLPR_KLARTEXT")
        dokument['seiten'] = elem.findtext("PLPR_SEITEN")
        dokument['wahlperiode'] = wahlperiode
        dokument['ablauf_key'] = key
        sl.upsert(engine, Referenz, dokument, unique=[
            'link', 'wahlperiode', 'ablauf_key', 'seiten'
            ])

    sl.upsert(engine, Ablauf, a, unique=['key', 'wahlperiode'])
    scrape_activities(a, doc, engine)
    engine.dispose()