aboutsummaryrefslogtreecommitdiffstats
path: root/scrapersources/postliste-aas-kommune
blob: 8af169992f8ef36528874275ac2e08a710562bb4 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- coding: utf-8 -*-
# YAML-tagger:
#  Type: kommune
#  Status: finished
#  Name: Ås kommune
#  Format: HTML
#  Datatype:
#  Vendor:
#  Run: daily
#  Missingfields: journalseqnr, journalyear, journalid
#  Publish duration: 3 months

import scraperwiki
import urllib2
import lxml.html
import re
import dateutil.parser
from collections import deque
import datetime
from dateutil.relativedelta import relativedelta

scraperwiki.scrape("http://www.as.kommune.no/offentlig-journal-og-innsyn-i-arkiv.352152.no.html")
postlistelib=scraperwiki.swimport('postliste-python-lib')

agency = u'Ås kommune'
baseurl = "http://www.as.kommune.no"

print "Fetching public journal for %s!" % agency

parser = postlistelib.JournalParser(agency=agency)

fieldmap = {
    'Tilhører sak:' : 'casedesc',
    'Dokumentdato:' : 'docdate',
    'Tilgangskode:' : 'exemption',
    'Dokumenttype:' : 'doctype',
    'Ansvarlig enhet:' : 'saksansvarligenhet',
    }

typemap = {
    u'Inngående dokument (I)' : 'I',
    u'Utgående dokument (U)' : 'U',
    }

class NoDataEntries(LookupError):
    pass

def expand_year(year):
    year = int(year)
    if year > 50:
        year = year + 1900
    else:
        year = year + 2000
    return year

def parse_day_html(parser, datastore, dayurl, html):
    root = lxml.html.fromstring(html)
#    count = 0
    for tr in root.cssselect("table.postjournal > tr"):
        data = {
            'agency' : parser.agency,
            'scrapedurl' : dayurl,
            'scrapestamputc' : datetime.datetime.now()
            }
#        count = count + 1
#        print "=========== %d =============" % count
#        print tr.text_content()
        doknrroot = tr.cssselect("td div.doknr")
        if not doknrroot:
            # No records found, just return
            msg = "No entries found in %s" % dayurl
            print msg
            raise NoDataEntries(msg)
        arkivsaksref = doknrroot[0].text_content().strip()
        caseyear = 0
        caseseqnr = 0
        casedocseq = 0
        caseid = 'unknown'
        matchObj = re.match( r'(\d+)/(\d+)\s*-\s*(\d+)$', arkivsaksref, re.M|re.I)
        if matchObj:
            caseyear = int(matchObj.group(1))
            data['caseseqnr'] = int(matchObj.group(2))
            data['casedocseq'] = int(matchObj.group(3))
            data['caseyear']  = expand_year(caseyear)
            data['caseid'] = str(data['caseyear']) + "/" + str(data['caseseqnr'])
            data['arkivsaksref'] = arkivsaksref
        else:
            print "error: really broken Arkivsaksnr: %s" % arkivsaksref
            raise Exception("unable to parse url %s" % dayurl)

        data['docdesc'] = tr.cssselect("div.tittel")[0].text_content().strip()

        datofratil = tr.cssselect("div.fratil")[0]

        for dtr in tr.cssselect("table.postjournaldetaljer > tr"):
            entry = dtr.cssselect('td')
            heading = entry[0].text_content().strip()
            if heading in fieldmap:
                data[fieldmap[heading]] = entry[1].text_content()

        if data['doctype'] in typemap:
            data['doctype'] = typemap[data['doctype']]
        else:
            raise Exception("unknown document type")

        if 'docdate' in data:
            data['docdate'] = dateutil.parser.parse(data['docdate'],
                                                    dayfirst=True).date()
        if 'exemption' in data:
            data['exemption'] = data['exemption'].replace('Unntatt offentlighet, ', '')

        dato, fratil = datofratil.text_content().split('-', 1)
        data['recorddate'] = dateutil.parser.parse(dato.replace('Dato: ', '').strip(), dayfirst=True).date()
        fratil = fratil.strip().replace('Avsender:', '').strip()
        fratil = fratil.strip().replace('Mottaker:', '').strip()
        if parser.is_sender_doctype(data['doctype']):
            fratilfield = 'sender'
        elif parser.is_recipient_doctype(data['doctype']):
             fratilfield = 'recipient'
        data[fratilfield] = fratil

#        print data
        parser.verify_entry(data)
        datastore.append(data)

def fetch_day(parser, day):
    datastore = []
    dayurl = 'http://www.as.kommune.no/offentlig-journal-og-innsyn-i-arkiv.352152.no.html?pjdate=%s&pjfind=&pjdoktype=&cat=352152' % day.strftime('%d.%m.%Y')
    html = postlistelib.fetch_url_harder(dayurl).decode('utf-8')
#    print html
    try:
        parse_day_html(parser, datastore, dayurl, html)
        scraperwiki.sqlite.save(unique_keys=['arkivsaksref'], data=datastore)
    except NoDataEntries, e:
    	return
    except Exception, e:
        print html
        raise

aday = datetime.timedelta(1) # one day delta
newest = None
try:
    newest = dateutil.parser.parse(scraperwiki.sqlite.select("max(recorddate) as max from swdata")[0]["max"], dayfirst=False).date()
    oldest = dateutil.parser.parse(scraperwiki.sqlite.select("min(recorddate) as min from swdata")[0]["min"], dayfirst=False).date()
except scraperwiki.sqlite.SqliteError:
    # Table not created yet, ignore the error
    pass

if not newest:
    # Bootstrap a month ago
    newest = datetime.datetime.today() - aday * 30
    oldest = newest

skiplimit = 10

# Look forward one week to at least get past the weekends
for n in xrange(skiplimit):
    fetch_day(parser, newest + aday * n)

for n in xrange(skiplimit):
    fetch_day(parser, oldest - aday * n)