@@ -2,10 +2,12 @@ package main
2
2
3
3
import (
4
4
"log"
5
+ "os"
5
6
"strings"
6
7
"time"
7
8
8
9
"go-nostrss/nostr"
10
+ "go-nostrss/types"
9
11
"go-nostrss/utils"
10
12
11
13
"github.com/mmcdole/gofeed"
@@ -22,23 +24,33 @@ func FetchRSSFeed(url string) ([]*gofeed.Item, error) {
22
24
}
23
25
24
26
func main () {
25
- // Load configuration
26
- config , err := utils .LoadConfig ("config.yml" )
27
- if err != nil {
28
- log .Fatalf ("Error loading configuration: %v" , err )
27
+ const configFileName = "config.yml"
28
+
29
+ var config * types.Config
30
+ if _ , err := os .Stat (configFileName ); os .IsNotExist (err ) {
31
+ log .Println ("Configuration file not found. Starting setup wizard..." )
32
+ var setupErr error
33
+ config , setupErr = utils .SetupConfig (configFileName )
34
+ if setupErr != nil {
35
+ log .Fatalf ("Error setting up configuration: %v" , setupErr )
36
+ }
37
+ } else {
38
+ var loadErr error
39
+ config , loadErr = utils .LoadConfig (configFileName )
40
+ if loadErr != nil {
41
+ log .Fatalf ("Error loading configuration: %v" , loadErr )
42
+ }
29
43
}
30
44
31
- // Load cache
32
45
cache , err := utils .LoadCache (config .CacheFile )
33
46
if err != nil {
34
47
log .Fatalf ("Error loading cache: %v" , err )
35
48
}
36
49
37
- // Main loop
38
50
ticker := time .NewTicker (time .Duration (config .FetchIntervalMins ) * time .Minute )
39
51
defer ticker .Stop ()
40
52
41
- for {
53
+ for range ticker . C {
42
54
items , err := FetchRSSFeed (config .RSSFeed )
43
55
if err != nil {
44
56
log .Printf ("Error fetching RSS feed: %v" , err )
@@ -47,25 +59,21 @@ func main() {
47
59
48
60
for _ , item := range items {
49
61
cache .Mu .Lock ()
50
- alreadyPosted := cache .PostedLinks [item .Link ]
51
- cache .Mu .Unlock ()
52
-
53
- if alreadyPosted {
62
+ if cache .PostedLinks [item .Link ] {
63
+ cache .Mu .Unlock ()
54
64
continue
55
65
}
66
+ cache .Mu .Unlock ()
56
67
57
- // Prepare event content
58
68
content := strings .TrimSpace (item .Title ) + "\n " + item .Link
59
69
60
- // Use the article's publish time for the event's created_at field
61
70
var createdAt int64
62
71
if item .PublishedParsed != nil {
63
72
createdAt = item .PublishedParsed .Unix ()
64
73
} else {
65
- createdAt = time .Now ().Unix () // Fallback to current time if not available
74
+ createdAt = time .Now ().Unix ()
66
75
}
67
76
68
- // Create Nostr event with the article's publish time and content
69
77
event , err := nostr .CreateNostrEvent (content , config .NostrPublicKey , createdAt )
70
78
if err != nil {
71
79
log .Printf ("Error creating Nostr event: %v" , err )
@@ -89,7 +97,6 @@ func main() {
89
97
if err != nil {
90
98
log .Printf ("Error saving cache: %v" , err )
91
99
}
92
-
93
- <- ticker .C
94
100
}
101
+
95
102
}
0 commit comments