forked from metafates/mangal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
178 lines (155 loc) 路 4 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package update
import (
"bytes"
"encoding/json"
"github.com/belphemur/mangal/constant"
"github.com/belphemur/mangal/filesystem"
"github.com/belphemur/mangal/log"
"github.com/belphemur/mangal/packer/cbz"
"github.com/belphemur/mangal/source"
"github.com/belphemur/mangal/util"
"os"
"path/filepath"
"strings"
)
func Metadata(mangaPath string) error {
log.Infof("extracting series name from %s", mangaPath)
name, err := GetName(mangaPath)
if err != nil {
log.Error(err)
return err
}
log.Infof("extracted name: %s", name)
log.Infof("finding %s on anilist", name)
manga := &source.Manga{
Name: name,
}
// will set new metadata from anilist
err = manga.PopulateMetadata(func(string) {})
if err != nil {
log.Error()
return err
}
chapters, err := getChapters(mangaPath)
if err != nil {
log.Error(err)
return err
}
manga.Chapters = make([]*source.Chapter, 0)
chaptersPaths := make(map[*source.Chapter]string)
for i, chapter := range chapters {
// since we are trying to update ComicInfo.xml here, we do not care about any other formats other than FormatCBZ
if chapter.format != constant.FormatCBZ {
continue
}
log.Infof("getting ComicInfoXML from %s", chapter.path)
comicInfo, err := getComicInfoXML(chapter.path)
if err != nil {
log.Error(err)
continue
}
chap := &source.Chapter{
Name: comicInfo.Title,
Manga: manga,
URL: comicInfo.Web,
Index: uint16(i + 1),
}
manga.Chapters = append(manga.Chapters, chap)
chaptersPaths[chap] = chapter.path
}
// okay, we're ready to regenerate series.json and ComicInfo.xml now
seriesJSON := manga.SeriesJSON()
buf, err := json.Marshal(seriesJSON)
if err != nil {
log.Error(err)
return err
}
// update series.json
log.Info("updating series json")
err = filesystem.Api().WriteFile(filepath.Join(mangaPath, "series.json"), buf, os.ModePerm)
if err != nil {
log.Error(err)
return err
}
log.Info("downloading new cover")
// remove old cover(s).
// even though DownloadCover() will overwrite previous one
// there may be a sitation when new cover has a different extension
// which would result having duplicates
files, err := filesystem.Api().ReadDir(mangaPath)
if err == nil {
for _, file := range files {
if util.FileStem(file.Name()) == "cover" {
_ = filesystem.Api().Remove(filepath.Join(mangaPath, file.Name()))
}
}
}
err = manga.DownloadCover(true, mangaPath, func(string) {})
if err != nil {
log.Error(err)
}
log.Infof("updating ComicInfo.xml for %d chapters", len(manga.Chapters))
for _, chapter := range manga.Chapters {
path := chaptersPaths[chapter]
file, err := filesystem.Api().Open(path)
if err != nil {
log.Error(err)
continue
}
stat, err := file.Stat()
if err != nil {
_ = file.Close()
continue
}
// go to memmap fs to unzip
filesystem.SetMemMapFs()
err = util.Unzip(file, stat.Size(), chapter.Name)
if err != nil {
log.Error(err)
_ = file.Close()
continue
}
// add pages before converting back to cbz
files, err := filesystem.Api().ReadDir(chapter.Name)
if err != nil {
log.Error(err)
_ = file.Close()
continue
}
for _, file := range files {
// skip ComicInfo.xml
if strings.HasSuffix(file.Name(), ".xml") {
continue
}
image, err := filesystem.Api().ReadFile(filepath.Join(chapter.Name, file.Name()))
// we can not let some pages be gone
// so if we can't open any - whole process should stop
if err != nil {
log.Error(err)
return err
}
chapter.Pages = append(chapter.Pages, &source.Page{
Chapter: chapter,
Size: uint64(file.Size()),
Index: uint16(len(chapter.Pages)),
Extension: filepath.Ext(file.Name()),
Contents: bytes.NewBuffer(image),
})
}
_ = file.Close()
filesystem.SetOsFs()
log.Debugf("removing old %s", path)
err = filesystem.Api().Remove(path)
if err != nil {
log.Error(err)
continue
}
log.Debugf("saving to %s", path)
err = cbz.SaveTo(chapter, path)
if err != nil {
log.Error(err)
return err
}
}
return nil
}