forked from KoenQQ/incident_monitor_nl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd
173 lines (164 loc) · 6.95 KB
/
cd
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
171
172
commit 2d6da217f6362b5a8029fc716d1d9792b229fc51 (HEAD -> master)
Author: KoenQQ <[email protected]>
Date: Fri Oct 30 16:46:50 2020 +0100
updated scraper that goes into more detail.
diff --git a/incident_example_data.xlsx b/incident_example_data.xlsx
new file mode 100644
index 0000000..1a931b4
Binary files /dev/null and b/incident_example_data.xlsx differ
diff --git a/scraper/management/commands/__pycache__/scrape.cpython-37.pyc b/scr
aper/management/commands/__pycache__/scrape.cpython-37.pyc
index 5f1ec88..04218d6 100644
Binary files a/scraper/management/commands/__pycache__/scrape.cpython-37.pyc and
b/scraper/management/commands/__pycache__/scrape.cpython-37.pyc differ
diff --git a/scraper/management/commands/scrape.py b/scraper/management/commands
/scrape.py
index febb3c6..6399383 100644
--- a/scraper/management/commands/scrape.py
+++ b/scraper/management/commands/scrape.py
@@ -1,6 +1,7 @@
from urllib.request import urlopen
import json
from django.core.management.base import BaseCommand, CommandError
+import numpy as np
import pandas as pd
import requests as rq
import cProfile
@@ -26,7 +27,7 @@ class Command(BaseCommand):
#convert to soup
soup = BeautifulSoup(xml_data,"html.parser")
- p
+
# grab all incidents, met toebehoren
incident_messages = soup.find_all("item")
@@ -46,36 +47,77 @@ class Command(BaseCommand):
call.append([title, description, geo_lat, geo_long, pubDate])
incidents_data = incidents_data.append(call)
+ incidents_data.rename(columns={ 0: "comment", 1: "monitorcode", 2:"lati
tude", 3:'longtitude', 4:'timestamp'}, inplace=True)
+ incidents_data['emergency_service'] = ""
+ incidents_data['priority_code'] = ""
+
+ #fill emergency service column
+ regex_medical = r'MKA'
+ regex_fire = r'BRW'
+ regex_police = r'Politie'
+
+ incidents_data['emergency_service'] = np.where(incidents_data['monitorc
ode'].str.contains(regex_medical), 'Medical',
+ np.where(incidents_data['monitorcode'].str.contains(regex_fire), 'F
ire brigade',
+ np.where(incidents_data['monitorcode'].str.contains(regex_police),
'Police', 'unknown')))
+
+
+ #clean up the entries a bit
+ incidents_data['monitorcode'] = incidents_data['monitorcode'].str[:-5]
+ incidents_data['monitorcode'] = incidents_data['monitorcode'].str.repla
ce(r'<br\/>', ', ')
+
+ incidents_data['latitude'] = incidents_data['latitude'].astype(str)
+ incidents_data['latitude'] = incidents_data['latitude'].str.replace(r'<
geo:lat>', '').str.replace(r'<\/geo:lat>', '')
+ incidents_data['latitude'] = incidents_data['latitude'].astype(float)
+
+ incidents_data['longtitude'] = incidents_data['longtitude'].astype(str)
+ incidents_data['longtitude'] = incidents_data['longtitude'].str.replace
(r'<geo:long>', '').str.replace(r'<\/geo:long>', '')
+ incidents_data['longtitude'] = incidents_data['longtitude'].astype(floa
t)
- incidents_data['emergency code'] = incidents_data['description'].regex
- print(incidents_data['emergency code'])
-
- # split the 'description' column up in multiple columns
- for r in incidents_data:
- incidents_data['code'] = incidents_data['description'].apply(lambda
x: re.search(r'\d+', x).group())
- incidents_data['e_service'] = incidents_data['description'].apply(l
ambda x: re.search(r'', x).group())
- #finetune and add
-
- incidents_data.rename(columns={"0": "title", "1": "description", "2" :
"geo_lat", "3" : "geo_long", "4" : "pubDate" })
- # print(incidents_data.head())
-
- # check if url in db
- for s in incidents_data:
- try:
- # dit moet save to dataframe worden
- # save in db
- # must match Model
- Incidents.objects.create(
- title=title,
- description = description if description is not None else "
",
- message_code =
- emergency_service =
- geo_lat = geo_lat.text if geo_lat is not None else " ",
- geo_long = geo_long.text if geo_long is not None else " ",
- pubDate = pubDate
- )
- print('%s added' % (title,))
- except:
- print('%s could not add' % (title, ))
+ #fill priority_code column
+ incidents_data['priority_code'] = np.where(incidents_data['comment'].st
r.contains(r'A1'), 'A1',
+ np.where(incidents_data['comment'].str.contains(r'B1'), 'B1',
+ np.where(incidents_data['comment'].str.contains(r'A2'), 'A2',
+ np.where(incidents_data['comment'].str.contains(r'B2'), 'B2',
+ np.where(incidents_data['comment'].str.contains(r'P 1'), 'Prio 1',
+ np.where(incidents_data['comment'].str.contains(r'Prio 1'), 'Prio 1
',
+ np.where(incidents_data['comment'].str.contains(r'P 2'), 'Prio 2',
+ np.where(incidents_data['comment'].str.contains(r'Prio 2'), 'Prio 2
',
+ np.where(incidents_data['comment'].str.contains(r'PRIO 1'), 'Prio 1
',
+ np.where(incidents_data['comment'].str.contains(r'prio 1'), 'Prio 1
',
+ np.where(incidents_data['comment'].str.contains(r'PRIO 2'), 'Prio 2
',
+ np.where(incidents_data['comment'].str.contains(r'prio 2'), 'Prio 2
',
+ np.where(incidents_data['comment'].str.contains(r'Prio 4'), 'Prio 4
',
+ np.where(incidents_data['monitorcode'].str.contains(r'Persinformati
e'), 'Persinformatie',
+ 'NaN'))))))))))))))
+
+ # #drop unknown values
+ index_names = incidents_data[incidents_data['emergency_service'] == 'un
known']
+ print(index_names)
+ # incidents_data.drop(incidents_data[incidents_data['emergency_service'
] == 'unknown'].index, inplace = True)
+
+
+
+
+ # incidents_data.to_excel('incident_example_data.xlsx')
+ # print('exported')
+
+ # # check if url in db
+ # for s in incidents_data:
+ # try:
+ # # dit moet save to dataframe worden
+ # # save in db
+ # # must match Model
+ # Incidents.objects.create(
+ # title=title,
+ # description = description if description is not None else
" ",
+ # message_code =
+ # emergency_service =
+ # geo_lat = geo_lat.text if geo_lat is not None else " ",
+ # geo_long = geo_long.text if geo_long is not None else " "
,
+ # pubDate = pubDate
+ # )
+ # print('%s added' % (title,))
+ # except:
+ # print('%s could not add' % (title, ))
- self.stdout.write( 'job complete' )
+ # self.stdout.write( 'job complete' )