-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.sh
242 lines (196 loc) · 5.03 KB
/
import.sh
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/bash
#
# This file is part of open source system FreenetIS
# and it is released under GPLv3 licence.
#
# More info about licence can be found:
# http://www.gnu.org/licenses/gpl-3.0.html
#
# More info about project can be found:
# http://www.freenetis.org/
#
#
# init enviroment variable
function style
{
if [[ $cron == FALSE ]]
then
tput $1 $2
fi
}
function print_info
{
echo -en "== "
style bold
echo -n $(date '+%D %T')
style sgr0
echo -n " == "
style setaf 2
style bold
echo $1
style sgr0
echo
}
function print_warning
{
echo -en "!! "
style bold
echo -n $(date '+%D %T')
style sgr0
echo -n " !! "
style setaf 1
style bold
echo $1
style sgr0
echo
}
function control_c
{
echo -e "\n"
print_warning "SIGINT? OKAY :'("
rm -rf $directory
exit 1
}
echo "--------------------------------------------------------------------------------"
if [[ "$1" == "--cron" ]] || [[ "$1" == "-c" ]];
then
cron=TRUE
else
cron=FALSE
fi
if [ -r /etc/freenetis-addresses.ini ]
then
. /etc/freenetis-addresses.ini
else
print_warning "Config file not found"
exit 1
fi
# do not modify
country_id=55 # ID of Czech republic
mysql_table="addresses"
mysql_table_old=$mysql_table"_old"
mysql_table_tmp=$mysql_table"_tmp"
# get start tim
starttime=$(date '+%s')
print_info "Trying to get latest database date"
# download web page
html=$(wget -O - http://nahlizenidokn.cuzk.cz/StahniAdresniMistaRUIAN.aspx 2>/dev/null)
if [ $? != 0 ];
then
print_warning "Cannot get latest database date"
rm -rf $directory
exit 1
fi
# read date from html source
datestamp=$(echo $html | sed -nre "s/.*([0-9]{8})_OB_ADR_csv\.zip.*/\1/p")
if [ $? != 0 ];
then
print_warning "Cannot get latest database date"
rm -rf $directory
exit 1
fi
db_datestamp=$(mysql $mysql_db -u $mysql_user -p$mysql_pass -h $mysql_server -P $mysql_port --silent -e "SELECT value FROM config WHERE name LIKE 'datestamp'")
if [ -n "$db_datestamp" ];
then
if [ "$db_datestamp" -ge "$datestamp" ];
then
print_info "Database is up to date"
exit 0
else
print_warning "Database is not up to date and will be updated"
fi
else
print_warning "Database is empty and will be imported"
fi
# create temp directory
directory=$(mktemp -d)
# catch SIGINT
trap control_c SIGINT
# download address database
print_info "Downloading address database"
wget http://vdp.cuzk.cz/vymenny_format/csv/$datestamp\_OB_ADR_csv.zip -O $directory/addresses.zip 2>/dev/null
if [ $? != 0 ];
then
print_warning "Cannot download addresses"
rm -rf $directory
exit 1
fi
# unzip
print_info "Extracting address database"
unzip -qd $directory $directory/addresses.zip
if [ $? != 0 ];
then
print_warning "Cannot extract addresses"
rm -rf $directory
exit 1
fi
# prepare database for importing
print_info "Preparing address database"
FILES=$directory/CSV/*.csv
FILE_NUM=$(ls -1 $FILES | wc -l)
I=0
for f in $FILES
do
I=$(($I+1))
if [[ $cron == FALSE ]]
then
echo -en "\rPreparing $I of $FILE_NUM: $f"
fi
# remove columns #change encoding #create number with orientation number #create number without orientaion number #add country id
cat $f | cut -s -d ";" -f 3-4,7-15 | iconv -f "WINDOWS-1250" -t "UTF-8" | sed -r 's/;(((č\.)(ev\.))|(č\.p\.));([0-9]*);([0-9][0-9]*);(.*)/;\4\3\6\/\7\8/g' | sed -r 's/;(((č\.)(ev\.))|(č\.p\.));([0-9]*);;/;\4\3\6/g' | sed -r "s/(.*)/$country_id;\1/g" > $f.utf8
if [ $? != 0 ];
then
echo -e "\n"
print_warning "Cannot prepare addresses - $I of $FILE_NUM: $f"
rm -rf $directory
exit 1
fi
done
# import database
FILES=$directory/CSV/*.utf8
FILE_NUM=$(ls -1 $FILES | wc -l)
I=0
echo -e "\n"
print_info "Importing address database"
mysql $mysql_db -u $mysql_user -p$mysql_pass -h $mysql_server -P $mysql_port --silent -e "TRUNCATE TABLE $mysql_table_tmp"
if [ $? != 0 ];
then
print_warning "Cannot clean temporary table"
rm -rf $directory
exit 1
fi
for f in $FILES
do
I=$(($I+1))
if [[ $cron == FALSE ]]
then
echo -en "\rImporting $I of $FILE_NUM: $f"
fi
mysql $mysql_db -u $mysql_user -p$mysql_pass -h $mysql_server -P $mysql_port --local-infile -e "LOAD DATA LOCAL INFILE '$f' INTO TABLE $mysql_table_tmp FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' IGNORE 1 LINES"
if [ $? != 0 ];
then
echo -e "\n"
print_warning "Cannot import addresses - $I of $FILE_NUM: $f"
rm -rf $directory
exit 1
fi
done
echo -e "\n"
print_info "Updating database"
mysql $mysql_db -u $mysql_user -p$mysql_pass -h $mysql_server -P $mysql_port --silent -e "RENAME TABLE $mysql_table TO $mysql_table_old; RENAME TABLE $mysql_table_tmp TO $mysql_table; RENAME TABLE $mysql_table_old TO $mysql_table_tmp; REPLACE INTO config VALUES ('datestamp', '$datestamp'); TRUNCATE TABLE $mysql_table_tmp"
if [ $? != 0 ];
then
print_warning "Cannot update database"
rm -rf $directory
exit 1
fi
# clean up
rm -rf $directory
# count script run time
endtime=$(date '+%s')
seconds=$(( $endtime - $starttime ))
hours=$(($seconds / 3600))
seconds=$(($seconds % 3600))
minutes=$(($seconds / 60))
seconds=$(($seconds % 60))
print_info "Import takes $(printf '%d:%02d:%02d\n' $hours $minutes $seconds)"