Skip to content

Commit

Permalink
Merge pull request #31 from Fesaa/mangadex/more-comicinfo
Browse files Browse the repository at this point in the history
Mangadex/more comicinfo
  • Loading branch information
Fesaa authored Jul 5, 2024
2 parents 9042cfe + 0318f6c commit e18067f
Show file tree
Hide file tree
Showing 31 changed files with 225 additions and 390 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
6 changes: 3 additions & 3 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const (
TokenCookieName = "token"
)

var authProvider AuthProvider
var authProvider Provider

func Init() {
authProvider = newAuth()
}

func I() AuthProvider {
func I() Provider {
return authProvider
}

Expand All @@ -28,7 +28,7 @@ type authImpl struct {
tokens map[string]time.Time
}

func newAuth() AuthProvider {
func newAuth() Provider {
return &authImpl{
tokens: make(map[string]time.Time),
pass: config.OrDefault(config.I().GetPassWord(), "admin"),
Expand Down
2 changes: 1 addition & 1 deletion auth/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package auth

import "github.com/gofiber/fiber/v2"

type AuthProvider interface {
type Provider interface {
// IsAuthenticated checks the current request for authentication. This should be handled by the middleware
IsAuthenticated(ctx *fiber.Ctx) (bool, error)

Expand Down
6 changes: 4 additions & 2 deletions comicinfo/comicinfo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
Package comicinfo
MIT License
# Copyright (c) 2023 Felipe Martin
Expand All @@ -21,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
https://github.com/fmartingr/go-comicinfo/blob/latest/schema.go, but with Tags support
https://github.com/fmartingr/go-comicinfo/blob/latest/schema.go, but with Tags support, and custom Kavitatags
*/
package comicinfo

Expand All @@ -31,6 +32,7 @@ var xmlHeader = []byte(`<?xml version="1.0" encoding="UTF-8"?>`)
type ComicInfo struct {
Title string `xml:"Title,omitempty"`
Series string `xml:"Series,omitempty"`
LocalizedSeries string `xml:"LocalizedSeries,omitempty"` // Kavita only
Number string `xml:"Number,omitempty"`
Count int `xml:"Count,omitempty"`
Volume int `xml:"Volume,omitempty"`
Expand Down Expand Up @@ -82,7 +84,7 @@ func (ci *ComicInfo) SetXMLAttributes() {
ci.XmlNsXsi = "http://www.w3.org/2001/XMLSchema-instance"
}

// New provides a new ComicInfo struct with the XML attributes set
// NewComicInfo provides a new ComicInfo struct with the XML attributes set
func NewComicInfo() *ComicInfo {
ci := ComicInfo{}
ci.SetXMLAttributes()
Expand Down
6 changes: 4 additions & 2 deletions comicinfo/io.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*
MIT License
Package comicinfo
# MIT License
# Copyright (c) 2023 Felipe Martin
Expand Down Expand Up @@ -58,7 +60,7 @@ func Write(ci *ComicInfo, w io.Writer) error {
return nil
}

// Read reads the ComicInfo spec from the specified path.
// Open reads the ComicInfo spec from the specified path.
func Open(path string) (*ComicInfo, error) {
f, err := os.ReadFile(path)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module github.com/Fesaa/Media-Provider

go 1.22

toolchain go1.22.2
go 1.22.2

require (
github.com/Fesaa/go-tools v0.0.3
github.com/PuerkitoBio/goquery v1.9.2
github.com/anacrolix/torrent v1.56.1
github.com/gofiber/fiber/v2 v2.52.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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.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
17 changes: 11 additions & 6 deletions limetorrents/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ package limetorrents
import (
"fmt"
"github.com/Fesaa/Media-Provider/log"
tools "github.com/Fesaa/go-tools"
"io"
"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/"
const SEARCH_URL = 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 All @@ -46,7 +47,7 @@ func parseResults(torrents *goquery.Selection) []SearchResult {
return results[1:]
}

func searchFromNode(i int, s *goquery.Selection) SearchResult {
func searchFromNode(_ int, s *goquery.Selection) SearchResult {
name := s.Find("td:nth-child(1) a").Text()
urlSel := s.Find("td:nth-child(1) a")
torrentUrl, _ := urlSel.First().Attr("href")
Expand Down Expand Up @@ -89,7 +90,11 @@ func getSearch(url string) (*goquery.Document, error) {
return nil, err
}

defer res.Body.Close()
defer func(Body io.ReadCloser) {
if err = Body.Close(); err != nil {
log.Warn("failed to close body", "error", err)
}
}(res.Body)
if res.StatusCode != 200 {
return nil, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status)
}
Expand Down
2 changes: 1 addition & 1 deletion mangadex/Readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Media-Provider supports downloading manga's from mangadex, currently it will always download the en version, and does no checking if a full volumes can be made. Planning to change this in the future.

You may add as many manga's as you want, but only one will be downloaded at a time, the rest is added in a queue. They will automatically start downloading after. Once a manga's has fully downloaded, Media-Provider wil zip them per volume, into `.cbz` files with the following layout `Title Vol. ? Ch. ?.cbz`.
You may add as many manga's as you want, but only one will be downloaded at a time, the rest is added in a queue. They will automatically start downloading after. Once a manga's has fully downloaded, Media-Provider wil zip them per volume, into `.cbz` files with the following layout `Title Vol. ?.cbz`.


## Config
Expand Down
44 changes: 22 additions & 22 deletions mangadex/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,53 @@ 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"
"sync"
)

var m MangadexClient
var m Client

func I() MangadexClient {
func I() Client {
return m
}

func newClient(c MangadexConfig) MangadexClient {
return &mangadexClientImpl{
func newClient(c Config) Client {
return &client{
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{},
}
}

type mangadexClientImpl struct {
type client 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
}

func (m *mangadexClientImpl) GetBaseDir() string {
func (m *client) GetBaseDir() string {
return m.dir
}

func (m *mangadexClientImpl) GetCurrentManga() Manga {
func (m *client) GetCurrentManga() Manga {
return m.downloading
}

func (m *mangadexClientImpl) GetQueuedMangas() []payload.QueueStat {
func (m *client) GetQueuedMangas() []payload.QueueStat {
return m.queue.Items()
}

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

Expand All @@ -64,13 +64,13 @@ 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
}

func (m *mangadexClientImpl) RemoveDownload(req payload.StopRequest) error {
func (m *client) RemoveDownload(req payload.StopRequest) error {
manga, ok := m.mangas.Get(req.Id)
if !ok {
ok = m.queue.RemoveFunc(func(item payload.QueueStat) bool {
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 All @@ -101,7 +101,7 @@ func (m *mangadexClientImpl) RemoveDownload(req payload.StopRequest) error {
return nil
}

func (m *mangadexClientImpl) startNext() {
func (m *client) startNext() {
if m.queue.IsEmpty() {
return
}
Expand All @@ -118,7 +118,7 @@ func (m *mangadexClientImpl) startNext() {
}
}

func (m *mangadexClientImpl) deleteFiles(manga Manga) {
func (m *client) deleteFiles(manga Manga) {
dir := path.Join(m.dir, manga.GetDownloadDir())
skip := manga.GetPrevVolumes()
if len(skip) == 0 {
Expand Down Expand Up @@ -148,7 +148,7 @@ func (m *mangadexClientImpl) deleteFiles(manga Manga) {
}
}

func (m *mangadexClientImpl) cleanup(manga Manga) {
func (m *client) cleanup(manga Manga) {
dir := path.Join(m.dir, manga.GetBaseDir(), manga.Title())
entries, err := os.ReadDir(dir)
if err != nil {
Expand All @@ -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
Loading

0 comments on commit e18067f

Please sign in to comment.