-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from metafates/dev
v3.8.0
- Loading branch information
Showing
81 changed files
with
1,957 additions
and
824 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
MAKEFLAGS += --silent | ||
|
||
ldflags := -X 'github.com/metafates/mangal/constant.BuiltAt=$(shell date -u)' | ||
ldflags += -X 'github.com/metafates/mangal/constant.BuiltBy=$(shell whoami)@$(shell hostname)' | ||
ldflags += -X 'github.com/metafates/mangal/constant.Revision=$(shell git rev-parse --short HEAD)' | ||
ldflags += -s | ||
ldlags += -w | ||
|
||
build_flags := -ldflags=${ldflags} | ||
|
||
all: help | ||
|
||
help: | ||
@echo "Usage: make [target]" | ||
@echo "" | ||
@echo "Targets:" | ||
@echo " build Build the mangal binary" | ||
@echo " install Install the mangal binary" | ||
@echo " test Run the tests" | ||
@echo " help Show this help message" | ||
@echo "" | ||
|
||
install: | ||
@go install "$(build_flags)" | ||
|
||
|
||
build: | ||
@go build "$(build_flags)" | ||
|
||
test: | ||
@go test ./... | ||
|
||
uninstall: | ||
@rm -f $(shell which mangal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package anilist | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/metafates/mangal/filesystem" | ||
"github.com/metafates/mangal/log" | ||
"github.com/metafates/mangal/where" | ||
"github.com/spf13/afero" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
var cache = anilistCache{ | ||
Mem: make(map[string]*Manga), | ||
} | ||
|
||
type anilistCache struct { | ||
Mem map[string]*Manga `json:"mangas"` | ||
file afero.File | ||
} | ||
|
||
func (a *anilistCache) Init() error { | ||
if a.file != nil { | ||
return nil | ||
} | ||
|
||
log.Debug("Initializing anilist cacher") | ||
|
||
var err error | ||
path := filepath.Join(where.Cache(), "anilist_cache.json") | ||
log.Debugf("Opening anilist cache file at %s", path) | ||
a.file, err = filesystem.Api().OpenFile(path, os.O_RDWR|os.O_CREATE, os.ModePerm) | ||
|
||
if err != nil { | ||
log.Warn(err) | ||
return err | ||
} | ||
|
||
contents, err := io.ReadAll(a.file) | ||
if err != nil { | ||
log.Warn(err) | ||
return err | ||
} | ||
|
||
if len(contents) == 0 { | ||
log.Debug("Anilist cache file is empty, skipping unmarshal") | ||
return nil | ||
} | ||
|
||
var temp anilistCache | ||
err = json.Unmarshal(contents, &temp) | ||
if err != nil { | ||
log.Warn(err) | ||
return err | ||
} | ||
|
||
log.Debugf("Anilist cache file unmarshalled successfully, len is %d", len(temp.Mem)) | ||
a.Mem = temp.Mem | ||
return nil | ||
} | ||
|
||
func (a *anilistCache) Get(name string) (*Manga, bool) { | ||
if a.file == nil { | ||
_ = a.Init() | ||
} | ||
mangas, ok := a.Mem[a.formatName(name)] | ||
return mangas, ok | ||
} | ||
|
||
func (a *anilistCache) Set(name string, manga *Manga) error { | ||
log.Debug("Setting anilist cacher entry") | ||
a.Mem[a.formatName(name)] = manga | ||
marshalled, err := json.Marshal(a) | ||
if err != nil { | ||
log.Warn(err) | ||
return err | ||
} | ||
|
||
_, _ = a.file.Seek(0, 0) | ||
_, err = a.file.Write(marshalled) | ||
if err != nil { | ||
log.Warn(err) | ||
} | ||
return err | ||
} | ||
|
||
func (a *anilistCache) formatName(name string) string { | ||
return strings.TrimSpace(strings.ToLower(name)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package anilist | ||
|
||
import ( | ||
"fmt" | ||
levenshtein "github.com/ka-weihe/fast-levenshtein" | ||
"github.com/metafates/mangal/log" | ||
"github.com/metafates/mangal/util" | ||
"github.com/samber/lo" | ||
"strings" | ||
) | ||
|
||
var ( | ||
retries uint8 | ||
limit uint8 = 3 | ||
) | ||
|
||
func FindClosest(name string) (*Manga, error) { | ||
if retries >= limit { | ||
retries = 0 | ||
err := fmt.Errorf("no results found on Anilist for manga %s", name) | ||
log.Error(err) | ||
return nil, err | ||
} | ||
|
||
if manga, ok := cache.Get(name); ok { | ||
return manga, nil | ||
} | ||
|
||
// search for manga on anilist | ||
urls, err := Search(name) | ||
if err != nil { | ||
log.Error(err) | ||
return nil, err | ||
} | ||
|
||
if len(urls) == 0 { | ||
// try again with a different name | ||
retries++ | ||
words := strings.Split(name, " ") | ||
if len(words) == 1 { | ||
// trigger limit | ||
retries = limit | ||
return FindClosest("") | ||
} | ||
|
||
// one word less | ||
alternateName := strings.Join(words[:util.Max(len(words)-1, 1)], " ") | ||
log.Infof(`No results found on Anilist for manga "%s", trying "%s"`, name, alternateName) | ||
return FindClosest(alternateName) | ||
} | ||
|
||
// find the closest match | ||
closest := lo.MinBy(urls, func(a, b *Manga) bool { | ||
return levenshtein.Distance(name, a.Name()) < levenshtein.Distance(name, b.Name()) | ||
}) | ||
|
||
log.Info("Found closest match: " + closest.Name()) | ||
retries = 0 | ||
_ = cache.Set(name, closest) | ||
return closest, nil | ||
} |
Oops, something went wrong.