Skip to content

Commit 1cbb4c0

Browse files
author
Sergio Cruz
committed
Finalizada la creación del proceso de descarga de datos.
1 parent 01d6d64 commit 1cbb4c0

File tree

1 file changed

+65
-32
lines changed

1 file changed

+65
-32
lines changed

geonames_importer.sh

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
11
#!/bin/bash
22

3+
working_dir=$( cd "$( dirname "$0" )" && pwd )
4+
data_dir="$working_dir/data"
5+
zip_codes_dir="$working_dir/data/zip_codes"
6+
37
# [BEGIN] CONFIGURATION FOR THE SCRIPT
48
# -------------------------------------
9+
10+
# Geonames URLs
11+
geonames_general_data_repo="http://download.geonames.org/export/dump/"
12+
geonames_postal_code_repo="http://download.geonames.org/export/zip/"
13+
514
# Default values for database variables.
615
dbhost="localhost"
716
dbport=3306
817
dbname="geonames"
9-
dbusername= "root"
10-
dbpassword= "root"
18+
dbusername="root"
19+
dbpassword="root"
1120

1221
# Default value for download folder
13-
download_folder="download"
22+
download_folder="$working_dir/download"
1423

1524
# Default general dumps to download
16-
dumps= "allCountries.zip alternateNames.zip hierarchy.zip admin1CodesASCII.txt admin2Codes.txt featureCodes_en.txt timeZones.txt countryInfo.txt"
25+
dumps="allCountries.zip alternateNames.zip hierarchy.zip admin1CodesASCII.txt admin2Codes.txt featureCodes_en.txt timeZones.txt countryInfo.txt"
1726
# By default all postal codes ... You can specify a set of the files located at http://download.geonames.org/export/zip/
18-
postal_codes= "allCountries.zip"
19-
27+
postal_codes="allCountries.zip"
28+
29+
#
30+
# The folders configuration used by this application is as follows:
31+
#
32+
# current_dir
33+
# ├── data => Decompressed data used in the import process
34+
# └── download => Default folder where downloaded files will be stored temporaly
35+
#
36+
#
37+
#
2038
# [END] CONFIGURATION FOR THE SCRIPT
2139
# -------------------------------------
2240

2341
logo() {
24-
echo " __________________________________________________________________________________________________________________ "
25-
echo " _____ _____ _ _____ _ "
26-
echo " / ____| | __ \ | | |_ _| | | "
27-
echo " | | __ ___ ___ _ __ __ _ _ __ ___ ___ ___ | | | | __ _| |_ __ _ | | _ __ ___ _ __ ___ _ __| |_ "
28-
echo " | | |_ |/ _ \/ _ \| '_ \ / _` | '_ ` _ \ / _ \/ __| | | | |/ _` | __/ _` | | | | '_ ` _ \| '_ \ / _ \| '__| __| "
29-
echo " | |__| | __/ (_) | | | | (_| | | | | | | __/\__ \ | |__| | (_| | || (_| | _| |_| | | | | | |_) | (_) | | | |_ "
30-
echo " \_____|\___|\___/|_| |_|\__,_|_| |_| |_|\___||___/ |_____/ \__,_|\__\__,_| |_____|_| |_| |_| .__/ \___/|_| \__| "
31-
echo " |_| "
32-
echo " __________________________________________________ v 2.0 _________________________________________________________ "
42+
echo "================================================================================================"
43+
echo " "
44+
echo " G E O N A M E S D A T A I M P O R T E R "
45+
echo " "
46+
echo "=========================================== v 2.0 =============================================="
3347
}
3448

