-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
80 lines (65 loc) · 1.59 KB
/
helpers.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
package main
import (
"database/sql"
"log"
"os"
"time"
"github.com/hashicorp/logutils"
"github.com/nbd-wtf/go-nostr"
)
func getEnv(key, fallback string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return fallback
}
func convertTimeString(itemTime string) *time.Time {
// find right date format
postTime, err := time.Parse(time.RFC3339, itemTime)
if err != nil {
postTime, err = time.Parse(time.RFC1123Z, itemTime) // try other one
}
if err != nil {
postTime, err = time.Parse(time.RFC1123, itemTime) // try other one
}
if err != nil {
log.Println("[WARN] Can't parse element time")
}
return &postTime
}
func checkMaxAge(itemTime *time.Time, maxAgeHours time.Duration) bool {
maxAge := time.Now().Add(-fetchInterval)
//postTime := convertTimeString(itemTime)
if itemTime.After(maxAge) {
return true
}
return false
}
func dbInit() *sql.DB {
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
log.Fatal("[FATAL] open db: %v", err)
}
log.Printf("[INFO] database opened at %s", dbPath)
//defer db.Close()
_, err = db.Exec(sqlInit)
if err != nil {
log.Printf("%q: %s\n", err, sqlInit)
}
return db
}
func generateKeysForUrl(feedUrl string) *feedStruct {
feedElem := feedStruct{}
feedElem.Url = feedUrl
feedElem.Sec = nostr.GeneratePrivateKey() // generate new key
feedElem.Pub, _ = nostr.GetPublicKey(feedElem.Sec)
return &feedElem
}
func logger() {
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "INFO", "WARN", "ERROR", "FATAL"},
MinLevel: logutils.LogLevel(logLevel),
Writer: os.Stderr,
}
log.SetOutput(filter)
}