This commit is contained in:
Bel LaPointe
2021-03-28 13:12:15 -05:00
commit be2a031834
46 changed files with 11740 additions and 0 deletions

80
arlo/arlo.go Executable file
View File

@@ -0,0 +1,80 @@
package arlo
import (
"fmt"
"local/logb"
"local/sandbox/arlo-cleaner/config"
"time"
"github.com/jeffreydwalter/arlo-go"
)
type Arlo struct {
client *arlo.Arlo
}
func New() (*Arlo, error) {
client, err := arlo.Login(config.ArloUser, config.ArloPass)
if err != nil {
err = fmt.Errorf("failed to arlo.New: %v", err)
}
logb.Debugf("logged into arlo @%v", config.ArloUser)
return &Arlo{
client: client,
}, err
}
func (a *Arlo) ListSince(since time.Time) ([]*Video, error) {
library, err := a.client.GetLibrary(since, time.Now())
if err != nil {
return nil, err
}
videos := make([]*Video, len(*library))
for i, recording := range *library {
videos[i] = NewVideo(recording)
}
logb.Debugf("found %v videos in library", len(videos))
return videos, nil
}
func (a *Arlo) DeleteVideos(videos []*Video) error {
library := make([]arlo.Recording, len(videos))
for i, video := range videos {
library[i] = video.Recording
}
lib := arlo.Library(library)
logb.Infof("deleting %v videos from library", len(videos))
if config.DryRun {
logb.Infof("delete", videos)
return nil
}
return a.client.BatchDeleteRecordings(&lib)
}
func (a *Arlo) DeleteVideo(video *Video) error {
if config.DryRun {
logb.Infof("delete", video)
return nil
}
logb.Infof("deleting %v videos from library", 1)
return a.DeleteVideos([]*Video{video})
}
func (a *Arlo) Usage() (int64, error) {
videos, err := a.ListSince(time.Now().Add(-1 * 24 * 30 * time.Hour))
if err != nil {
return 0, err
}
var current int64
for _, video := range videos {
current += video.Size()
}
logb.Debugf("arlo probably using %v B (%v MB) over %v videos", current, current/1000/1000, len(videos))
return current, nil
}
func (a *Arlo) NeedsPruning() (bool, error) {
usage, err := a.Usage()
logb.Debugf("arlo needs pruning = %v > %v", usage, config.ArloCapacity)
return usage > config.ArloCapacity, err
}

73
arlo/video.go Executable file
View File

@@ -0,0 +1,73 @@
package arlo
import (
"fmt"
"io"
"local/logb"
"local/sandbox/arlo-cleaner/config"
"net/http"
"net/url"
"path"
"strings"
"time"
"github.com/jeffreydwalter/arlo-go"
)
const TSFormat = "2006_01_02_15_04"
type Video struct {
Duration time.Duration
Loc *url.URL
Created time.Time
Device string
ID string
Recording arlo.Recording
}
func NewVideo(recording arlo.Recording) *Video {
url, err := url.Parse(recording.PresignedContentUrl)
if err != nil {
panic(err)
}
return &Video{
Duration: time.Duration(recording.MediaDurationSecond) * time.Second,
Loc: url,
Created: time.Unix(0, int64(time.Millisecond)*recording.UtcCreatedDate),
Device: recording.DeviceId,
ID: recording.UniqueId,
Recording: recording,
}
}
func (v *Video) Key() string {
return path.Join(
fmt.Sprintf("%s.%s.%s",
v.Created.Format(TSFormat),
v.Device,
v.Loc.Path[strings.LastIndex(v.Loc.Path, ".")+1:],
),
)
}
func (v *Video) String() string {
return fmt.Sprintf("%s:{dur:%q,create:%q,url:%q}",
v.ID,
v.Duration.String(),
v.Created.String(),
v.Loc.String(),
)
}
func (v *Video) Download() (io.ReadCloser, error) {
resp, err := http.Get(v.Loc.String())
if err != nil {
return nil, err
}
logb.Infof("downloading arlo video from %v", v.Created)
return resp.Body, err
}
func (v *Video) Size() int64 {
return int64(v.Duration.Seconds()) * config.ArloBPS
}