modify sorting; improve runtime a bit; only care for online title if new manga, else use existing one from manga.yaml

This commit is contained in:
m3philis
2025-04-29 00:15:57 +02:00
parent 37edab5197
commit 693b070a61
5 changed files with 46 additions and 28 deletions

View File

@@ -1,4 +1,9 @@
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c=
golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

31
main.go
View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"sort"
"strings"
"mangacrawler/createepub"
@@ -21,6 +22,7 @@ func main() {
var forceEpub bool
var mangas []mangacrawler.MangaYaml
var skipDl bool
var completedMangas []string
homepath, err := os.UserHomeDir()
if err != nil {
@@ -54,17 +56,28 @@ func main() {
}
}
sort.Sort(mangacrawler.ByName(mangas))
for i, manga := range mangas {
manga.Name, manga.Completed = mangacrawler.GetMangaInfo(manga)
if manga.Completed {
completedMangas = append(completedMangas, manga.Name)
continue
}
var newChapter bool
if manga.Name == "" {
manga.Name, manga.Completed = mangacrawler.GetMangaInfo(manga)
}
mangaPath := strings.Join([]string{path, "MangaDex", manga.Name}, "/")
os.MkdirAll(mangaPath, 0755)
if (!manga.Completed && !skipDl) || forceDl {
if _, err := os.Stat(strings.Join([]string{homepath, "mangas/MangaDex", manga.Name}, "/")); err == nil {
fmt.Printf("%s found on system!\n", manga.Name)
} else {
os.MkdirAll(mangaPath, 0755)
}
if !skipDl || forceDl {
manga, newChapter = mangacrawler.GetManga(manga, mangaPath, forceDl)
} else if manga.Completed {
fmt.Print(" Manga already completed!\n\n")
}
if forceEpub || newChapter {
@@ -73,7 +86,7 @@ func main() {
createepub.CreateEpub(mangaPath, manga.Name, manga.ID)
fmt.Printf("EPUB created and saved under: %s\n\n", "/tmp/epub")
} else {
fmt.Print("No update on manga, skipping epub creation!\n\n")
fmt.Printf("%s has no updates!\n\n", manga.Name)
}
mangas[i] = manga
@@ -87,6 +100,12 @@ func main() {
yamlPrint, _ := yaml.Marshal(&mangas)
fmt.Println(string(yamlPrint))
}
fmt.Println("Finished mangas on system:")
sort.Strings(completedMangas)
for _, manga := range completedMangas {
fmt.Printf(" %s\n", manga)
}
}
func parseFile(file string) []mangacrawler.MangaYaml {

View File

@@ -2,7 +2,9 @@ package mangacrawler
import (
"encoding/json"
"slices"
"strconv"
"strings"
)
type Chapters struct {
@@ -13,7 +15,6 @@ type Chapters struct {
type ChaptersData struct {
Id string `json:"id"`
Attributes ChaptersAttributes `json:"attributes"`
Rels []ChaptersRels `json:"relationships"`
}
type ChaptersAttributes struct {
@@ -23,17 +24,10 @@ type ChaptersAttributes struct {
Language string `json:"translatedLanguage"`
}
type ChaptersRels struct {
RelsAttr RelsAttributes `json:"attributes"`
}
type RelsAttributes struct {
Name string `json:"name"`
}
func getChapterInfo(mangaId string) []ChaptersData {
var tempChapters Chapters
var chapters Chapters
var found_chapters []string
url := "https://api.mangadex.org/manga/" + mangaId + "/feed"
@@ -65,9 +59,13 @@ func getChapterInfo(mangaId string) []ChaptersData {
for _, chapter := range tempChapters.Data {
if chapter.Attributes.Language == "en" {
if !slices.Contains(found_chapters, chapter.Attributes.Chapter) {
found_chapters = append(found_chapters, chapter.Attributes.Chapter)
chapter.Attributes.Title = strings.ReplaceAll(chapter.Attributes.Title, "/", "|")
chapters.Data = append(chapters.Data, chapter)
}
}
}
return chapters.Data

View File

@@ -17,6 +17,12 @@ type MangaYaml struct {
Completed bool
}
type ByName []MangaYaml
func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByName) Less(i, j int) bool { return a[i].Name < a[j].Name }
func GetManga(manga MangaYaml, filepath string, forceDl bool) (MangaYaml, bool) {
chaptersData := getChapterInfo(manga.ID)
newChapter := false
@@ -49,9 +55,6 @@ func GetManga(manga MangaYaml, filepath string, forceDl bool) (MangaYaml, bool)
latestChapter = chapterIndex
}
}
if !newChapter {
fmt.Print(" No new chapter released yet!\n\n")
}
manga.Chapter = latestChapter

View File

@@ -33,7 +33,6 @@ func GetMangaInfo(mangaYaml MangaYaml) (string, bool) {
var manga Manga
var mangaTitles []string
status := false
homepath, _ := os.UserHomeDir()
url := "https://api.mangadex.org/manga/" + mangaYaml.ID
data := GetJson(url)
@@ -60,12 +59,6 @@ func GetMangaInfo(mangaYaml MangaYaml) (string, bool) {
mangaTitles = append(mangaTitles, title.JP)
}
}
for _, title := range mangaTitles {
if _, err := os.Stat(strings.Join([]string{homepath, "mangas/MangaDex", title}, "/")); err == nil && title != "" {
fmt.Printf("Title found on system! Using: %s\n", title)
return title, status
}
}
for i, title := range mangaTitles {
fmt.Printf("(%d): %s\n", i, title)