forked from jorgecarleitao/echr_network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.py
163 lines (113 loc) · 5.14 KB
/
crawler.py
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
162
163
import urllib.request
import urllib.error
import json
import datetime
import models
# select everything:
SELECT_ALL = 'itemid,applicability,appno,article,conclusion,decisiondate,docname,documentcollectionid,' \
'documentcollectionid2,doctype,externalsources,importance,introductiondate,issue,judgementdate,' \
'kpthesaurus,meetingnumber,originatingbody,publishedby,referencedate,kpdate,reportdate,representedby,' \
'resolutiondate,resolutionnumber,respondent,rulesofcourt,separateopinion,scl,typedescription,ecli,' \
'nonviolation,violation'
SELECT_NONE = 'itemid'
# sorting by itemid makes the download systematic since this number always increases with new documents in hudoc.
META_URL = 'http://hudoc.echr.coe.int/app/query/results' \
'?query=(contentsitename=ECHR) AND (documentcollectionid2:"JUDGMENTS" OR documentcollectionid2:"COMMUNICATEDCASES")' \
'&select={select}' + \
'&sort=itemid Ascending' + \
'&start={start}&length={count}'
META_URL = META_URL.replace(' ', '%20')
META_URL = META_URL.replace('"', '%22')
HTML_URL = 'http://hudoc.echr.coe.int/app/conversion/docx/html/body?library=ECHR&id={doc_id}'
def open_url(url, file_id, reset_cache=False):
file_name = '_cache/' + file_id + ".html"
if not reset_cache:
try:
with open(file_name) as f:
return f.read()
except IOError:
reset_cache = True
if reset_cache:
response = urllib.request.urlopen(url)
data = response.read()
encoding = response.info().get_content_charset('utf-8')
data = data.decode(encoding)
with open(file_name, "w") as f:
f.write(data)
return data
def get_or_create(session, model, **kwargs):
instance = session.query(model).filter_by(**kwargs).first()
if instance:
return instance
else:
instance = model(**kwargs)
session.add(instance)
return instance
def retrieve_html(doc_id):
url = HTML_URL.format(doc_id=doc_id)
return open_url(url, 'doc_%s' % doc_id)
def retrieve_documents(start=0, count=1, select=SELECT_ALL):
url = META_URL.format(start=start, count=count, select=select)
data = open_url(url, 'doc_list_%d_%d_all' % (start, count), reset_cache=True)
json_object = json.loads(data)
return json_object["resultcount"], json_object['results']
def parse_articles(doc):
articles = []
articles_ids = doc['article'].split(';')
for article_id in articles_ids:
try:
article_id = int(article_id) # consider only main articles
articles.append(article_id)
except ValueError:
continue
return articles
def process_case_name(string):
# remove trailing spaces, see http://stackoverflow.com/a/2077944/931303
return ' '.join(string.split()).replace('CASE OF ', '').upper()
def process(docs, session):
for doc_object in docs:
json_object = doc_object['columns']
doc_id = json_object['itemid']
# todo: pass this to logger
print(doc_id)
html = retrieve_html(doc_id)
case_name = process_case_name(json_object['docname'])
doc = models.Document(id=doc_id, scl=json_object['scl'],
html=html,
case=json_object['appno'],
date=parse_date(json_object['kpdate']),
case_name=case_name,
tags=json_object['documentcollectionid2'],
violations=json_object['violation'],
nonviolations=json_object['nonviolation'],
)
for article_id in parse_articles(json_object):
article = get_or_create(session, models.Article, id=article_id)
doc.articles.append(article)
# merge: if doc already exists in db, update it.
session.merge(doc)
session.commit()
def retrieve_and_save(session, start=0, max_docs=None, batch_size=500):
assert batch_size <= 500, "Batch size must be smaller than 500. " \
"The server returns a 400 (Bad request) with more."
documents_to_retrieve, _ = retrieve_documents()
if max_docs is not None:
documents_to_retrieve = min(max_docs-start, documents_to_retrieve)
batches = documents_to_retrieve//batch_size
for batch in range(batches):
_, docs = retrieve_documents(start, batch_size)
process(docs, session)
start += batch_size
documents_to_retrieve -= batch_size
if documents_to_retrieve > 0:
assert(documents_to_retrieve < batch_size)
_, docs = retrieve_documents(start, documents_to_retrieve)
process(docs, session)
def parse_date(date_string):
# for some reason dates are not returned consistently.
# in some instances the month and day are exchanged
if '12:00:00 AM' in date_string:
date_string = date_string.split(' ')[0]
return datetime.datetime.strptime(date_string, '%m/%d/%Y').date()
# '29/01/2004 00:00:00'
return datetime.datetime.strptime(date_string, '%d/%m/%Y %H:%M:%S').date()