Skip to content

Commit

Permalink
Merge pull request #96 from yann0917/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
yann0917 authored Jul 18, 2022
2 parents eb98109 + c4ab3af commit b5695a8
Show file tree
Hide file tree
Showing 19 changed files with 600 additions and 424 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
* 可查看知识城邦推荐话题精选内容
* 课程可生成PDF,文稿生成 Markdown 文档,也可生成 mp3 文件
* 每天听本书可下载音频
* 电子书可下载 pdf(有 bug ,待解决)
* 可切换登录账号

## 安装

### 安装依赖

* chromedp
> 生成 PDF 需要借助 [chromedp](https://github.com/chromedp/chromedp), 该功能必须安装 [Google-Chrome](https://www.google.cn/intl/zh-CN/chrome/) 浏览器
> 课程生成 PDF 需要借助 [chromedp](https://github.com/chromedp/chromedp), 该功能必须安装 [Google-Chrome](https://www.google.cn/intl/zh-CN/chrome/) 浏览器
* ffmpeg
> 音频需要借助 [ffmpeg](https://ffmpeg.org/) 合成
* wkhtmltopdf
> 电子书转 PDF 需要借助[wkhtmltopdf](https://wkhtmltopdf.org/downloads.html)
### 使用 `go get` 安装

Expand Down Expand Up @@ -85,6 +88,7 @@ Available Commands:
cat 获取课程分类
course 获取我购买过课程
dl 下载已购买课程, 并转换成 PDF & 音频 & markdown
dle 下载电子书
dlo 下载每天听本书音频
ebook 获取我的电子书架
help Help about any command
Expand Down Expand Up @@ -235,6 +239,8 @@ Available Commands:

注意:生成 PDF 的时候,操作过于频繁会触发 `496 NoCertificate` , 因此每次生成一次PDF sleep 0~5秒, 尽管如此,还是有极大可能触发操作频繁图形验证。

`dedao-dl dle 123` 下载电子书,先通过 `dedao-dl ebook` 获取要下载的电子书 id.

## References

* [geektime-dl](https://github.com/mmzou/geektime-dl)
Expand Down
95 changes: 68 additions & 27 deletions cmd/app/ebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,87 @@ package app

import (
"fmt"
"os"
"strconv"

"github.com/olekukonko/tablewriter"
"github.com/yann0917/dedao-dl/services"
"github.com/yann0917/dedao-dl/utils"
)

// EbookDetail 电子书详情
func EbookDetail(id int) (err error) {
func EbookDetail(id int) (detail *services.EbookDetail, err error) {
courseDetail, err := getService().CourseDetail(CateEbook, id)
if err != nil {
return
}

enID := courseDetail.Enid
detail, err := getService().EbookDetail(enID)
detail, err = getService().EbookDetail(enID)

return
}

func EbookInfo(enID string) (info *services.EbookInfo, err error) {
token, err1 := getService().EbookReadToken(enID)
if err1 != nil {
err = err1
return
}

info, err = getService().EbookInfo(token.Token)
return
}

func EbookPage(title, enID string) (pages *services.EbookPage, err error) {
token, err1 := getService().EbookReadToken(enID)
if err1 != nil {
err = err1
return
}

info, err1 := getService().EbookInfo(token.Token)
if err1 != nil {
err = err1
return
}
var svgContent []string
for _, order := range info.BookInfo.Orders {

index, count, offset := 0, 20, 0
svgList, err1 := generateEbookPages(order.ChapterID, token.Token, index, count, offset)
if err1 != nil {
err = err1
return
}
svgContent = append(svgContent, svgList...)

}
if len(svgContent) > 0 {
utils.Svg2Html(title, svgContent)
}
return
}

func generateEbookPages(chapterID, token string, index, count, offset int) (svgList []string, err error) {
fmt.Printf("chapterID:%#v\n", chapterID)
pageList, err := getService().EbookPages(chapterID, token, index, count, offset)
if err != nil {
return
}

out := os.Stdout
table := tablewriter.NewWriter(out)
fmt.Fprint(out, "书名:"+detail.OperatingTitle+"\n")
fmt.Fprint(out, "单价:"+detail.Price+"\n")
fmt.Fprint(out, "作者:"+detail.BookAuthor+"\n")
fmt.Fprint(out, "类型:"+detail.ClassifyName+"\n")
fmt.Fprint(out, "专家推荐指数:"+detail.ProductScore+"\n")
fmt.Fprint(out, "豆瓣评分:"+detail.DoubanScore+"\n")
fmt.Fprint(out, "发行日期:"+detail.PublishTime+"\n")
fmt.Fprint(out, "出版社:"+detail.Press.Name+"\n")
fmt.Fprintln(out)

table.SetHeader([]string{"#", "ID", "章节名称"})
table.SetAutoWrapText(false)
// table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
// table.SetCenterSeparator("|")
for i, p := range detail.CatalogList {
table.Append([]string{strconv.Itoa(i), strconv.Itoa(p.PlayOrder),
p.Text,
})
}
table.Render()
for _, item := range pageList.Pages {
svgList = append(svgList, item.Svg)
}
fmt.Printf("IsEnd:%#v\n", pageList.IsEnd)
if !pageList.IsEnd {
index = count
count += 20
list, err1 := generateEbookPages(chapterID, token, index, count, offset)
if err1 != nil {
err = err1
return
}

svgList = append(svgList, list...)
}
// utils.WriteFileWithTrunc(chapterID+".svg", strings.Join(svgList, "\n"))
return
}
48 changes: 39 additions & 9 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,31 @@ var dlOdobCmd = &cobra.Command{
},
}

var dlEbookCmd = &cobra.Command{
Use: "dle",
Short: "下载电子书",
Long: `使用 dedao-dl dle 下载电子书`,
Example: "dedao-dl dle 123",
PreRunE: AuthFunc,
RunE: func(cmd *cobra.Command, args []string) error {

id, err := strconv.Atoi(args[0])
if err != nil {
return errors.New("电子书ID错误")
}
aid := 0
if len(args) > 1 {
return errors.New("参数错误")
}
err = download(app.CateEbook, id, aid)
return err
},
}

func init() {
rootCmd.AddCommand(downloadCmd)
rootCmd.AddCommand(dlOdobCmd)
rootCmd.AddCommand(dlEbookCmd)
downloadCmd.PersistentFlags().IntVarP(&downloadType, "downloadType", "t", 1, "下载格式, 1:mp3, 2:PDF文档, 3:markdown文档")
}

Expand Down Expand Up @@ -155,19 +177,27 @@ func download(cType string, id, aid int) error {
if err := downloader.Download(datum, stream, path); err != nil {
errors = append(errors, err)
}
// use m3u8 downloader
// downloader, err := downloader.NewTask(path, datum.M3U8URL)
// if err != nil {
// errors = append(errors, err)
// }
// outName := datum.Title + ".mp3"
// if err := downloader.Start(25, outName); err != nil {
// errors = append(errors, err)
// }
}
if len(errors) > 0 {
return errors[0]
}

case app.CateEbook:
detail, err := app.EbookDetail(id)
if err != nil {
return err
}

title := strconv.Itoa(id) + "_"
if detail.Title != "" {
title += detail.Title
} else if detail.OperatingTitle != "" {
title += detail.OperatingTitle
}

title += "_" + detail.BookAuthor
app.EbookPage(title, detail.Enid)

}
return nil
}
Expand Down
38 changes: 35 additions & 3 deletions cmd/ebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package cmd

import (
"fmt"
"os"
"strconv"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/yann0917/dedao-dl/cmd/app"
)
Expand All @@ -15,10 +18,8 @@ var ebookCmd = &cobra.Command{
Example: "dedao-dl ebook",
PreRunE: AuthFunc,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("bookID", bookID)
if bookID > 0 {
return app.EbookDetail(bookID)

return ebookDetail(bookID)
}
return courseList(app.CateEbook)
},
Expand All @@ -39,3 +40,34 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
}

func ebookDetail(id int) (err error) {
detail, err := app.EbookDetail(id)
if err != nil {
return
}

out := os.Stdout
table := tablewriter.NewWriter(out)
fmt.Fprint(out, "书名:"+detail.OperatingTitle+"\n")
fmt.Fprint(out, "单价:"+detail.Price+"\n")
fmt.Fprint(out, "作者:"+detail.BookAuthor+"\n")
fmt.Fprint(out, "类型:"+detail.ClassifyName+"\n")
fmt.Fprint(out, "专家推荐指数:"+detail.ProductScore+"\n")
fmt.Fprint(out, "豆瓣评分:"+detail.DoubanScore+"\n")
fmt.Fprint(out, "发行日期:"+detail.PublishTime+"\n")
fmt.Fprint(out, "出版社:"+detail.Press.Name+"\n")
fmt.Fprintln(out)

table.SetHeader([]string{"#", "ID", "章节名称"})
table.SetAutoWrapText(false)
// table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
// table.SetCenterSeparator("|")
for i, p := range detail.CatalogList {
table.Append([]string{strconv.Itoa(i), strconv.Itoa(p.PlayOrder),
p.Text,
})
}
table.Render()
return
}
8 changes: 0 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ func (c *ConfigsData) Init() error {
return errors.New("配置文件未找到")
}

// 初始化默认配置
c.initDefaultConfig()
// 从配置文件中加载配置
err := c.loadConfigFromFile()
if err != nil {
Expand All @@ -68,7 +66,6 @@ func (c *ConfigsData) Init() error {
return nil
}

fmt.Printf("xxxxxx %#v\n", c.activeUser)
if c.activeUser != nil {
c.service = c.activeUser.New()
}
Expand Down Expand Up @@ -151,11 +148,6 @@ func (c *ConfigsData) Save() error {
return nil
}

func (c *ConfigsData) initDefaultConfig() {
// todo 默认配置
c.Save()
}

func (c *ConfigsData) loadConfigFromFile() error {
err := c.lazyOpenConfigFile()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/yann0917/dedao-dl
go 1.16

require (
github.com/JoshVarga/svgparser v0.0.0-20200804023048-5eaba627a7d1
github.com/SebastiaanKlippert/go-wkhtmltopdf v1.7.2
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/cheggaaa/pb/v3 v3.1.0
github.com/chromedp/cdproto v0.0.0-20220629234738-4cfc9cdeeb92
Expand All @@ -15,11 +17,10 @@ require (
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/mapstructure v1.5.0
github.com/olekukonko/tablewriter v0.0.5
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.0 // indirect
github.com/ysmood/gson v0.7.2 // indirect
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
Loading

0 comments on commit b5695a8

Please sign in to comment.