Skip to content

Commit

Permalink
(*)support generating pdf for kindle DXG
Browse files Browse the repository at this point in the history
  • Loading branch information
missdeer committed Dec 26, 2017
1 parent 5c526f9 commit 5840d93
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 64 deletions.
11 changes: 5 additions & 6 deletions 168xs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"regexp"
"time"

"github.com/dfordsoft/golib/ebook"
"github.com/dfordsoft/golib/httputil"
"github.com/dfordsoft/golib/ic"
)
Expand Down Expand Up @@ -81,8 +80,8 @@ func init() {

b = ic.Convert("gbk", "utf-8", b)
b = bytes.Replace(b, []byte("</a></dd><dd><a"), []byte("</a></dd>\n<dd><a"), -1)
mobi := &ebook.Mobi{}
mobi.Begin()

gen.Begin()

var title string
r, _ := regexp.Compile(`<dd><a\shref="([0-9]+\.html)">([^<]+)</a></dd>$`)
Expand All @@ -96,7 +95,7 @@ func init() {
if len(ss) > 0 && len(ss[0]) > 0 {
s := ss[0]
title = s[1]
mobi.SetTitle(title)
gen.SetTitle(title)
continue
}
}
Expand All @@ -105,11 +104,11 @@ func init() {
s := ss[0]
finalURL := fmt.Sprintf("%s%s", tocURL, s[1])
c := dlPage(finalURL)
mobi.AppendContent(s[2], finalURL, string(c))
gen.AppendContent(s[2], finalURL, string(c))
fmt.Println(s[2], finalURL, len(c), "bytes")
}
}
mobi.End()
gen.End()
},
})
}
11 changes: 5 additions & 6 deletions aszw.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"regexp"
"time"

"github.com/dfordsoft/golib/ebook"
"github.com/dfordsoft/golib/httputil"
"github.com/dfordsoft/golib/ic"
)
Expand Down Expand Up @@ -67,8 +66,8 @@ func init() {
b = ic.Convert("gbk", "utf-8", b)
b = bytes.Replace(b, []byte("<tr><td class=\"L\">"), []byte("<tr>\n<td class=\"L\">"), -1)
b = bytes.Replace(b, []byte("</td><td class=\"L\">"), []byte("</td>\n<td class=\"L\">"), -1)
mobi := &ebook.Mobi{}
mobi.Begin()

gen.Begin()

var title string
// <td class="L"><a href="43118588.html">1、我会对你负责的</a></td>
Expand All @@ -83,7 +82,7 @@ func init() {
if len(ss) > 0 && len(ss[0]) > 0 {
s := ss[0]
title = s[1]
mobi.SetTitle(title)
gen.SetTitle(title)
continue
}
}
Expand All @@ -92,11 +91,11 @@ func init() {
s := ss[0]
finalURL := fmt.Sprintf("%s%s", tocURL, s[1])
c := dlPage(finalURL)
mobi.AppendContent(s[2], finalURL, string(c))
gen.AppendContent(s[2], finalURL, string(c))
fmt.Println(s[2], finalURL, len(c), "bytes")
}
}
mobi.End()
gen.End()
},
})
}
10 changes: 4 additions & 6 deletions biquge.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/dfordsoft/golib/ebook"
"github.com/dfordsoft/golib/httputil"
"github.com/dfordsoft/golib/ic"
)
Expand Down Expand Up @@ -77,8 +76,7 @@ func init() {
b = ic.Convert("gbk", "utf-8", b)
}

mobi := &ebook.Mobi{}
mobi.Begin()
gen.Begin()

