-
Notifications
You must be signed in to change notification settings - Fork 0
/
sunbar.1m.go
executable file
·222 lines (201 loc) · 5.63 KB
/
sunbar.1m.go
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
// usr/bin/env go run $0 $@; exit
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
// <bitbar.title>Sunrise / Sunset countdown</bitbar.title>
// <bitbar.version>v1.0.0</bitbar.version>
// <bitbar.author>Boris Petersen</bitbar.author>
// <bitbar.author.github>transacid</bitbar.author.github>
// <bitbar.desc>Displays the time till next sunset or sunrise.</bitbar.desc>
// <bitbar.image>https://raw.githubusercontent.com/transacid/sunbar/main/screenshot.png</bitbar.image>
// <bitbar.dependencies>go</bitbar.dependencies>
// <bitbar.abouturl>https://github.com/transacid/sunbar</bitbar.abouturl>
// get your API key for free at https://ipgeolocation.io/
const API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
// set to true when you want to automatically set darkmode on sunset
const DARKMODE_SWITCH = false
func main() {
data, err := getData()
if err != nil {
panic(err)
}
sunrise := data.Sunrise
sunset := data.Sunset
now := time.Now()
var onOff string
switch {
case sunrise.After(now) && sunset.After(now):
fmt.Print(Printer("sunrise", eventDurationFromNow(sunrise), "sunset", eventDurationFromNow(sunset)))
onOff = "true"
case sunrise.Before(now) && sunset.After(now):
fmt.Print(Printer("sunset", eventDurationFromNow(sunset), "sunrise", eventDurationFromNow(sunrise.Add(time.Hour*24))))
onOff = "false"
case sunrise.Before(now) && sunset.Before(now):
fmt.Print(Printer("sunrise", eventDurationFromNow(sunrise.Add(time.Hour*24)), "sunset", eventDurationFromNow(sunset.Add(time.Hour*24))))
onOff = "true"
}
if DARKMODE_SWITCH {
dMcmd := exec.Command("osascript", "-e", fmt.Sprintf("tell app \"System Events\" to tell appearance preferences to set dark mode to %s", onOff))
dMcmd.Run()
nScmd := exec.Command("shortcuts", "run", fmt.Sprintf("nightshift %s", onOff))
nScmd.Run()
}
}
func Printer(nowEvent, nowDuration, nextEvent, nextDuration string) string {
return fmt.Sprintf(":%s: %s\n---\n:%s: %s | [symbolize = true]", nowEvent, nowDuration, nextEvent, nextDuration)
}
func getData() (sunData, error) {
var sdata sunData
var data map[string]interface{}
home, err := os.UserHomeDir()
if err != nil {
return sdata, err
}
f, err := os.Open(fmt.Sprintf("%s/.sun.json", home))
if err != nil {
return sdata, err
}
stat, err := f.Stat()
if err != nil {
return sdata, err
}
mtime := stat.ModTime()
now := time.Now().Truncate(time.Hour * 24)
if !mtime.Before(now) {
res := make([]byte, stat.Size())
f.Read(res)
err = json.Unmarshal(res, &data)
if err != nil {
return sdata, err
}
parsedSunrise, parsedSunset, err := parseDates(data)
if err != nil {
return sdata, err
}
sdata.Sunrise = parsedSunrise
sdata.Sunset = parsedSunset
return sdata, nil
} else {
sdata, err = getSunData()
if err != nil {
return sdata, err
}
return sdata, nil
}
}
func getSunData() (sunData, error) {
lat, long, err := getLocation()
if err != nil {
return sunData{}, err
}
var sdata sunData
req, err := http.NewRequest("GET", "https://api.ipgeolocation.io/astronomy", nil)
if err != nil {
return sdata, err
}
qeryString := req.URL.Query()
qeryString.Add("apiKey", API_KEY)
qeryString.Add("lat", lat)
qeryString.Add("long", long)
req.URL.RawQuery = qeryString.Encode()
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return sdata, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return sdata, err
}
if resp.StatusCode != http.StatusOK {
return sdata, fmt.Errorf("%s\n%s", resp.Status, body)
}
home, err := os.UserHomeDir()
if err != nil {
return sdata, err
}
err = os.WriteFile(fmt.Sprintf("%s/.sun.json", home), body, 0600)
if err != nil {
return sdata, err
}
var rdata map[string]interface{}
err = json.Unmarshal(body, &rdata)
if err != nil {
return sdata, err
}
sunrise, sunset, err := parseDates(rdata)
if err != nil {
return sdata, err
}
sdata.Sunrise = sunrise
sdata.Sunset = sunset
return sdata, nil
}
func parseDates(dates map[string]interface{}) (time.Time, time.Time, error) {
sunRiseSplit := strings.Split(dates["sunrise"].(string), ":")
sunSetSplit := strings.Split(dates["sunset"].(string), ":")
sunRiseHour, err := strconv.Atoi(sunRiseSplit[0])
if err != nil {
return time.Time{}, time.Time{}, err
}
SunRiseMinute, err := strconv.Atoi(sunRiseSplit[1])
if err != nil {
return time.Time{}, time.Time{}, err
}
sunSetHour, err := strconv.Atoi(sunSetSplit[0])
if err != nil {
return time.Time{}, time.Time{}, err
}
sunSetMinute, err := strconv.Atoi(sunSetSplit[1])
if err != nil {
return time.Time{}, time.Time{}, err
}
now := time.Now()
sunrise := time.Date(now.Year(), now.Month(), now.Day(), sunRiseHour, SunRiseMinute, 0, 0, time.Local)
sunset := time.Date(now.Year(), now.Month(), now.Day(), sunSetHour, sunSetMinute, 0, 0, time.Local)
return sunrise, sunset, nil
}
func eventDurationFromNow(event time.Time) string {
d := time.Until(event)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
if h == 0 {
return fmt.Sprintf("%dm", m)
} else {
return fmt.Sprintf("%dh %dm", h, m)
}
}
type sunData struct {
Sunrise time.Time
Sunset time.Time
}
func getLocation() (string, string, error) {
resp, err := http.Get("https://ifconfig.co/json")
if err != nil {
return "", "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", "", err
}
var data map[string]interface{}
err = json.Unmarshal(body, &data)
if err != nil {
return "", "", err
}
lat := strconv.FormatFloat(data["latitude"].(float64), 'f', 2, 64)
long := strconv.FormatFloat(data["longitude"].(float64), 'f', 2, 64)
return lat, long, nil
}