forked from XiovV/Golty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_handlers.go
200 lines (183 loc) · 4.45 KB
/
database_handlers.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
const (
TypeChannel = "Channel"
TypePlaylist = "Playlist"
)
var confs = map[string]*Database{
TypeChannel: &Database{RWMutex: &sync.RWMutex{}, file: "channels.json"},
TypePlaylist: &Database{RWMutex: &sync.RWMutex{}, file: "playlists.json"},
}
func init() {
initLogFile()
CreateDirIfNotExist("./config")
initDatabase()
for _, v := range confs {
if err := v.load(); err != nil {
log.Fatal(nil)
}
}
}
func (db *Database) write() error {
b, err := json.MarshalIndent(&db.contents, "", " ")
if err != nil {
err = fmt.Errorf("database.write: could not marshal json: %w", err)
log.Error(err)
return err
}
err = ioutil.WriteFile(filepath.Join(CONFIG_ROOT, db.file), b, 0644)
if err != nil {
err = fmt.Errorf("database.write: could not write file %w", err)
log.Error(err)
return err
}
return nil
}
func (db *Database) load() error {
b, err := ioutil.ReadFile(filepath.Join(CONFIG_ROOT, db.file))
if err != nil {
err = fmt.Errorf("database.load: could not read file: %w", err)
log.Error(err)
return err
}
err = json.Unmarshal(b, &db.contents)
if err != nil {
err = fmt.Errorf("database.load: could not read file: %w", err)
log.Error(err)
return err
}
db.lookup = make(map[string]*DownloadTarget)
for i := range db.contents {
db.lookup[db.contents[i].URL] = &db.contents[i]
}
return nil
}
func UpdateCheckingInterval(file, interval string) error {
log.Info("update checking interval")
cf, ok := confs[file]
if !ok {
return fmt.Errorf("UpdateCheckingInterval: bad conf type")
}
if len(cf.contents) == 0 {
return fmt.Errorf("UpdateCheckingInterval: empty config list")
}
cf.Lock()
cf.contents[0].CheckingInterval = interval
cf.write()
cf.Unlock()
return nil
}
func updateDB(file, url string, f func(t *DownloadTarget) error) error {
cf, ok := confs[file]
if !ok {
return fmt.Errorf("updateDB: bad conf type")
}
cf.Lock()
defer cf.Unlock()
t, ok := cf.lookup[url]
if !ok {
return fmt.Errorf("updateDB: no such URL")
}
err := f(t)
if err != nil {
return err
}
return cf.write()
}
func (t DownloadTarget) UpdateLastChecked() error {
return updateDB(t.Type, t.URL, func(t *DownloadTarget) error {
t.LastChecked = time.Now().Format("01-02-2006 15:04:05")
log.Info("last checked date and time updated successfully")
return nil
})
}
func (t DownloadTarget) UpdateLatestDownloaded(videoId string) error {
return updateDB(t.Type, t.URL, func(t *DownloadTarget) error {
t.LatestDownloaded = videoId
log.Info("latest downloaded video id updated successfully")
return nil
})
}
func (t DownloadTarget) DoesExist() (bool, error) {
cf, ok := confs[t.Type]
if !ok {
return false, fmt.Errorf("DoesExist: bad conf type")
}
cf.RLock()
_, ok = cf.lookup[t.URL]
cf.RUnlock()
return ok, nil
}
func (t DownloadTarget) AddToDatabase() error {
cf, ok := confs[t.Type]
if !ok {
return fmt.Errorf("updateDB: bad conf type")
}
cf.Lock()
cf.contents = append(cf.contents, t)
cf.lookup[t.URL] = &cf.contents[len(cf.contents)-1]
cf.Unlock()
return nil
}
func (t DownloadTarget) UpdateDownloadHistory(videoId string) error {
return updateDB(t.Type, t.URL, func(t *DownloadTarget) error {
t.DownloadHistory = append(t.DownloadHistory, videoId)
log.Info(t.DownloadHistory)
return nil
})
}
func (t DownloadTarget) Delete() error {
cf, ok := confs[t.Type]
if !ok {
return fmt.Errorf("Delete: bad conf type")
}
cf.Lock()
defer cf.write()
defer cf.Unlock()
delete(cf.lookup, t.URL)
for i := range cf.contents {
if cf.contents[i].URL == t.URL {
cf.contents = RemoveAtIndex(cf.contents, i)
log.Info("successfully removed channel from channels.json")
return nil
}
}
return fmt.Errorf("DownloadTarget.Delete: no such target")
}
func (t DownloadTarget) GetFromDatabase() (out DownloadTarget, err error) {
cf, ok := confs[t.Type]
if !ok {
err = fmt.Errorf("GetFromDatabase: bad conf type")
return
}
cf.RLock()
defer cf.RUnlock()
ft, ok := cf.lookup[t.URL]
if !ok {
err = fmt.Errorf("GetFromDatabase: no such URL")
return
}
out = *ft
return
}
func GetCheckingInterval(file string) (int, error) {
cf, ok := confs[file]
if !ok {
return 0, fmt.Errorf("GetFromDatabase: bad conf type")
}
cf.RLock()
defer cf.RUnlock()
if len(cf.contents) == 0 {
return 0, fmt.Errorf("GetCheckingInterval: empty target list")
}
return strconv.Atoi(cf.contents[0].CheckingInterval)
}