summaryrefslogtreecommitdiff
path: root/scrapers/at.py
blob: 1af9894e32d6cc30b12a52b1c38de3c239787181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import datetime
import json
import math
from multiprocessing.dummy import Pool as ThreadPool

import requests

sess = requests.session()

# API documentation:
# https://data.bka.gv.at/ris/api/v2.5/applications/bundesnormen

def fetch_page(page):
    res = sess.get('https://data.bka.gv.at/ris/api/v2.5/bundesnormen', params=dict(
        Seitennummer=page,
        DokumenteProSeite='OneHundred',
        FassungVom=datetime.datetime.today().strftime('%Y-%m-%d'),
        Abschnitt_Von=1
    ))
    print(res.request.url)
    data = res.json()['OgdSearchResult']

    if 'Error' in data:
        print(data)
        return

    return data['OgdDocumentResults']

pages = []
first = fetch_page(1)
pages.append(first)
page_count = math.ceil(int(first['Hits']['#text']) / 100)

for page in ThreadPool(8).map(fetch_page, range(2, page_count+1)):
    pages.append(page)

normen = {}

for page in pages:
    for result in page['OgdDocumentReference']:
        info = result['Data']['Metadaten']['Bundes-Landesnormen']
        if info['Typ'] in ('K', 'K (Geltungsbereich)'):
            continue
        if info['Typ'].startswith('Vertrag -'):
            continue
        data = dict(
            title=info['Kurztitel'].strip(),
            url=info['GesamteRechtsvorschriftUrl'],
        )
        if 'Abkuerzung' in info:
            data['abbr'] = info['Abkuerzung'].strip()
            data['redir'] = data['abbr'].lower()\
                .replace(')', '')\
                .replace('(', '')\
                .replace(' – ', '-')\
                .replace(' ', '-')\
                .replace('\xa0', '-')\
                .replace('ä', 'ae')\
                .replace('ü', 'ue')\
                .replace('ö', 'oe')\
                .replace('ß', 'ss')
        normen[info['Gesetzesnummer']] = data

with open('laws/at.json', 'w') as f:
    json.dump(list(normen.values()), f, indent=2, ensure_ascii=False)