summaryrefslogtreecommitdiffstats
path: root/download.sh
blob: 791819e21d74e54f7ce5cf9585cf40d9401bb846 (plain)
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
#! /bin/bash

# cat list.html | cut -d '"' -f 6 > list.txt
# Need to clean

if [ -z "$1" ]; then
    echo "$0 <list_name>"
    exit -1
fi

list="$1"
listname="$(basename $list .txt)"
if [ ! -f "$list" ]; then
    echo "List "$list" is not found"
    exit -1
fi

if [[ "$list" =~ "velo" ]]; then
    url_nonu="http://openmtbmap.org/?s2member_file_download=odbl/nonunicode/english"
    url_u="http://openmtbmap.org/?s2member_file_download=odbl/english"
    url="http://ftp5.gwdg.de/pub/misc/openstreetmap/openmtbmap/odbl/velomap"
else
    url_nonu="http://openmtbmap.org/?s2member_file_download=odbl/nonunicode/english"
    url_u="http://openmtbmap.org/?s2member_file_download=odbl/english"
    url="http://ftp5.gwdg.de/pub/misc/openstreetmap/openmtbmap/odbl"
fi

if [ -f "cookies.jar" ]; then
    if test `find "cookies.jar" -mmin +120`; then
	do_auth=1
    else
	do_auth=0
    fi
else
    do_auth=1
fi

if [ $do_auth -ne 0 ]; then
    curl -d log="csa7fff" -d pwd="$(cat .pass)" -d rememberme="forever" --cookie-jar ./cookies.jar https://openmtbmap.org/wp-login.php &> /dev/null
    [ $? -ne 0 ] && { echo "Login failed"; exit 1; }
else
    echo "Re-using old authentication cookies"
fi

function check {
    local fail=$1
    local name="$2"

    [ $fail -ne 0 ] && return $fail
    size=$(du -sm "downloads/$name"  | awk '{ print $1 }')
    [ -z "$size" ] && return 1
    [ "$size" -lt 2 ] && return 1
    return 0
}
 
mkdir -p downloads
for name in $(cat $list); do
    [ -f downloads/$name ] && continue
    
    # Try first non-unicode
    fail=0
    curl -L --cookie cookies.jar "$url_nonu/$name" -o "downloads/$name" &> /dev/null
    check "$?" "$name"
    fail=$?

    if [ $fail -ne 0 ]; then
	curl -L --cookie cookies.jar "$url_u/$name" -o "downloads/$name" &> /dev/null
	check "$?" "$name"
	fail=$?
	fail_nonu=1
    fi

    if [ $fail -ne 0 ]; then
        rm -f downloads/$name

        fail=0
        curl -o downloads/$name $url/$name &> /dev/null
	check "$?" "$name"

        if [ $? -ne 0 ]; then
	    curl -o downloads/$name $url/asia/$name &> /dev/null
	    check "$?" "$name"
	fi
        if [ $? -ne 0 ]; then
    	    curl -o downloads/$name $url/africa/$name &> /dev/null
	    check "$?" "$name"
	fi

        if [ $? -ne 0 ]; then
            fail=1
        fi

        [ $fail -ne 0 ] && echo "*** Failed ***: $name"
        [ $fail -eq 0 ] && echo "Unicode version (local language!): $name"
    elif [ -n "$fail_nonu" ]; then
        echo "Non-unicode english version downloaded: $name"
    else
        echo "Unicode english version downloaded: $name"
    fi
done