-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (137 loc) · 4.21 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"time"
"github.com/codeformuenster/dkan-newest-dataset-notifier/datasets"
"github.com/codeformuenster/dkan-newest-dataset-notifier/externalservices"
"github.com/codeformuenster/dkan-newest-dataset-notifier/s3"
"github.com/codeformuenster/dkan-newest-dataset-notifier/tooter"
"github.com/codeformuenster/dkan-newest-dataset-notifier/util"
)
const defaultDKANInstance = "https://opendata.stadt-muenster.de"
var (
dkanInstanceURL, localPath, externalServicesConfigPath string
enableTweeter, allowEmpty, tootUnlisted, reverseDkanResponse bool
)
// How this works (at least in my head)
//
// - load local (previous data.json)
// - Download data.json
// - Compare with previous data.json
func main() {
flag.BoolVar(&enableTweeter, "enable-tooter", false, "enable the creation of toots")
flag.BoolVar(&allowEmpty, "allow-empty", false, "allow empty previous dataset, for initialization")
flag.BoolVar(&tootUnlisted, "toot-unlisted", false, "toot unlisted instead of public, for initialization")
flag.BoolVar(&reverseDkanResponse, "reverse-dkan-response", false, "reverse dataset list from DKAN, for initialization")
flag.StringVar(&dkanInstanceURL, "url", defaultDKANInstance, "base url of the dkan instance (https://...)")
flag.StringVar(&localPath, "local-path", "", "path to local json file for comparison")
flag.StringVar(&externalServicesConfigPath, "config-path", "", "path to local json external services configuration")
flag.Parse()
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
var err error
// validate + constructr dkan urls
datasetsURL, err := util.MakeURL(fmt.Sprintf("%s/%s", dkanInstanceURL, "data.json"))
if err != nil {
log.Printf("Could not create valid datasets URL")
log.Panicln(err)
}
cfg, err := externalservices.FromFile(externalServicesConfigPath)
if err != nil {
log.Println(err)
log.Println("Disabling external services")
}
s3Instance, s3Available := setupS3(cfg.S3Config)
t, tooterAvailable, err := setupMastodon(cfg.MastodonConfig)
if err != nil {
log.Panicln(err)
}
if !tooterAvailable {
log.Println("disabling tooter, no toots will be created")
}
var prevDatasets datasets.Datasets
if s3Available {
prevDatasets, err = datasets.FromS3(s3Instance)
} else {
if localPath == "" {
localPath = makeDataPath(time.Now().Add(-24 * time.Hour))
log.Printf("empty local-path flag, assuming path %s\n", localPath)
}
prevDatasets, err = datasets.FromPath(localPath)
}
// handle error of prev dataset fetch
if err != nil {
log.Println(err)
log.Println("Reading previous datasets failed, assuming empty")
}
if !allowEmpty && len(prevDatasets.Dataset) == 0 {
log.Println("Empty previous datasets not allowed")
return
}
currDatasets, err := datasets.FromURL(datasetsURL)
if err != nil {
log.Panicln(err)
}
if reverseDkanResponse {
prevDatasets.Reverse()
currDatasets.Reverse()
}
missing := currDatasets.Compare(&prevDatasets)
for _, m := range missing {
tootText, err := m.ToTootText(dkanInstanceURL)
if err != nil {
log.Println(err)
continue
}
log.Printf("%d %s\n", len(tootText), tootText)
if tooterAvailable {
visibility := "public"
if tootUnlisted {
visibility = "unlisted"
}
err = t.SendToot(tootText, visibility)
if err != nil {
log.Println(err)
continue
}
time.Sleep(10 * time.Second)
}
}
{
var err error
if s3Available {
if len(missing) != 0 {
err = currDatasets.SaveToS3(fmt.Sprintf("data-%s.json", time.Now().Format("2006-01-02")), s3Instance)
}
} else {
err = currDatasets.Save(makeDataPath(time.Now()))
}
if err != nil {
log.Panicln(err)
}
}
}
func setupMastodon(cfg externalservices.MastodonConfig) (tooter.Tooter, bool, error) {
if !enableTweeter || !cfg.Validate() {
return tooter.Tooter{}, false, nil
}
t, err := tooter.NewTooter(cfg)
if err != nil {
return t, false, err
}
return t, true, nil
}
func setupS3(cfg externalservices.S3Config) (s3.S3, bool) {
return s3.NewS3(cfg), cfg.Validate()
}
func makeDataPath(date time.Time) string {
filename := fmt.Sprintf("data-%s.json", date.Format("2006-01-02"))
dir, err := os.Getwd()
if err != nil {
return "./" + filename
}
return path.Join(dir, filename)
}