var title string
var lines []string
Expand All @@ -101,7 +99,7 @@ func init() {
if len(ss) > 0 && len(ss[0]) > 0 {
s := ss[0]
title = s[p.bookTitlePos]
mobi.SetTitle(title)
gen.SetTitle(title)
continue
}
}
Expand All @@ -125,10 +123,10 @@ func init() {
finalURL = articleURL
}
c := dlPage(finalURL)
mobi.AppendContent(s[p.articleTitlePos], finalURL, string(c))
gen.AppendContent(s[p.articleTitlePos], finalURL, string(c))
fmt.Println(s[p.articleTitlePos], finalURL, len(c), "bytes")
}
mobi.End()
gen.End()
}

registerNovelSiteHandler(&novelSiteHandler{
Expand Down
11 changes: 5 additions & 6 deletions luoxia.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"time"

"github.com/dfordsoft/golib/ebook"
"github.com/dfordsoft/golib/httputil"
)

Expand Down Expand Up @@ -93,8 +92,8 @@ func init() {
}

b = bytes.Replace(b, []byte("</li><li>"), []byte("</li>\n<li>"), -1)
mobi := &ebook.Mobi{}
mobi.Begin()

gen.Begin()

var title string
// <li><a target="_blank" title="第二章&nbsp;破釜沉舟" href="http://www.luoxia.com/jingzhou/32741.htm">第二章&nbsp;破釜沉舟</a></li>
Expand All @@ -110,7 +109,7 @@ func init() {
if len(ss) > 0 && len(ss[0]) > 0 {
s := ss[0]
title = s[1]
mobi.SetTitle(title)
gen.SetTitle(title)
continue
}
}
Expand All @@ -121,12 +120,12 @@ func init() {
c := dlPage(finalURL)
if len(c) > 0 {
title := strings.Replace(s[2], `&nbsp;`, ` `, -1)
mobi.AppendContent(title, finalURL, string(c))
gen.AppendContent(title, finalURL, string(c))
fmt.Println(title, finalURL, len(c), "bytes")
}
}
}
mobi.End()
gen.End()
},
})
}
31 changes: 21 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/dfordsoft/golib/ebook"
"net/url"
"os"
"regexp"
Expand Down Expand Up @@ -32,6 +33,7 @@ type novelSiteHandler struct {

var (
novelSiteHandlers []*novelSiteHandler
gen ebook.IBook
)

func registerNovelSiteHandler(h *novelSiteHandler) {
Expand Down Expand Up @@ -64,23 +66,32 @@ func main() {
return
}

if os.Args[1] == "list" {
listCommandHandler()
return
var novelURL string
for i := 1; i < len(os.Args); i++ {
switch os.Args[i] {
case "list":
listCommandHandler()
return
case "dxg":
gen = &ebook.Pdf{}
default:
_, e := url.Parse(os.Args[i])
if e == nil {
novelURL = os.Args[i]
}
}
}

_, e := url.Parse(os.Args[1])
if e != nil {
fmt.Println("不支持的输入参数")
listCommandHandler()
return
if gen == nil {
gen = &ebook.Mobi{}
}

for _, h := range novelSiteHandlers {
for _, pattern := range h.MatchPatterns {
r, _ := regexp.Compile(pattern)
if r.MatchString(os.Args[1]) {
h.Download(os.Args[1])
if r.MatchString(novelURL) {
gen.Info()
h.Download(novelURL)
return
}
}
Expand Down
10 changes: 4 additions & 6 deletions piaotian.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"time"

"github.com/dfordsoft/golib/ebook"
"github.com/dfordsoft/golib/httputil"
"github.com/dfordsoft/golib/ic"
)
Expand Down Expand Up @@ -71,8 +70,7 @@ func init() {
return
}

mobi := &ebook.Mobi{}
mobi.Begin()
gen.Begin()

var title string
r, _ = regexp.Compile(`^<li><a\shref="([0-9]+\.html)">([^<]+)</a></li>$`)
Expand All @@ -92,7 +90,7 @@ func init() {
if idx > 0 {
title = title[:idx]
}
mobi.SetTitle(title)
gen.SetTitle(title)
continue
}
}
Expand All @@ -101,11 +99,11 @@ func init() {
s := ss[0]
finalURL := fmt.Sprintf("%s%s", tocURL, s[1])
c := dlPage(finalURL)
mobi.AppendContent(s[2], finalURL, string(c))
gen.AppendContent(s[2], finalURL, string(c))
fmt.Println(s[2], finalURL, len(c), "bytes")
}
}
mobi.End()
gen.End()
},
})
}
10 changes: 4 additions & 6 deletions shumilou.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"time"

