108 lines
2.0 KiB
Go
108 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/br0xen/boltease"
|
|
)
|
|
|
|
type model struct {
|
|
bolt *boltease.DB
|
|
dbFileName string
|
|
|
|
Users []User
|
|
Comics []Comic
|
|
|
|
Site *SiteData
|
|
}
|
|
|
|
func NewModel() (*model, error) {
|
|
var err error
|
|
m := new(model)
|
|
m.dbFileName = DbName
|
|
m.bolt, err = boltease.Create(m.dbFileName, 0600, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err = m.initDB(); err != nil {
|
|
return nil, errors.New("Unable to initialzie DB: " + err.Error())
|
|
}
|
|
m.LoadSiteData()
|
|
if err = m.LoadUsers(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err = m.LoadComics(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
func (m *model) initDB() error {
|
|
var err error
|
|
if err = m.bolt.OpenDB(); err != nil {
|
|
return err
|
|
}
|
|
defer m.bolt.CloseDB()
|
|
|
|
if err = m.bolt.MkBucketPath([]string{"site"}); err != nil {
|
|
return err
|
|
}
|
|
if err = m.bolt.MkBucketPath([]string{"users"}); err != nil {
|
|
return err
|
|
}
|
|
if err = m.bolt.MkBucketPath([]string{"comics"}); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *model) saveChanges() {
|
|
m.Site.LastSave = time.Now()
|
|
m.SaveSite()
|
|
//m.SaveAllComics(m.Comics)
|
|
m.SaveAllUsers(m.Users)
|
|
}
|
|
|
|
func (m *model) UpdateAllUserFeeds() {
|
|
var allSubs []string
|
|
for _, usr := range m.Users {
|
|
// Pull all user subs
|
|
for _, sub := range usr.SubSlugs {
|
|
allSubs = addStringIfUnique(sub, allSubs)
|
|
}
|
|
}
|
|
// So we have allSubs which contains all subscribed comics for all users
|
|
for _, sub := range allSubs {
|
|
fmt.Println("Updating Comic: " + sub)
|
|
pts := strings.Split(sub, ";")
|
|
if len(pts) != 2 {
|
|
continue
|
|
}
|
|
c, err := m.GetComic(pts[0], pts[1])
|
|
if err != nil {
|
|
fmt.Println(sub, ":", err)
|
|
continue
|
|
}
|
|
if err = c.Update(); err != nil {
|
|
fmt.Println(sub, ":", err.Error())
|
|
continue
|
|
}
|
|
if err = m.SaveComic(c); err != nil {
|
|
fmt.Println(sub, ":", err.Error())
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
type Source interface {
|
|
downloadList() []Comic
|
|
getRssItem(slug string) (string, error)
|
|
getUrl(slug string, dt time.Time) (string, error)
|
|
getDescription(slug string, dt time.Time) (string, error)
|
|
}
|