Skip to content

Commit 3bcbac4

Browse files
authored
Merge pull request #17 from lethal-bacon0/dev2
Dev2
2 parents e9649f7 + 5717e18 commit 3bcbac4

17 files changed

+182
-262
lines changed

pkg/terminal/scrape.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/lethal-bacon0/WebnovelYoinker/pkg/yoinker"
77
"github.com/lethal-bacon0/WebnovelYoinker/pkg/yoinker/book"
8-
"github.com/lethal-bacon0/WebnovelYoinker/pkg/yoinker/events"
98
"github.com/lethal-bacon0/WebnovelYoinker/pkg/yoinker/export"
109
"github.com/lethal-bacon0/WebnovelYoinker/pkg/yoinker/scrape"
1110
"github.com/schollz/progressbar/v3"
@@ -35,7 +34,6 @@ var scrapeFlags = []cli.Flag{
3534
}
3635

3736
func scrapeCommand(c *cli.Context) error {
38-
events.OnErrorEvent.Add(logErr)
3937
fmt.Println("Starting conversion.")
4038
fmt.Println("Status:")
4139
jobs := getBookConfigs(c.String("in"))

pkg/terminal/yoinkerTerminal.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package terminal
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"log"
76
"os"
87

9-
"github.com/lethal-bacon0/WebnovelYoinker/pkg/yoinker"
108
"github.com/lethal-bacon0/WebnovelYoinker/pkg/yoinker/book"
119
"github.com/urfave/cli/v2"
1210
"gopkg.in/yaml.v2"
@@ -38,12 +36,6 @@ func StartTerminal() {
3836
}
3937
}
4038

