77 lines
1.4 KiB
Go
77 lines
1.4 KiB
Go
package events
|
|
|
|
import (
|
|
"reflect"
|
|
"time"
|
|
|
|
"git.quimbo.fr/odwrtw/canape/backend/users"
|
|
"github.com/odwrtw/papi"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// TorrentNotifier is a struct implementing the Notifier interface
|
|
type TorrentNotifier struct {
|
|
*Notifier
|
|
user *users.User
|
|
client *papi.Client
|
|
torrents []papi.Torrent
|
|
log *logrus.Entry
|
|
}
|
|
|
|
// Launch implements the Notifier interface
|
|
func (t *TorrentNotifier) Launch() {
|
|
// Create the timer that will check for the torrents every X seconds
|
|
timeTicker := time.NewTicker(10 * time.Second)
|
|
defer timeTicker.Stop()
|
|
|
|
// Create a new papi client
|
|
client, err := t.user.NewPapiClient()
|
|
if err != nil {
|
|
return
|
|
}
|
|
t.client = client
|
|
|
|
err = t.torrentsUpdate()
|
|
if err != nil {
|
|
t.log.Warnf("got error getting torrent update: %s", err)
|
|
return
|
|
}
|
|
|
|
for {
|
|
select {
|
|
case <-timeTicker.C:
|
|
err := t.torrentsUpdate()
|
|
if err != nil {
|
|
t.log.Warnf("got error getting torrent update: %s", err)
|
|
return
|
|
}
|
|
case <-t.done:
|
|
t.log.Info("quit torrent notifier")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (t *TorrentNotifier) torrentsUpdate() error {
|
|
// Get torrents
|
|
torrents, err := t.client.GetTorrents()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if reflect.DeepEqual(t.torrents, torrents) {
|
|
return nil
|
|
}
|
|
|
|
// If they're different, send the event
|
|
event := ServerEvent{
|
|
Type: "torrents",
|
|
Data: torrents,
|
|
}
|
|
t.eventStream <- event
|
|
|
|
t.torrents = torrents
|
|
|
|
return nil
|
|
}
|