-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (84 loc) · 2.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
package main
import (
"log"
"os"
"strings"
"time"
"go-nostrss/nostr"
"go-nostrss/types"
"go-nostrss/utils"
"github.com/mmcdole/gofeed"
)
// FetchRSSFeed fetches and parses the RSS feed
func FetchRSSFeed(url string) ([]*gofeed.Item, error) {
parser := gofeed.NewParser()
feed, err := parser.ParseURL(url)
if err != nil {
return nil, err
}
return feed.Items, nil
}
func main() {
const configFileName = "config.yml"
var config *types.Config
if _, err := os.Stat(configFileName); os.IsNotExist(err) {
log.Println("Configuration file not found. Starting setup wizard...")
var setupErr error
config, setupErr = utils.SetupConfig(configFileName)
if setupErr != nil {
log.Fatalf("Error setting up configuration: %v", setupErr)
}
} else {
var loadErr error
config, loadErr = utils.LoadConfig(configFileName)
if loadErr != nil {
log.Fatalf("Error loading configuration: %v", loadErr)
}
}
cache, err := utils.LoadCache(config.CacheFile)
if err != nil {
log.Fatalf("Error loading cache: %v", err)
}
ticker := time.NewTicker(time.Duration(config.FetchIntervalMins) * time.Minute)
defer ticker.Stop()
for range ticker.C {
items, err := FetchRSSFeed(config.RSSFeed)
if err != nil {
log.Printf("Error fetching RSS feed: %v", err)
continue
}
for _, item := range items {
cache.Mu.Lock()
if cache.PostedLinks[item.Link] {
cache.Mu.Unlock()
continue
}
cache.Mu.Unlock()
content := strings.TrimSpace(item.Title) + "\n" + item.Link
var createdAt int64
if item.PublishedParsed != nil {
createdAt = item.PublishedParsed.Unix()
} else {
createdAt = time.Now().Unix()
}
event, err := nostr.CreateNostrEvent(content, config.NostrPublicKey, createdAt)
if err != nil {
log.Printf("Error creating Nostr event: %v", err)
continue
}
err = nostr.SignAndSendEvent(event, config.NostrPrivateKey, config.RelayURL)
if err != nil {
log.Printf("Error sending Nostr event: %v", err)
continue
}
cache.Mu.Lock()
cache.PostedLinks[item.Link] = true
cache.Mu.Unlock()
log.Printf("Posted event: %s", event.ID)
}
err = utils.SaveCache(config.CacheFile, cache)
if err != nil {
log.Printf("Error saving cache: %v", err)
}
}
}