Skip to content

Commit

Permalink
Merge pull request #53 from yann0917/develop
Browse files Browse the repository at this point in the history
🐛 print pdf failed
  • Loading branch information
yann0917 authored Jan 3, 2022
2 parents 4a9ee54 + 6551c20 commit 934d9db
Show file tree
Hide file tree
Showing 14 changed files with 418 additions and 180 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* 可查看听书书架,电子书架列表
* 可查看已购买的锦囊
* 可查看知识城邦推荐话题精选内容
* 课程可生成PDF,也可生成 mp3 文件
* 课程可生成PDF,文稿生成 Markdown 文档,也可生成 mp3 文件
* 每天听本书可下载音频
* 可切换登录账号

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


`dedao-dl article -i xxx` 查看课程文章信息

```markdown
Expand Down
9 changes: 5 additions & 4 deletions cmd/app/article.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"fmt"
"math"

"github.com/pkg/errors"
Expand Down Expand Up @@ -35,7 +36,7 @@ func ArticleList(id int, chapterID string) (list *services.ArticleList, err erro
// ArticleInfo article info
//
// get article token, audio token, media security token etc
func ArticleInfo(id, aid int) (info *services.ArticleInfo, err error) {
func ArticleInfo(id, aid int) (info *services.ArticleInfo, aEnid string, err error) {
list, err := ArticleList(id, "")
if err != nil {
return
Expand All @@ -44,7 +45,6 @@ func ArticleInfo(id, aid int) (info *services.ArticleInfo, err error) {
var aids []int

// get article enid
var aEnid string
for _, p := range list.List {
aids = append(aids, p.ID)
if p.ClassID == id && p.ID == aid {
Expand All @@ -65,14 +65,15 @@ func ArticleInfo(id, aid int) (info *services.ArticleInfo, err error) {

// ArticleDetail article detail
func ArticleDetail(id, aid int) (detail *services.ArticleDetail, err error) {
info, err := ArticleInfo(id, aid)
info, aEnid, err := ArticleInfo(id, aid)
if err != nil {
return
}
token := info.DdArticleToken
appid := "1632426125495894021"
detail, err = getService().ArticleDetail(token, info.ArticleInfo.Enid, appid)
detail, err = getService().ArticleDetail(token, aEnid, appid)
if err != nil {
fmt.Printf("err:%#v\n", err)
return
}
return
Expand Down
94 changes: 92 additions & 2 deletions cmd/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strconv"
"strings"

jsoniter "github.com/json-iterator/go"
"github.com/olekukonko/tablewriter"
Expand Down Expand Up @@ -82,10 +83,99 @@ func articleDetail(id, aid int) (err error) {
out := os.Stdout
table := tablewriter.NewWriter(out)

var content services.Content
var content []services.Content
jsoniter.UnmarshalFromString(detail.Content, &content)
fmt.Fprint(out, content.Plaintext)
fmt.Fprint(out, contentsToMarkdown(content))
fmt.Fprintln(out)
table.Render()
return
}

func contentsToMarkdown(contents []services.Content) (res string) {
for _, content := range contents {
switch content.Type {
case "audio":
title := strings.TrimRight(content.Title, ".mp3")
res += getMdHeader(1) + title + "\r\n\r\n"

case "header":
res += getMdHeader(content.Level) + content.Text + "\r\n\r\n"
case "paragraph":
for _, item := range content.Contents {
subContent := strings.Trim(item.Text.Content, " ")
switch item.Type {
case "text":
if item.Text.Bold {
res += " **" + subContent + "** "
} else if item.Text.Highlight {
res += " *" + subContent + "* "
} else {
res += subContent
}
}
}
res = strings.Trim(res, " ")
res += "\r\n\r\n"
case "elite": // 划重点
res += getMdHeader(2) + "划重点\r\n\r\n" + content.Text + "\r\n\r\n"

case "image":
res += "![" + content.URL + "](" + content.URL + ")" + "\r\n\r\n"
case "label-group":
res += getMdHeader(2) + "`" + content.Text + "`" + "\r\n\r\n"
}
}

res += "---\r\n"
return
}

func getMdHeader(level int) string {
switch level {
case 1:
return "# "
case 2:
return "## "
case 3:
return "### "
case 4:
return "#### "
case 5:
return "##### "
case 6:
return "###### "
default:
return ""
}
}

func DownloadMarkdown(cType string, id int, filePath string) error {
switch cType {
case app.CateCourse:
list, err := app.ArticleList(id, "")
if err != nil {
return err
}
for _, v := range list.List {
detail, err := app.ArticleDetail(id, v.ID)
if err != nil {
return err
}
var content []services.Content
jsoniter.UnmarshalFromString(detail.Content, &content)
res := contentsToMarkdown(content)
f, err := os.OpenFile(filePath+"/"+v.Title+".md", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error opening file")
return err
}
defer f.Close()
f.WriteString(res)
}
case app.CateAudioBook:

}

return nil

}
2 changes: 1 addition & 1 deletion cmd/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func courseList(category string) (err error) {
classID, p.Title, p.Author,
utils.Unix2String(int64(p.CreateTime)),
p.Price,
strconv.Itoa(p.Progress),
strconv.Itoa(p.Progress) + "%",
})
}
table.Render()
Expand Down
20 changes: 17 additions & 3 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var OutputDir = "output"
var downloadCmd = &cobra.Command{
Use: "dl",
Short: "下载已购买课程,并转换成 PDF & 音频",
Long: `使用 dedao-dl dl 下载已购买课程, 并转换成 PDF & 音频`,
Long: `使用 dedao-dl dl 下载已购买课程, 并转换成 PDF & 音频 & markdown`,
Example: "dedao-dl dl 123",
PreRunE: AuthFunc,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -78,7 +78,9 @@ func download(cType string, id, aid int) error {
downloadData := extractDownloadData(course, articles, aid)
errors := make([]error, 0)
path, err := utils.Mkdir(OutputDir, utils.FileName(course.ClassInfo.Name, ""), "MP3")

if err != nil {
return err
}
for _, datum := range downloadData.Data {
if !datum.IsCanDL {
continue
Expand All @@ -100,6 +102,7 @@ func download(cType string, id, aid int) error {
if len(errors) > 0 {
return errors[0]
}

// 下载 PDF
path, err = utils.Mkdir(OutputDir, utils.FileName(course.ClassInfo.Name, ""), "PDF")
if err != nil {
Expand All @@ -118,6 +121,14 @@ func download(cType string, id, aid int) error {
if len(errors) > 0 {
return errors[0]
}

// 下载 Markdown
path, err = utils.Mkdir(OutputDir, utils.FileName(course.ClassInfo.Name, ""), "MD")
if err != nil {
return err
}
DownloadMarkdown(app.CateCourse, id, path)

case app.CateAudioBook:
list, err := app.CourseList(cType)
if err != nil {
Expand All @@ -131,6 +142,9 @@ func download(cType string, id, aid int) error {
downloadData.Data = extractOdobDownloadData(list, id)
errors := make([]error, 0)
path, err := utils.Mkdir(OutputDir, utils.FileName(fileName, ""), "MP3")
if err != nil {
return err
}
for _, datum := range downloadData.Data {
if !datum.IsCanDL {
continue
Expand Down Expand Up @@ -245,7 +259,7 @@ func extractOdobDownloadData(lists *services.CourseList, aid int) []downloader.D
},
}
isCanDL := true
if article.HasPlayAuth == false {
if !article.HasPlayAuth {
isCanDL = false
}
datum := &downloader.Datum{
Expand Down
33 changes: 10 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,18 @@ module github.com/yann0917/dedao-dl
go 1.15

require (
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/cheggaaa/pb/v3 v3.0.8
github.com/chromedp/cdproto v0.0.0-20210713064928-7d28b402946a
github.com/chromedp/chromedp v0.7.4
github.com/coreos/bbolt v1.3.2 // indirect
github.com/coreos/etcd v3.3.13+incompatible // indirect
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/go-rod/rod v0.101.4
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/jonboulle/clockwork v0.1.0 // indirect
github.com/json-iterator/go v1.1.11
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.4.1
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/chromedp/cdproto v0.0.0-20211226214557-5a9a0564ad26
github.com/chromedp/chromedp v0.7.6
github.com/go-rod/rod v0.101.8
github.com/json-iterator/go v1.1.12
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/mapstructure v1.4.3
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/prometheus/client_golang v0.9.3 // indirect
github.com/soheilhy/cmux v0.1.4 // indirect
github.com/spf13/cobra v1.2.1
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.etcd.io/bbolt v1.3.2 // indirect
gopkg.in/resty.v1 v1.12.0 // indirect
github.com/spf13/cobra v1.3.0
github.com/ysmood/gson v0.7.0 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
)
Loading

0 comments on commit 934d9db

Please sign in to comment.