-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-location-to-gnome-weather.sh
87 lines (67 loc) · 2.66 KB
/
add-location-to-gnome-weather.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
#!/bin/bash
#Original developer: julianfairfax
#Original repository: https://gitlab.com/julianfairfax/scripts
#Description: This Script help you to add a location to Gnome Weather
#Dependency: Need to install "bc" alongside!
if [[ ! -z "$(which gnome-weather)" ]]; then
system=1
fi
if [[ ! -z "$(flatpak list | grep org.gnome.Weather)" ]]; then
flatpak=1
fi
if [[ ! $system == 1 && ! $flatpak == 1 ]]; then
echo "GNOME Weather isn't installed"
exit
fi
language=$(locale | sed -n 's/^LANG=\([^_]*\).*/\1/p')
if [[ ! -z "$*" ]]; then
query="$*"
else
read -p "Type the name of the location you want to add to GNOME Weather: " query
fi
query="$(echo $query | sed 's/ /+/g')"
request=$(curl "https://nominatim.openstreetmap.org/search?q=$query&format=json&limit=1" -H "Accept-Language: $language" -s)
if [[ $request == "[]" ]]; then
echo "No locations found, consider removing some search terms"
exit
fi
read -p "If this is not the location you wanted, consider adding search terms
Are you sure you want to add $(echo $request | sed 's/.*"display_name":"//' | sed 's/".*//')? [y/n] : " answer
if [[ ! $answer == "y" ]]; then
echo "Not adding location"
exit
else
echo "Adding location"
fi
id=$(echo $request | sed 's/.*"place_id"://' | sed 's/,.*//')
details=$(curl "https://nominatim.openstreetmap.org/details.php?place_id=$id&format=json" -s)
if [[ $details == *"name:$language"* ]]; then
name=$(echo $details | sed "s/.*\"name:$language\": \"//" | sed 's/".*//')
else
name=$(echo $details | sed 's/.*"name": "//' | sed 's/".*//')
fi
lat=$(echo $request | sed 's/.*"lat":"//' | sed 's/".*//')
lat=$(echo "$lat / (180 / 3.141592654)" | bc -l)
lon=$(echo $request | sed 's/.*"lon":"//' | sed 's/".*//')
lon=$(echo "$lon / (180 / 3.141592654)" | bc -l)
if [[ $system == 1 ]]; then
locations=$(gsettings get org.gnome.Weather locations)
fi
if [[ $flatpak == 1 ]]; then
locations=$(flatpak run --command=gsettings org.gnome.Weather get org.gnome.Weather locations)
fi
location="<(uint32 2, <('$name', '', false, [($lat, $lon)], @a(dd) [])>)>"
if [[ $system == 1 ]]; then
if [[ ! $(gsettings get org.gnome.Weather locations) == "@av []" ]]; then
gsettings set org.gnome.Weather locations "$(echo $locations | sed "s|>]|>, $location]|")"
else
gsettings set org.gnome.Weather locations "[$location]"
fi
fi
if [[ $flatpak == 1 ]]; then
if [[ ! $(flatpak run --command=gsettings org.gnome.Weather get org.gnome.Weather locations) == "@av []" ]]; then
flatpak run --command=gsettings org.gnome.Weather set org.gnome.Weather locations "$(echo $locations | sed "s|>]|>, $location]|")"
else
flatpak run --command=gsettings org.gnome.Weather set org.gnome.Weather locations "[$location]"
fi
fi