-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (99 loc) · 3.05 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
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"pinboard-popular-feed/data"
)
func main() {
// handle dry-run command line flag
dryRun := flag.Bool("dryrun", false, "scan for new posts but don't post to mastodon")
flag.Parse()
// set up logging
logFile, err := os.OpenFile("pinboard-popular-feed.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Println("error opening log file")
os.Exit(1)
}
defer logFile.Close()
log.SetOutput(logFile)
log.Println("starting pinboard-popular-feed")
// set up mastodon credentials
mastodonCredentials, err := buildMastodonCredentials()
if err != nil {
os.Exit(1)
}
popular, err := fetchCurrentPinboardPopular()
if err != nil {
log.Println("error scraping popular bookmarks")
os.Exit(1)
}
// initialize the bookmark store
//db := data.Init()
var db = data.BookmarkStore{}
db.InitStore(data.CreateDBConfigFromEnv())
postCount, err := postNewLinks(popular, db, mastodonCredentials, *dryRun)
if err != nil {
log.Println("error posting new bookmarks")
os.Exit(1)
}
log.Println("posted " + fmt.Sprint(postCount) + " new bookmarks")
log.Println("finished pinboard-popular-feed")
}
func buildMastodonCredentials() (MastodonCredentials, error) {
if os.Getenv("MASTODON_ACCESS_TOKEN") == "" {
log.Println("MASTODON_ACCESS_TOKEN not set")
return MastodonCredentials{}, errors.New("MASTODON_ACCESS_TOKEN not set")
}
if os.Getenv("MASTODON_SERVER_DOMAIN") == "" {
log.Println("MASTODON_SERVER_DOMAIN not set")
return MastodonCredentials{}, errors.New("MASTODON_SERVER_DOMAIN not set")
}
return MastodonCredentials{
accessToken: os.Getenv("MASTODON_ACCESS_TOKEN"),
serverDomain: os.Getenv("MASTODON_SERVER_DOMAIN"),
}, nil
}
func postNewLinks(popular []*data.Bookmark, db data.BookmarkStore, mastodonCredentials MastodonCredentials, dryRun bool) (int, error) {
//TODO there should be an if statement here
log.Println("dryrun: not posting any new bookmarks")
var postCount int
for i := 0; i < len(popular); i++ {
found, err := db.FindBookmark(popular[i].BookmarkId)
if err != nil {
log.Println("error finding bookmark in store: " + popular[i].BookmarkId)
return postCount, err
}
if !found {
if dryRun {
log.Println("dry run: new bookmark found, but not posted")
} else {
db.StoreBookmark(*popular[i])
log.Println("new bookmark stored")
postCount++
TootBookmark(*popular[i], mastodonCredentials)
}
}
// TODO implement some kind of rate limiting?
// https://docs.joinmastodon.org/api/rate-limits/
}
return postCount, nil
}
func fetchCurrentPinboardPopular() ([]*data.Bookmark, error) {
popular, err := ScrapePinboardPopular()
if err != nil {
log.Println("error scraping pinboard popular")
return nil, err
}
log.Println("current popular bookmarks: ")
for i := 0; i < len(popular); i++ {
log.Println(popular[i].BookmarkId)
log.Println(popular[i].Title)
log.Println(popular[i].Url)
log.Println()
}
log.Println("found " + fmt.Sprint(len(popular)) + " bookmarks on pinboard popular")
return popular, err //TODO handle any errors
}