Skip to content

Commit

Permalink
feat: add anilist target implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
RobBrazier committed Nov 3, 2024
1 parent f2b5e15 commit bf3c1aa
Show file tree
Hide file tree
Showing 14 changed files with 520 additions and 121 deletions.
78 changes: 0 additions & 78 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
package config

import (
"errors"
"os"
"path/filepath"

"github.com/RobBrazier/readflow/internal"
"github.com/adrg/xdg"
"github.com/charmbracelet/log"
"gopkg.in/yaml.v3"
)

type Config struct {
Columns ColumnConfig `yaml:"columns"`
Databases DatabaseConfig `yaml:"databases"`
Expand Down Expand Up @@ -64,70 +53,3 @@ func GetTargets() []string {
func GetTokens() TokenConfig {
return config.Tokens
}

func GetConfigPath(override *string) string {
// Has the config flag been passed in? - if it's got a value, use it
if override != nil {
if *override != "" {
configPath = *override
}
}

if configPath == "" {
// look in the XDG_CONFIG_HOME location
var err error
configPath, err = xdg.ConfigFile(filepath.Join(internal.NAME, "config.yaml"))

if err != nil {
// if that doesn't work for some reason, fall back to the current dir
currentDir, err := os.Getwd()
if err != nil {
currentDir = "."
}
configPath = filepath.Join(currentDir, "readflow.yaml")
}
}

return configPath

}

func LoadConfig(path string) error {
log.Debug("Loading config from", "file", path)
data, err := os.ReadFile(path)
if err != nil {
return err
}
if err := yaml.Unmarshal(data, &config); err != nil {
return err
}
log.Debug("Successfully loaded config")
return nil
}

func SaveConfig(cfg *Config) error {
data, err := yaml.Marshal(cfg)
if err != nil {
return err
}

if _, err = os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
// file doesn't exist - lets create the folder structure required
path := filepath.Dir(configPath)
if path != "." {
err := os.MkdirAll(path, 0755)
if err != nil {
log.Error("Something went wrong when trying to create the folder structure for", "file", configPath, "path", path, "error", err)
}
}
}

err = os.WriteFile(configPath, data, 0644)
if err != nil {
log.Error("Couldn't save config to", "file", configPath)
return err
}
log.Debug("Successfully saved config to", "file", configPath)
config = *cfg
return nil
}
79 changes: 79 additions & 0 deletions internal/config/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package config

import (
"errors"
"os"
"path/filepath"

"github.com/RobBrazier/readflow/internal"
"github.com/adrg/xdg"
"github.com/charmbracelet/log"
"gopkg.in/yaml.v3"
)

func GetConfigPath(override *string) string {
// Has the config flag been passed in? - if it's got a value, use it
if override != nil {
if *override != "" {
configPath = *override
}
}

if configPath == "" {
// look in the XDG_CONFIG_HOME location
var err error
configPath, err = xdg.ConfigFile(filepath.Join(internal.NAME, "config.yaml"))

if err != nil {
// if that doesn't work for some reason, fall back to the current dir
currentDir, err := os.Getwd()
if err != nil {
currentDir = "."
}
configPath = filepath.Join(currentDir, "readflow.yaml")
}
}

return configPath

}

func LoadConfig(path string) error {
log.Debug("Loading config from", "file", path)
data, err := os.ReadFile(path)
if err != nil {
return err
}
if err := yaml.Unmarshal(data, &config); err != nil {
return err
}
log.Debug("Successfully loaded config")
return nil
}

func SaveConfig(cfg *Config) error {
data, err := yaml.Marshal(cfg)
if err != nil {
return err
}

if _, err = os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
// file doesn't exist - lets create the folder structure required
path := filepath.Dir(configPath)
if path != "." {
err := os.MkdirAll(path, 0755)
if err != nil {
log.Error("Something went wrong when trying to create the folder structure for", "file", configPath, "path", path, "error", err)
}
}
}

err = os.WriteFile(configPath, data, 0644)
if err != nil {
log.Error("Couldn't save config to", "file", configPath)
return err
}
log.Debug("Successfully saved config to", "file", configPath)
config = *cfg
return nil
}
20 changes: 0 additions & 20 deletions internal/form/form.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package form

import (
"errors"
"maps"

"github.com/RobBrazier/readflow/internal/source"
Expand Down Expand Up @@ -59,25 +58,6 @@ func TargetSelect(value *[]string) *huh.MultiSelect[string] {
Value(value)
}

func ValidationMinValues[T comparable](min int) func([]T) error {
return func(t []T) error {
if len(t) < min {
return errors.New("You must select at least one")
}
return nil
}
}

func ValidationRequired[T comparable]() func(T) error {
return func(t T) error {
var empty T
if t == empty {
return errors.New("This field is required")
}
return nil
}
}

func Confirm(message string, value *bool) *huh.Confirm {
return huh.NewConfirm().
Title(message).
Expand Down
22 changes: 22 additions & 0 deletions internal/form/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package form

import "errors"

func ValidationMinValues[T comparable](min int) func([]T) error {
return func(t []T) error {
if len(t) < min {
return errors.New("You must select at least one")
}
return nil
}
}

func ValidationRequired[T comparable]() func(T) error {
return func(t T) error {
var empty T
if t == empty {
return errors.New("This field is required")
}
return nil
}
}
3 changes: 2 additions & 1 deletion internal/source/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type chaptersRow struct {
}

const CHAPTERS_COLUMN = "columns.chapter"
const QUERY_DAYS = 30

func (s *databaseSource) getReadOnlyDbString(file string) string {
if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) {
Expand Down Expand Up @@ -77,7 +78,7 @@ func (s *databaseSource) getRecentReads(db *sqlx.DB) ([]Book, error) {
query = fmt.Sprintf(RECENT_READS_QUERY, s.chaptersColumn)
}

daysToQuery := "-7 day"
daysToQuery := fmt.Sprintf("-%d day", QUERY_DAYS)

err := db.Select(&books, query, daysToQuery)
if err != nil {
Expand Down
12 changes: 9 additions & 3 deletions internal/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,17 @@ func (a *syncAction) Sync() ([]SyncResult, error) {

func (a *syncAction) processTarget(t target.SyncTarget, reads []source.BookContext, wg *sync.WaitGroup) {
defer wg.Done()
user := t.GetCurrentUser()
log.Debug("current user for", "target", t.GetName(), "user", user)
// user := t.GetCurrentUser()
// log.Debug("current user for", "target", t.GetName(), "user", user)

for _, book := range reads {
log.Info("Processing", "book", book.Current.BookName, "target", t.GetName())
name := book.Current.BookName
log := log.With("target", t.GetName(), "book", name)
if !t.ShouldProcess(book) {
log.Debug("Skipping processing of ineligible book")
continue
}
log.Info("Processing")
err := t.UpdateReadStatus(book)
if err != nil {
log.Error("failed to update reading status", "error", err)
Expand Down
Loading

0 comments on commit bf3c1aa

Please sign in to comment.