3549
usage() {
@@ -85,6 +99,9 @@ if [ $# -lt 1 ]; then
8599
fi
86100

87101
logo
102+
echo "Current working folder: $working_dir"
103+
echo "Current data folder: $data_dir"
104+
echo "Default download folder: $download_folder"
88105

89106
# Deals operation mode 1 (Download data bundles from geonames.org)
90107
#if { [ "$1" == "--download-data" ]; } then
@@ -117,14 +134,18 @@ done
117134
case $action in
118135
download-data)
119136
echo "STARTING DATA DOWNLOAD !!"
120-
# Checks if a download folder has been specified.
137+
# Checks if a download folder has been specified otherwise checks if the default download folder
138+
# exists and if it doesn't then creates it.
121139
if { [ "$3" != "" ]; } then
122140
if [ ! -d "$3" ]; then
123-
echo "Temporary download data folder '$3' doesn't exists. Creating it."
124-
mkdir -p "$3"
141+
echo "Temporary download data folder $3 doesn't exists. Creating it."
142+
mkdir -p "$working_dir/$3"
125143
fi
126-
download_folder="`pwd`/$3"
144+
# Changes the default download folder to the one specified by the user.
145+
download_folder="$working_dir/$3"
146+
echo "Changed defuault download folder to $download_folder"
127147
else
148+
# Creates default download folder
128149
if [ ! -d "$download_folder" ]; then
129150
echo "Temporary download data folder '$download_folder' doesn't exists. Creating it."
130151
mkdir -p "$download_folder"
@@ -133,30 +154,42 @@ case $action in
133154

134155
# Dumps General data.
135156
echo "Downloading general files"
157+
if [ ! -d $data_dir ]; then
158+
echo "Data folder does not exist. Creating it ..."
159+
mkdir -p $data_dir
160+
fi
136161
for dump in $dumps; do
137162
echo "Downloading $dump into $download_folder"
138-
wget -c -P "$download_folder" http://download.geonames.org/export/dump/$dump
163+
wget -c -P "$download_folder" "$geonames_general_data_repo/$dump"
139164
if [ ${dump: -4} == ".zip" ]; then
140-
echo "Unzipping $dump into `pwd`"
141-
unzip "$download_folder/$dump" -d "`pwd`"
165+
echo "Unzipping $dump into $data_dir"
166+
unzip "$download_folder/$dump" -d $data_dir
167+
else
168+
if [ ${dump: -4} == ".txt" ]; then
169+
mv "$download_folder/$dump" $data_dir
170+
fi
142171
fi
143172
done
144173

145174
# Dumps Postal Code data.
146175
echo "Downloading postal code information"
176+
if [ ! -d $zip_codes_dir ]; then
177+
echo "Zip Codes data folder does not exist. Creating it ..."
178+
mkdir -p $zip_codes_dir
179+
fi
147180
if [ ! -d "$download_folder/zip_codes" ]; then
148181
echo "Temporary download data folder '$download_folder/zip_codes' doesn't exists. Creating it."
149182
mkdir -p "$download_folder/zip_codes"
150-
fi
183+
fi
151184
for postal_code_file in $postal_codes; do
152185
echo "Downloading $postal_code_file into $download_folder/zip_codes"
153-
wget -c -P "$download_folder/zip_codes" http://download.geonames.org/export/zip/$postal_code_file
154-
if [ ${dump: -4} == ".zip" ]; then
186+
wget -c -P "$download_folder/zip_codes" "$geonames_postal_code_repo/$postal_code_file"
187+
if [ ${postal_codes: -4} == ".zip" ]; then
155188
echo "Unzipping Postal Code file $postal_code_file into $download_folder/zip_codes"
156-
unzip "$download_folder/zip_codes/$postal_code_file" -d "$download_folder/zip_codes"
189+
unzip "$download_folder/zip_codes/$postal_code_file" -d $zip_codes_dir
157190
fi
158191
done
159-
echo "DATA DOWNLOAD FINISH !!"
192+
echo "DATA DOWNLOAD FINISHED !!"
160193
exit 0
161194
;;
162195
esac
@@ -177,18 +210,18 @@ case "$action" in
177210
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "DROP DATABASE IF EXISTS $dbname;"
178211
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "CREATE DATABASE $dbname DEFAULT CHARACTER SET utf8;"
179212
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "USE $dbname;"
180-
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < `pwd`/geonames_db_struct.sql
213+
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < "$working_dir/geonames_db_struct.sql"
181214
;;
182215

183216
create-tables)
184217
echo "Creating tables for database $dbname..."
185218
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "USE $dbname;"
186-
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < `pwd`/geonames_db_struct.sql
219+
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < "$working_dir/geonames_db_struct.sql"
187220
;;
188221

189222
import-dumps)
190223
echo "Importing geonames dumps into database $dbname"
191-
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword --local-infile=1 $dbname < `pwd`/geonames_import_data.sql
224+
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword --local-infile=1 $dbname < "$working_dir/geonames_import_data.sql"
192225
;;
193226

194227
drop-db)
@@ -198,7 +231,7 @@ case "$action" in
198231

199232
truncate-db)
200233
echo "Truncating \"geonames\" database"
201-
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < `pwd`/geonames_truncate_db.sql
234+
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < "$working_dir/geonames_truncate_db.sql"
202235
;;
203236
esac
204237

@@ -208,4 +241,4 @@ else
208241
echo "[FAILED]"
209242
fi
210243

211-
exit 0
244+
exit 0

0 commit comments

Comments
 (0)