"github.com/dfordsoft/golib/ebook"
"github.com/dfordsoft/golib/httputil"
"github.com/dfordsoft/golib/ic"
)
Expand Down Expand Up @@ -65,8 +64,7 @@ func init() {

b = bytes.Replace(b, []byte("</li><li class=\"zl\">"), []byte("</li>\n<li class=\"zl\">"), -1)

mobi := &ebook.Mobi{}
mobi.Begin()
gen.Begin()

var title string
// <li class="zl"><a href="12954102.html">阅读指南(重要,必读)</a></li>
Expand All @@ -88,7 +86,7 @@ func init() {
if idx > 0 {
title = title[:idx]
}
mobi.SetTitle(title)
gen.SetTitle(title)
continue
}
}
Expand All @@ -97,11 +95,11 @@ func init() {
s := ss[0]
finalURL := fmt.Sprintf("%s%s", u, s[1])
c := dlPage(finalURL)
mobi.AppendContent(s[2], finalURL, string(c))
gen.AppendContent(s[2], finalURL, string(c))
fmt.Println(s[2], finalURL, len(c), "bytes")
}
}
mobi.End()
gen.End()
},
})
}
10 changes: 4 additions & 6 deletions uukanshu.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"time"

"github.com/dfordsoft/golib/ebook"
"github.com/dfordsoft/golib/httputil"
"github.com/dfordsoft/golib/ic"
)
Expand Down Expand Up @@ -73,8 +72,7 @@ func init() {
return
}

mobi := &ebook.Mobi{}
mobi.Begin()
gen.Begin()

var title string
var lines []string
Expand All @@ -97,7 +95,7 @@ func init() {
if idx > 0 {
title = title[:idx]
}
mobi.SetTitle(title)
gen.SetTitle(title)
continue
}
}
Expand All @@ -111,10 +109,10 @@ func init() {
s := ss[0]
finalURL := fmt.Sprintf("%s%s", u, s[1])
c := dlPage(finalURL)
mobi.AppendContent(s[2], finalURL, string(c))
gen.AppendContent(s[2], finalURL, string(c))
fmt.Println(s[2], finalURL, len(c), "bytes")
}
mobi.End()
gen.End()
},
})
}
10 changes: 4 additions & 6 deletions wutuxs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"regexp"
"time"

"github.com/dfordsoft/golib/ebook"
"github.com/dfordsoft/golib/httputil"
"github.com/dfordsoft/golib/ic"
)
Expand Down Expand Up @@ -63,8 +62,7 @@ func init() {
return
}

mobi := &ebook.Mobi{}
mobi.Begin()
gen.Begin()

var title string
// <td class="L"><a href="/html/7/7542/5843860.html">第一章.超级网吧系统</a></td>
Expand All @@ -82,7 +80,7 @@ func init() {
if len(ss) > 0 && len(ss[0]) > 0 {
s := ss[0]
title = s[1]
mobi.SetTitle(title)
gen.SetTitle(title)
continue
}
}
Expand All @@ -91,11 +89,11 @@ func init() {
s := ss[0]
finalURL := fmt.Sprintf("http://www.wutuxs.com%s", s[1])
c := dlPage(finalURL)
mobi.AppendContent(s[2], finalURL, string(c))
gen.AppendContent(s[2], finalURL, string(c))
fmt.Println(s[2], finalURL, len(c), "bytes")
}
}
mobi.End()
gen.End()
},
})
}
Loading

0 comments on commit 5840d93

Please sign in to comment.