fead: expand archive options

add an option to select how to archive the entries
add an option for what entries to archive
change the concurrentcy limit to a semaphore instead of a ticker
This commit is contained in:
Alexander Navarro 2024-12-20 14:59:43 -03:00
parent 1788241cd5
commit b0506c3eab
9 changed files with 99 additions and 72 deletions

View file

@ -1,35 +1,17 @@
package app
import (
"sync"
"time"
"context"
"git.alecodes.page/alecodes/miniflux-archiver/internal/logger"
"git.alecodes.page/alecodes/miniflux-archiver/internal/miniflux"
"git.alecodes.page/alecodes/miniflux-archiver/internal/service"
"golang.org/x/sync/semaphore"
)
// NewRateLimiter creates a rate limiter that emits time events at a specified rate.
// request_per specifies the number of requests allowed per time_scale duration.
// time_scale specifies the duration over which the requests are allowed.
func newRateLimiter(request_per int, time_scale time.Duration) <-chan time.Time {
rate_limit := make(chan time.Time, request_per)
tickrate := time_scale / time.Duration(request_per)
for range request_per {
rate_limit <- time.Now()
}
go func() {
for t := range time.Tick(tickrate) {
rate_limit <- t
}
}()
return rate_limit
}
func Archive(minifluxConfig miniflux.MinifluxConfig, serviceConfig service.ServiceConfig) {
ctx := context.Background()
mf, err := miniflux.NewMiniflux(minifluxConfig)
if err != nil {
logger.Fatal("Could not connect to the miniflux server: %v", err)
@ -45,17 +27,16 @@ func Archive(minifluxConfig miniflux.MinifluxConfig, serviceConfig service.Servi
logger.Fatal("Could not retrieve entries from the miniflux feed: %v", err)
}
var wg sync.WaitGroup
ticker := newRateLimiter(5, 10*time.Second)
sem := semaphore.NewWeighted(int64(serviceConfig.MaxRequests))
for _, entry := range result.Entries {
wg.Add(1)
if err := sem.Acquire(ctx, 1); err != nil {
logger.Fatal("Failed to acquire semaphore: %v", err)
}
go func() {
defer wg.Done()
<-ticker
defer sem.Release(1)
// logger.Debug("Sending url \"%v\" to be marked as read", entry.URL)
err := externalService.Archive(entry.URL)
if err != nil {
logger.Fatal("Could not archive url \"%v\" from the service: %v", entry.URL, err)
@ -65,5 +46,7 @@ func Archive(minifluxConfig miniflux.MinifluxConfig, serviceConfig service.Servi
}()
}
wg.Wait()
if err := sem.Acquire(ctx, int64(serviceConfig.MaxRequests)); err != nil {
logger.Fatal("Failed to acquire semaphore: %v", err)
}
}

View file

@ -1,17 +0,0 @@
package config
import "git.alecodes.page/alecodes/miniflux-archiver/internal/service"
type MinifluxConfig struct {
Host string
User string
Token string
FeedId int64
}
type ServiceConfig struct {
Service service.ServiceOption
Host string
User string
Token string
}

View file

@ -5,11 +5,14 @@ import (
mfApi "miniflux.app/v2/client"
)
type Filter = mfApi.Filter
type MinifluxConfig struct {
Host string
User string
Token string
FeedId int64
Host string
User string
Token string
FeedId int64
FeedFilter *Filter
}
type Miniflux struct {
@ -18,11 +21,13 @@ type Miniflux struct {
}
func (mf *Miniflux) GetEntries() (*mfApi.EntryResultSet, error) {
filter := &mfApi.Filter{
Statuses: []string{mfApi.EntryStatusRead, mfApi.EntryStatusRemoved},
}
mf.FeedFilter.Statuses = []string{mfApi.EntryStatusRead, mfApi.EntryStatusRemoved}
return mf.client.FeedEntries(mf.FeedId, filter)
if mf.FeedId == -1 {
return mf.client.Entries(mf.FeedFilter)
} else {
return mf.client.FeedEntries(mf.FeedId, mf.FeedFilter)
}
}
func NewMiniflux(config MinifluxConfig) (*Miniflux, error) {

View file

@ -52,6 +52,18 @@ func (ld *Linkding) Archive(url string) error {
TagNames: bookmark.TagNames,
}
switch ld.ServiceConfig.Method {
case ServiceArchiveMethodArchive:
payload.IsArchived = true
case ServiceArchiveMethodSeen:
payload.Unread = false
case ServiceArchiveMethodBoth:
payload.Unread = false
payload.IsArchived = true
default:
return fmt.Errorf("Archive method is invalid")
}
if payload.TagNames == nil {
payload.TagNames = []string{}
}

View file

@ -1,6 +1,8 @@
package service
import "fmt"
import (
"fmt"
)
type ServiceOption string
@ -8,11 +10,21 @@ const (
ServiceLinkding ServiceOption = "linkding"
)
type ServiceArchiveMethod string
const (
ServiceArchiveMethodSeen = "seen"
ServiceArchiveMethodArchive = "archive"
ServiceArchiveMethodBoth = "both"
)
type ServiceConfig struct {
Service ServiceOption
Host string
User string
Token string
Service ServiceOption
Host string
User string
Token string
Method ServiceArchiveMethod
MaxRequests uint8
}
type Service interface {