summaryrefslogtreecommitdiffstats
path: root/etc/ioverlander-wiki-import.sh
blob: 7f550854fea4b4cc1f067cf62d1d9078b924cd2d (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
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
#!/usr/bin/env bash
#
# This script imports iOverlander data into a imag-wiki
#
# Requirements
# ============
#
# * imag-wiki
# * imag-gps
# * imag-store
# * jq
#
# Usage
# =====
#
# Download the JSON data files from app.ioverlander.com and run them through
# this script.
#
#   ./this/script wiki-name ioverlander-file.json
#
# What it does
# ============
#
# It imports each location from the JSON data into one entry at:
#
#       <wiki_name>/<country>/<location name>_<location id>
#
# (The location id is added because names are sometimes reused)
#
# * It uses imag-gps to set the GPS data
# * It sets the header of the entry to the data it finds from the JSON object.
#   It puts all data in the "userdata.ioverlander" namespace.
#   It strips whitespace from the keys.
#   It ignores the GPS data from the JSON object though, as this was added via
#   imag-gps.
# * It uses the "description" field as entry content.
#
# Warning
# =======
#
# As the ioverlander data sometimes contains "/" in the name, some entries are
# namespaced. This has to be fixed and the linkings have to be adapted. This is
# not yet handled by this script.
#
#

WIKI_NAME="$1"
IOVERLANDER_FILE_PATH="$2"

[[ -z "$WIKI_NAME" ]]               && echo "Wiki name missing" && exit 1
[[ -z "$IOVERLANDER_FILE_PATH" ]]   && echo "JSON file missing" && exit 1

imag wiki create-wiki "$WIKI_NAME" --no-edit
echo "Created imag wiki '$WIKI_NAME'"

tobool() {
    if [[ "$1" == "Yes" ]]; then
        echo "true"
    elif [[ "$1" == "No" ]]; then
        echo "false"
    else
        echo "\"$1\""
    fi
}

object_to_vars() {
    jq -r -c 'to_entries | .[] | .key + "=\"" + (.value | tostring | gsub("\\\""; "")) + "\""'
}


# The comments in this function can be outcommented for debugging
process_object() {
    read -r json

    local location_latitude
    local location_longitude
    local location_altitude
    local location_horizontal_accuracy
    local location_vertical_accuracy
    local amenities_Open
    local amenities_Electricity
    local amenities_Wifi
    local amenities_Kitchen
    local amenities_Restaurant
    local amenities_Showers
    local amenities_Water
    local amenities_Toilets
    local amenities_Big_rig_friendly
    local amenities_Tent_friendly
    local amenities_Pet_friendly
    local id
    local name
    local description
    local date_verified
    local category_icon_path
    local category_icon_pin_path
    local country
    local category

    #echo "----------------------------------------------------------"
    #echo "JSON = $json"
    #echo "-----------"
    #echo $json | jq '.location' | object_to_vars | sed 's,^,location_,'
    #echo $json | jq '.amenities' | object_to_vars | sed 's,^,amenities_,; s, Big,_big,; s, Rig,_rig,; s, Friendly,_friendly,'
    #echo $json | jq 'del(.location)|del(.amenities)' | object_to_vars

    eval $(echo $json | jq '.location' | object_to_vars | sed 's,^,location_,')
    eval $(echo $json | jq '.amenities' | object_to_vars | sed 's,^,amenities_,; s, Big,_big,; s, Rig,_rig,; s, Friendly,_friendly,')
    eval $(echo $json | jq 'del(.location)|del(.amenities)' | object_to_vars)

    local ctry_slug=$(echo $country | sed 's, ,_,g')
    local name_slug=$(echo $name | sed 's, ,_,g')

    echo "create = $ctry_slug/${name_slug}_${id}"

    local article=$(imag wiki --wiki "$WIKI_NAME" create "$ctry_slug/${name_slug}_${id}" --no-edit --print-id || exit 1)

    echo "ARTICLE = $article"

    imag gps add --lat="$location_latitude" --long="$location_longitude" "$article" || {
        echo "GPS setting failed"
        exit 1
    }
    imag store update --id "$article"                           \
        --header                                                \
        "userdata.ioverlander.id=\"$id\""                       \
        "userdata.ioverlander.name=\"$name\""                   \
        "userdata.ioverlander.date_verified=\"$date_verified\"" \
        "userdata.ioverlander.country=\"$country\""             \
        "userdata.ioverlander.category=\"$category\""           \
        "userdata.ioverlander.amenities.open=$(tobool "$amenities_Open")"                         \
        "userdata.ioverlander.amenities.electricity=$(tobool "$amenities_Electricity")"           \
        "userdata.ioverlander.amenities.wifi=$(tobool "$amenities_Wifi")"                         \
        "userdata.ioverlander.amenities.kitchen=$(tobool "$amenities_Kitchen")"                   \
        "userdata.ioverlander.amenities.restaurant=$(tobool "$amenities_Restaurant")"             \
        "userdata.ioverlander.amenities.showers=$(tobool "$amenities_Showers")"                   \
        "userdata.ioverlander.amenities.water=$(tobool "$amenities_Water")"                       \
        "userdata.ioverlander.amenities.toilets=$(tobool "$amenities_Toilets")"                   \
        "userdata.ioverlander.amenities.big_rig_friendly=$(tobool "$amenities_Big_rig_friendly")" \
        "userdata.ioverlander.amenities.tent_friendly=$(tobool "$amenities_Tent_friendly")"       \
        "userdata.ioverlander.amenities.pet_friendly=$(tobool "$amenities_Pet_friendly")"         \
        --content \""$description\"" || {
            echo "header/content setting failed";
            exit 1
        }

}

cat "$IOVERLANDER_FILE_PATH" | jq -c '.[]' | while read line; do
    process_object
done