summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/gos/main.go2
-rw-r--r--internal/config/args.go1
-rw-r--r--internal/schedule/schedule.go3
-rw-r--r--internal/schedule/stats.go25
4 files changed, 24 insertions, 7 deletions
diff --git a/cmd/gos/main.go b/cmd/gos/main.go
index 3a4a676..d77cd3d 100644
--- a/cmd/gos/main.go
+++ b/cmd/gos/main.go
@@ -26,6 +26,7 @@ func main() {
secretsConfigPath = *flag.String("secretsConfig", secretsConfigPath, "Gos' secret config")
platforms := flag.String("platforms", "Mastodon:500,LinkedIn:1000", "Platforms enabled plus their post size limits")
target := flag.Int("target", 2, "How many posts per week are the target?")
+ pauseDays := flag.Int("pauseDays", 3, "How many days until next post can be posted?")
lookback := flag.Int("lookback", 30, "How many days look back in time for posting history")
flag.Parse()
@@ -39,6 +40,7 @@ func main() {
GosDir: *gosDir,
Platforms: make(map[string]int),
Target: *target,
+ PauseDays: *pauseDays,
Lookback: time.Duration(*lookback) * time.Hour * 24,
SecretsConfigPath: secretsConfigPath,
Secrets: secrets,
diff --git a/internal/config/args.go b/internal/config/args.go
index 47712f9..995853a 100644
--- a/internal/config/args.go
+++ b/internal/config/args.go
@@ -14,6 +14,7 @@ type Args struct {
DryRun bool
Platforms map[string]int // Platform name and post size limits
Target int
+ PauseDays int
Lookback time.Duration
SecretsConfigPath string
Secrets Secrets
diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go
index 5cb80be..5512e4d 100644
--- a/internal/schedule/schedule.go
+++ b/internal/schedule/schedule.go
@@ -18,8 +18,6 @@ var (
ErrNothingQueued = errors.New("nothing queued")
)
-// TODO: Make it so that I don't post 2 days in a row, make it configurable how
-// many days between each post for each platform there should be.
func Run(args config.Args, platform string) (entry.Entry, error) {
dir := fmt.Sprintf("%s/db/platforms/%s", args.GosDir, strings.ToLower(platform))
stats, err := newStats(dir, args.Lookback, args.Target)
@@ -29,7 +27,6 @@ func Run(args config.Args, platform string) (entry.Entry, error) {
log.Println("For", platform, "stats:", stats)
if stats.targetHit() {
- log.Println("Target hit, not posting at", platform)
return entry.Zero, ErrNothingToSchedule
}
diff --git a/internal/schedule/stats.go b/internal/schedule/stats.go
index 172272f..bf5a12c 100644
--- a/internal/schedule/stats.go
+++ b/internal/schedule/stats.go
@@ -2,6 +2,7 @@ package schedule
import (
"fmt"
+ "log"
"os"
"path/filepath"
"time"
@@ -18,11 +19,12 @@ type stats struct {
sinceDays float64
postsPerDay float64
postsPerDayTarget float64
+ lastPostDaysAgo float64
}
func (s stats) String() string {
- return fmt.Sprintf("posted:%d,queued:%d,sinceDays:%v,postsPerDay:%v >? postsPerDayTarget:%v",
- s.posted, s.queued, s.sinceDays, s.postsPerDay, s.postsPerDayTarget,
+ return fmt.Sprintf("posted:%d,queued:%d,sinceDays:%v,postsPerDayTarget:%v>?%v,lastPostDaysAgo:%v",
+ s.posted, s.queued, s.sinceDays, s.postsPerDay, s.postsPerDayTarget, s.lastPostDaysAgo,
)
}
@@ -39,14 +41,24 @@ func newStats(dir string, lookback time.Duration, target int) (stats, error) {
return stats, nil
}
-func (s stats) targetHit() bool {
- return s.postsPerDay >= s.postsPerDayTarget
+func (s stats) targetHit(pauseDays int) bool {
+ if s.postsPerDay >= s.postsPerDayTarget {
+ log.Println("Posts per day target hit")
+ return true
+ }
+ if s.lastPostDaysAgo <= float64(pauseDays) {
+ log.Println("Need to wait a bit longer as last post isn't", pauseDays, "ago yet")
+ return true
+
+ }
+ return false
}
func (s *stats) gatherPostedStats(dir string, lookbackTime time.Time) error {
var (
now time.Time = timestamp.NowTime()
oldest time.Time = now
+ newest time.Time = timestamp.OldestValidTime()
)
err := oi.TraverseDir(dir, func(file os.DirEntry) error {
@@ -61,6 +73,9 @@ func (s *stats) gatherPostedStats(dir string, lookbackTime time.Time) error {
if ent.Time.Before(oldest) {
oldest = ent.Time
}
+ if ent.Time.After(newest) {
+ newest = ent.Time
+ }
s.posted++
return nil
})
@@ -71,6 +86,8 @@ func (s *stats) gatherPostedStats(dir string, lookbackTime time.Time) error {
since := now.Sub(oldest)
s.sinceDays = since.Abs().Hours() / 24
s.postsPerDay = float64(s.posted) / float64(s.sinceDays)
+ s.lastPostDaysAgo = now.Sub(newest).Hours() / 24.0
+
return nil
}