-
Notifications
You must be signed in to change notification settings - Fork 1
/
freq_count_place_topic_nothread_count.py
executable file
·170 lines (137 loc) · 4.25 KB
/
freq_count_place_topic_nothread_count.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
164
165
166
167
168
169
170
#!/usr/bin/env python3.5
import requests
import json
from lxml import etree
import time
import random
import threading
OUTPUT_DIR = "result"
SRU_QUERY = 'http://jsru.kb.nl/sru/sru?query='
SRU_QUERY += '"%s" and "%s" and ppn exact "%s" '
SRU_QUERY += 'and date within "01-01-%s 31-12-%s" '
SRU_QUERY += 'and type exact "%s"'
SRU_QUERY += '&x-collection=DDD_artikel&maximumRecords=1'
SRU_QUERY += '&startRecord=%s'
START_STOP = {}
WANTED_TOPICS = []
WANTED_PLACES = []
WANTED_PPNS = []
WANTED_RANGES = []
WANTED_TYPES = ["illustratie met onderschrift",
"artikel",
"advertentie",
"familiebericht"]
def read_ppns(filename='ppn.csv'):
global WANTED_PPNS
with open(filename) as fh:
data = fh.read()
for line in data.split('\n'):
ppn = line[1:-1]
if ppn:
WANTED_PPNS.append(ppn)
def read_startstop(filename='start_stop.csv'):
global START_STOP
with open(filename) as fh:
data = fh.read()
for line in data.split('\n'):
if line.strip():
line = line.split(',')
ppn = line[0]
start = line[1]
stop = line[2]
if int(start) > int(stop):
t = start
start = stop
stop = t
START_STOP[ppn] = [int(start), int(stop)]
def read_places(filename='places.csv'):
global WANTED_PLACES
with open(filename) as fh:
data = fh.read()
for line in data.split('\n'):
place = line.split(',')[0][1:-1]
if place:
WANTED_PLACES.append(place)
else:
print(line)
def read_years(filename='years.txt'):
global WANTED_RANGES
years = []
'''
with open(filename) as fh:
data = fh.read()
for line in data.split('\n'):
if not line == 'Years':
years.append(str(line))
for i in range(0, len(years)-1):
if i >0:
WANTED_RANGES.append([str(int(years[i]) + 1), years[i + 1]])
else:
WANTED_RANGES.append([years[i], years[i+1]])
'''
for i in range(1865, 1995):
WANTED_RANGES.append(str(i))
def exec_query(topic, ppn, place, date, atype):
done = False
while not done:
sru = SRU_QUERY % (topic, place, ppn, date, date, atype, '0')
try:
data = requests.get(sru)
data = etree.fromstring(data.content)
done = True
except:
time.sleep(random.random())
for item in data.iter():
if item.tag.endswith('numberOfRecords'):
return item.text
def read_topics(fname='categories.txt'):
global WANTED_TOPICS
cat = {}
all_topics = []
with open(fname, 'r') as fh:
for line in fh.read().split('\n'):
if not line:
continue
name = line.split(',')[0]
if not line.split(',')[1].isupper():
if not name in cat:
cat[name] = []
cat[name].append(line.split(',')[1])
all_topics.append(line.split(',')[1])
WANTED_TOPICS = all_topics
def do_one_topic(topic, outfile, start_stop, c):
result = []
#WANTED_RANGES = range(start_stop[0], start_stop[1])
for place in WANTED_PLACES:
for date in WANTED_RANGES:
for atype in WANTED_TYPES:
#print(topic, place, date, ppn, atype)
c += 1
#result.append((topic,
# ppn,
# place,
# date,
# atype,
# exec_query(topic,
# ppn,
# place,
# date,
# atype,
# )
# ))
#out = str(result[-1])[1:-1] + '\n'
#outfile.write(out)
return c
if __name__ == '__main__':
read_ppns()
read_places()
read_years()
read_topics()
read_startstop()
uk = []
c = 0
for ppn in WANTED_PPNS:
for topic in WANTED_TOPICS:
c= do_one_topic(topic, None, START_STOP[ppn], c)
print(c)
print(c)