Skip to content

Commit

Permalink
Remove utils, use go-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Fesaa committed Jul 5, 2024
1 parent 585fcdd commit bb41c87
Show file tree
Hide file tree
Showing 21 changed files with 88 additions and 315 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ COPY ./mangadex ./mangadex
COPY ./payload ./payload
COPY ./providers ./providers
COPY ./subsplease ./subsplease
COPY ./utils ./utils
COPY ./yoitsu ./yoitsu
COPY ./yts ./yts
COPY ./*.go ./
Expand Down
3 changes: 2 additions & 1 deletion api/routes/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ func Stats(ctx *fiber.Ctx) error {
Running: []payload.InfoStat{},
Queued: []payload.QueueStat{},
}
yoitsu.I().GetRunningTorrents().ForEachSafe(func(key string, torrent yoitsu.Torrent) {
yoitsu.I().GetRunningTorrents().ForEach(func(key string, torrent yoitsu.Torrent) bool {
statsResponse.Running = append(statsResponse.Running, torrent.GetInfo())
return true
})
manga := mangadex.I().GetCurrentManga()
if manga != nil {
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/Fesaa/Media-Provider

go 1.22

toolchain go1.22.2
go 1.22.2

require (
github.com/PuerkitoBio/goquery v1.9.2
Expand All @@ -14,6 +12,7 @@ require (
)

require (
github.com/Fesaa/go-tools v0.0.3 // indirect
github.com/RoaringBitmap/roaring v1.9.4 // indirect
github.com/ajwerner/btree v0.0.0-20211221152037-f427b3e689c0 // indirect
github.com/alecthomas/atomic v0.1.0-alpha2 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ crawshaw.io/sqlite v0.3.2/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4=
filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU=
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Fesaa/go-tools v0.0.1 h1:YnOM/nkD0R/d+ryIMqWJW4OUMbfDCddokX0QSsIHdCI=
github.com/Fesaa/go-tools v0.0.1/go.mod h1:cqt4m4QkWtNlVN+NGvWjNKai6/hHJYbKPpS6VnrORiA=
github.com/Fesaa/go-tools v0.0.2 h1:z1XUKbb7GGMOppUEy8CC1tH8x1TPD+QN43wpRSYeNwI=
github.com/Fesaa/go-tools v0.0.2/go.mod h1:cqt4m4QkWtNlVN+NGvWjNKai6/hHJYbKPpS6VnrORiA=
github.com/Fesaa/go-tools v0.0.3 h1:eq5H03eowQV4SYGxIVWa1HUAnmAQ/yl39l4p/wTk9Gw=
github.com/Fesaa/go-tools v0.0.3/go.mod h1:cqt4m4QkWtNlVN+NGvWjNKai6/hHJYbKPpS6VnrORiA=
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
Expand Down
6 changes: 3 additions & 3 deletions limetorrents/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ package limetorrents
import (
"fmt"
"github.com/Fesaa/Media-Provider/log"
tools "github.com/Fesaa/go-tools"
"net/http"
"net/url"
"strings"
"time"

"github.com/Fesaa/Media-Provider/utils"
"github.com/PuerkitoBio/goquery"
)

const BASE_URl string = "https://www.limetorrents.lol"
const SEARCH_URL string = BASE_URl + "/search/%s/%s/%d/"

var cache utils.Cache[[]SearchResult] = *utils.NewCache[[]SearchResult](5 * time.Minute)
var cache = tools.NewCache[[]SearchResult](5 * time.Minute)

func Search(searchOptions SearchOptions) ([]SearchResult, error) {
searchUrl := formatUrl(searchOptions)
log.Debug("searching lime for torrents", "url", searchUrl)
if res := cache.Get(searchUrl); res != nil {
log.Trace("Limetorrents Cache hit", "url", searchUrl)
return *res, nil
return res.Get(), nil
}

doc, err := getSearch(searchUrl)
Expand Down
18 changes: 9 additions & 9 deletions mangadex/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/Fesaa/Media-Provider/config"
"github.com/Fesaa/Media-Provider/log"
"github.com/Fesaa/Media-Provider/payload"
"github.com/Fesaa/Media-Provider/utils"
tools "github.com/Fesaa/go-tools"
"os"
"path"
"slices"
Expand All @@ -22,8 +22,8 @@ func newClient(c MangadexConfig) MangadexClient {
return &mangadexClientImpl{
dir: config.OrDefault(c.GetRootDir(), "temp"),
maxImages: c.GetMaxConcurrentMangadexImages(),
mangas: utils.NewSafeMap[string, Manga](),
queue: utils.NewQueue[payload.QueueStat](),
mangas: tools.NewSafeMap[string, Manga](),
queue: tools.NewQueue[payload.QueueStat](),
downloading: nil,
mu: sync.Mutex{},
}
Expand All @@ -32,8 +32,8 @@ func newClient(c MangadexConfig) MangadexClient {
type mangadexClientImpl struct {
dir string
maxImages int
mangas *utils.SafeMap[string, Manga]
queue utils.Queue[payload.QueueStat]
mangas tools.SafeMap[string, Manga]
queue tools.Queue[payload.QueueStat]
downloading Manga
mu sync.Mutex
}
Expand All @@ -51,7 +51,7 @@ func (m *mangadexClientImpl) GetQueuedMangas() []payload.QueueStat {
}

func (m *mangadexClientImpl) Download(req payload.DownloadRequest) (Manga, error) {
if m.mangas.Has(req.Id) {
if m.mangas.Contains(req.Id) {
return nil, fmt.Errorf("manga already exists: %s", req.Id)
}

Expand All @@ -64,7 +64,7 @@ func (m *mangadexClientImpl) Download(req payload.DownloadRequest) (Manga, error

log.Info("downloading manga", "mangaId", req.Id, "into", req.BaseDir, "title?", req.TempTitle)
manga := newManga(req, m.maxImages, m)
m.mangas.Set(req.Id, manga)
m.mangas.Put(req.Id, manga)
m.downloading = manga
manga.WaitForInfoAndDownload()
return manga, nil
Expand All @@ -85,7 +85,7 @@ func (m *mangadexClientImpl) RemoveDownload(req payload.StopRequest) error {

log.Info("Dropping manga", "mangaId", req.Id, "title", manga.Title(), "deleteFiles", req.DeleteFiles)
go func() {
m.mangas.Delete(req.Id)
m.mangas.Remove(req.Id)
manga.Cancel()
m.mu.Lock()
m.downloading = nil
Expand Down Expand Up @@ -159,7 +159,7 @@ func (m *mangadexClientImpl) cleanup(manga Manga) {
if !entry.IsDir() {
continue
}
err = utils.ZipFolder(path.Join(dir, entry.Name()), path.Join(dir, entry.Name()+".cbz"))
err = zipFolder(path.Join(dir, entry.Name()), path.Join(dir, entry.Name()+".cbz"))
if err != nil {
log.Error("error while zipping directory", "dir", dir, "mangaId", manga.Id(), "err", err)
continue
Expand Down
18 changes: 12 additions & 6 deletions mangadex/manga.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/Fesaa/Media-Provider/config"
"github.com/Fesaa/Media-Provider/log"
"github.com/Fesaa/Media-Provider/payload"
"github.com/Fesaa/Media-Provider/utils"
tools "github.com/Fesaa/go-tools"
"io"
"log/slog"
"net/http"
Expand Down Expand Up @@ -37,7 +37,7 @@ type mangaImpl struct {

info *MangaSearchData
chapters ChapterSearchResponse
covers *utils.SafeMap[string, string]
covers tools.SafeMap[string, string]

volumeMetadata []string
alreadyDownloadedVolumes []string
Expand Down Expand Up @@ -120,7 +120,7 @@ func (m *mangaImpl) GetInfo() payload.InfoStat {
}(),
Size: strconv.Itoa(len(m.chapters.Data)) + " Chapters",
Downloading: m.wg != nil,
Progress: utils.Percent(int64(m.chaptersDownloaded), int64(len(m.chapters.Data))),
Progress: Percent(int64(m.chaptersDownloaded), int64(len(m.chapters.Data))),
SpeedType: payload.IMAGES,
Speed: payload.SpeedData{T: time.Now().Unix(), Speed: speed},
DownloadDir: m.GetDownloadDir(),
Expand Down Expand Up @@ -182,9 +182,9 @@ func (m *mangaImpl) loadInfo() chan struct{} {
covers, err := GetCoverImages(m.id)
if err != nil || covers == nil {
m.log.Warn("error while loading manga covers, ignoring", "err", err)
m.covers = &utils.SafeMap[string, string]{}
m.covers = tools.NewSafeMap[string, string]()
} else {
m.covers = utils.NewSafeMap(covers.GetUrlsPerVolume(m.id))
m.covers = tools.NewSafeMap(covers.GetUrlsPerVolume(m.id))
}

close(out)
Expand Down Expand Up @@ -403,7 +403,7 @@ func (m *mangaImpl) comicInfo(chapter ChapterSearchData) *comicinfo.ComicInfo {
m.log.Trace("unable to parse volume number", "volume", chapter.Attributes.Volume, "err", err)
}

ci.Tags = strings.Join(utils.MaybeMap(m.info.Attributes.Tags, func(t TagData) (string, bool) {
ci.Tags = strings.Join(tools.TransformMaybeArray(m.info.Attributes.Tags, func(t TagData) (string, bool) {
n, ok := t.Attributes.Name["en"]
if !ok {
return "", false
Expand Down Expand Up @@ -468,3 +468,9 @@ func downloadAndWrite(url string, path string) error {

return nil
}

func Percent(a, b int64) int64 {
b = max(b, 1)
ratio := (float64)(a) / (float64)(b)
return (int64)(ratio * 100)
}
9 changes: 5 additions & 4 deletions mangadex/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"encoding/json"
"fmt"
"github.com/Fesaa/Media-Provider/log"
"github.com/Fesaa/Media-Provider/utils"
tools "github.com/Fesaa/go-tools"
"io"
"net/http"
"time"
)

var tags = utils.NewSafeMap[string, string]()
var tags = tools.NewSafeMap[string, string]()

var cache = utils.NewCache[MangaSearchResponse](5 * time.Minute)
var cache = tools.NewCache[*MangaSearchResponse](5 * time.Minute)

func mapTags(in []string, skip bool) ([]string, error) {
mappedTags := make([]string, 0)
Expand Down Expand Up @@ -48,14 +48,15 @@ func SearchManga(options SearchOptions) (*MangaSearchResponse, error) {
log.Trace("searching Mangadex for Manga", "options", fmt.Sprintf("%#v", options), "url", url)
if hit := cache.Get(url); hit != nil {
log.Trace("Mangadex Cache hit", "url", url)
return hit, nil
return hit.Get(), nil
}

var searchResponse MangaSearchResponse
err = do(url, &searchResponse)
if err != nil {
return nil, err
}
cache.Set(url, &searchResponse)
return &searchResponse, nil
}

Expand Down
2 changes: 1 addition & 1 deletion mangadex/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func loadTags() error {
if !ok {
continue
}
tags.Set(enName, tag.Id)
tags.Put(enName, tag.Id)
}
return nil
}
4 changes: 2 additions & 2 deletions utils/zip.go → mangadex/zip.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package mangadex

import (
"archive/zip"
Expand Down Expand Up @@ -40,7 +40,7 @@ func addFileToZip(zipWriter *zip.Writer, filename string, baseDir string) error
return err
}

func ZipFolder(folderPath string, zipFileName string) error {
func zipFolder(folderPath string, zipFileName string) error {
log.Trace("zipping folder", "path", folderPath, "filename", zipFileName)
zipFile, err := os.Create(zipFileName)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions providers/nyaaWrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package providers
import (
"fmt"
"github.com/Fesaa/Media-Provider/log"
"github.com/Fesaa/Media-Provider/utils"
tools "github.com/Fesaa/go-tools"
"github.com/irevenko/go-nyaa/nyaa"
"github.com/irevenko/go-nyaa/types"
"time"
)

var cache = *utils.NewCache[[]types.Torrent](5 * time.Minute)
var cache = tools.NewCache[[]types.Torrent](5 * time.Minute)

func cacheKey(opts nyaa.SearchOptions) string {
return fmt.Sprintf("%s_%s_%s_%s_%s", opts.Provider, opts.Filter, opts.SortBy, opts.Category, opts.Query)
Expand All @@ -21,7 +21,7 @@ func nyaaSearch(opts nyaa.SearchOptions) ([]types.Torrent, error) {

if hit := cache.Get(key); hit != nil {
log.Trace("Nyaa Cache hit", "key", key)
return *hit, nil
return hit.Get(), nil
}

search, err := nyaa.Search(opts)
Expand Down
6 changes: 3 additions & 3 deletions subsplease/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/Fesaa/Media-Provider/log"
"github.com/Fesaa/Media-Provider/utils"
tools "github.com/Fesaa/go-tools"
"io"
"net/http"
"net/url"
Expand All @@ -13,7 +13,7 @@ import (

const URL string = "https://subsplease.org/api/?f=search&tz=Europe/Brussels&s=%s"

var cache = *utils.NewCache[SearchResult](5 * time.Minute)
var cache = tools.NewCache[SearchResult](5 * time.Minute)

type SearchOptions struct {
Query string
Expand All @@ -29,7 +29,7 @@ func Search(options SearchOptions) (SearchResult, error) {

if res := cache.Get(u); res != nil {
log.Trace("Cache hit", "url", u)
return *res, nil
return res.Get(), nil
}

req, err := http.Get(u)
Expand Down
68 changes: 0 additions & 68 deletions utils/cache.go

This file was deleted.

Loading

0 comments on commit bb41c87

Please sign in to comment.