41-
func logErr(y *yoinker.CtxYoink) {
42-
if y.Error != nil {
43-
fmt.Println(y.Error.Error())
44-
}
45-
}
46-
4739
func getBookConfigs(path string) []book.Metadata {
4840
books := []book.Metadata{}
4941
rawBooks, err := ioutil.ReadFile(path)

pkg/yoinker/ctxYoink.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

pkg/yoinker/event.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

pkg/yoinker/events/invoke.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

pkg/yoinker/events/onErrorEvent.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

pkg/yoinker/events/onExportFinished.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

pkg/yoinker/events/onExportStart.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

pkg/yoinker/events/onVolumeScrapedEvent.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

pkg/yoinker/export/epubExport.go

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,100 @@ import (
44
"fmt"
55
"html"
66
"log"
7+
"os"
78
"path/filepath"
89
"strings"
910
"sync"
1011

1112
"github.com/bmaupin/go-epub"
1213
"github.com/lethal-bacon0/WebnovelYoinker/pkg/yoinker"
1314
"github.com/lethal-bacon0/WebnovelYoinker/pkg/yoinker/book"
14-
"github.com/lethal-bacon0/WebnovelYoinker/pkg/yoinker/events"
1515
)
1616

17+
var extensionMediaTypes = map[string]string{
18+
".css": "text/css",
19+
".gif": "image/gif",
20+
".jpeg": "image/jpeg",
21+
".jpg": "image/jpeg",
22+
".otf": "application/vnd.ms-opentype",
23+
".png": "image/png",
24+
".svg": "image/svg+xml",
25+
".ttf": "application/font-sfnt",
26+
".woff": "application/font-woff",
27+
".woff2": "font/woff2",
28+
}
29+
1730
//epubExporter exports a volume a epub
1831
type epubExporter struct {
1932
epubExport *epub.Epub
33+
fileDump *os.File
2034
}
2135

2236
//Export exports a valume as epub
23-
func (e *epubExporter) Export(metadata book.Metadata, path string, chapterChannel <-chan book.Chapter) string {
24-
go func() {
25-
events.OnExportStartEvent.Invoke(&yoinker.CtxYoink{
26-
Volume: book.Volume{
27-
Metadata: metadata,
28-
},
29-
})
30-
}()
37+
func (e *epubExporter) Export(metadata book.Metadata, path string, chapters []book.Chapter) string {
38+
var (
39+
coverImage string
40+
waiter sync.WaitGroup
41+
exportPath = filepath.Join(path, metadata.Title+".epub")
42+
err error
43+
cssPath string
44+
)
45+
// e.fileDump, err = os.Create(filepath.Join(path, fmt.Sprintf("volume_dump_%v_.txt", metadata.Title)))
46+
// if err != nil {
47+
// log.Println(err)
48+
// }
49+
// defer e.fileDump.Close()
50+
3151
e.epubExport = epub.NewEpub(metadata.Title)
32-
cssPath, err := e.epubExport.AddCSS("https://raw.githubusercontent.com/lethal-bacon0/WebnovelYoinker/master/assets/ebookstyle.css", "stylesheet.css")
52+
cssPath, err = e.epubExport.AddCSS("https://raw.githubusercontent.com/lethal-bacon0/WebnovelYoinker/master/assets/ebookstyle.css", "stylesheet.css")
3353
if err != nil {
34-
// invokeError(err)
54+
log.Println(err)
3555
}
36-
var coverImage string
37-
var waiter sync.WaitGroup
56+
waiter.Add(1)
3857
go func() {
39-
waiter.Add(1)
40-
var err error
4158
coverImage, err = e.epubExport.AddImage(metadata.Cover, "")
4259
if err != nil {
43-
// invokeError(err)
60+
coverImage = ""
61+
} else {
62+
e.epubExport.SetCover(coverImage, "")
4463
}
64+
e.epubExport.SetAuthor(metadata.Author)
65+
e.epubExport.SetLang(metadata.Language)
4566
waiter.Done()
4667
}()
47-
48-
i := 0
49-
for chapter := range chapterChannel {
50-
i++
68+
for i, chapter := range chapters {
5169
if chapter.ChapterName == "" {
52-
chapter.ChapterName = fmt.Sprintf("Chapter %v", i)
70+
chapter.ChapterName = fmt.Sprintf("Chapter %v", i+1)
5371
}
5472
e.epubExport.AddSection(e.addChapter(chapter), chapter.ChapterName, "", cssPath)
5573
}
56-
5774
waiter.Wait()
58-
e.epubExport.SetCover(coverImage, "")
59-
e.epubExport.SetAuthor(metadata.Author)
60-
e.epubExport.SetLang(metadata.Language)
61-
exportPath := filepath.Join(path, metadata.Title+".epub")
75+
defer func() {
76+
if r := recover(); r != nil {
77+
log.Printf("Panic while exporting %v: %v\n", metadata.Title, r)
78+
}
79+
}()
80+
err = e.epubExport.Write(exportPath)
6281
if err != nil {
63-
log.Fatal(err)
82+
fmt.Println(err)
6483
}
65-
err = e.epubExport.Write(exportPath)
66-
events.OnExportFinishedEvent.Invoke(&yoinker.CtxYoink{
67-
Volume: book.Volume{
68-
Metadata: metadata,
69-
},
70-
})
84+
7185
return exportPath
7286
}
7387

7488
func (e *epubExporter) addChapter(chapter book.Chapter) string {
75-
var parsedContent strings.Builder
89+
var (
90+
parsedContent strings.Builder
91+
)
92+
7693
parsedContent.WriteString(fmt.Sprintf("<p><strong> %v </strong></p>", chapter.ChapterName))
7794
for _, page := range chapter.Content {
7895
switch page.(type) {
7996
case *book.PageImage:
8097
pageImage := page.(*book.PageImage)
8198
imagePath, err := e.epubExport.AddImage(pageImage.Image, "")
8299
if err != nil {
83-
// e.makeCallback(err.Error())
100+
continue
84101
}
85102
content := fmt.Sprintf("<div class=\"width\">"+
86103
"<div class=\"pc\">"+
@@ -94,6 +111,7 @@ func (e *epubExporter) addChapter(chapter book.Chapter) string {
94111

95112
case *book.Paragraph:
96113
par := page.(*book.Paragraph)
114+
// e.fileDump.WriteString(fmt.Sprintf("%v\n", par.Content))
97115
content := fmt.Sprintf("<p>%v</p>", html.EscapeString(par.Content))
98116
parsedContent.WriteString(content)
99117
}

0 commit comments

Comments
 (0)