Skip to content

Commit

Permalink
Merge pull request #65 from yann0917/develop
Browse files Browse the repository at this point in the history
🐛 fixed #64
  • Loading branch information
yann0917 authored Mar 10, 2022
2 parents 09a97d6 + 6e93c43 commit 354e7d0
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 46 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ Available Commands:

`dedao-dl dl 123 -t 1` 下载课程ID 123 的所有课程, -t 下载格式, 1:mp3, 2:PDF文档, 3:markdown文档 (default 1)

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

## References

* [geektime-dl](https://github.com/mmzou/geektime-dl)
Expand Down
20 changes: 18 additions & 2 deletions cmd/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -55,7 +56,7 @@ func articleList(id int) (err error) {
return
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"#", "ID", "课程名称", "更新时间", "是否阅读"})
table.SetHeader([]string{"#", "ID", "课程名称", "更新时间", "音频进度", "是否阅读"})
table.SetAutoWrapText(false)

for i, p := range list.List {
Expand All @@ -67,6 +68,7 @@ func articleList(id int) (err error) {
table.Append([]string{strconv.Itoa(i),
p.IDStr, p.Title,
utils.Unix2String(int64(p.UpdateTime)),
strconv.FormatFloat(p.Audio.ListenProgress, 'g', 5, 32),
isRead,
})
}
Expand Down Expand Up @@ -99,6 +101,14 @@ func contentsToMarkdown(contents []services.Content) (res string) {
res += getMdHeader(1) + title + "\r\n\r\n"
case "header":
res += getMdHeader(content.Level) + content.Text + "\r\n\r\n"
case "blockquote":
texts := strings.Split(content.Text, "\n")
for _, text := range texts {
res += "> " + text + "\r\n"
res += "> \r\n"
}
res = strings.TrimRight(res, "> \r\n")
res += "\r\n\r\n"
case "paragraph":
// map 转结构体
tmpJson, err := jsoniter.Marshal(content.Contents)
Expand Down Expand Up @@ -200,7 +210,13 @@ func DownloadMarkdown(cType string, id int, filePath string) error {
}
res := contentsToMarkdown(content)
fmt.Printf("正在生成文件:【\033[37;1m%s\033[0m】 ", v.Title)
f, err := os.OpenFile(filePath+"/"+v.Title+".md", os.O_CREATE|os.O_WRONLY, 0644)

filePreName := filepath.Join(filePath, v.Title)
fileName, err1 := utils.FilePath(filePreName, "md", false)
if err1 != nil {
return err1
}
f, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("\033[31;1m%s\033[0m\n", "失败"+err.Error())
return err
Expand Down
78 changes: 39 additions & 39 deletions services/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,45 @@ type MediaBaseInfo struct {

// Audio audio
type Audio struct {
AliasID string `json:"alias_id"`
Extrainfo string `json:"extrainfo"`
ClassID int `json:"class_id"`
Title string `json:"title"`
ShareTitle string `json:"share_title"`
Mp3PlayURL string `json:"mp3_play_url"`
Duration int `json:"duration"`
Schedule int `json:"schedule"`
TopicID int `json:"topic_id"`
Summary string `json:"summary"`
Price int `json:"price"`
Icon string `json:"icon"`
Size int `json:"size"`
Etag string `json:"etag"`
Token string `json:"token"`
ShareSummary string `json:"share_summary"`
Collection int `json:"collection"`
Count int `json:"count"`
BoredCount int `json:"bored_count"`
AudioType int `json:"audio_type"`
DrmVersion int `json:"drm_version"`
SourceID int `json:"source_id"`
SourceType int `json:"source_type"`
SourceIcon string `json:"source_icon"`
SourceName string `json:"source_name"`
ListenProgress int `json:"listen_progress"`
ListenFinished bool `json:"listen_finished"`
DdArticleID string `json:"dd_article_id"`
AudioListIcon string `json:"audio_list_icon"`
ClassCourseID string `json:"class_course_id"`
ClassArticleID string `json:"class_article_id"`
LogID string `json:"log_id"`
LogType string `json:"log_type"`
LogInterface string `json:"log_interface"`
Trackinfo string `json:"trackinfo"`
UsedDrm int `json:"used_drm"`
IndexImg string `json:"index_img"`
Reader string `json:"reader"`
ReaderName string `json:"reader_name"`
AliasID string `json:"alias_id"`
Extrainfo string `json:"extrainfo"`
ClassID int `json:"class_id"`
Title string `json:"title"`
ShareTitle string `json:"share_title"`
Mp3PlayURL string `json:"mp3_play_url"`
Duration int `json:"duration"`
Schedule int `json:"schedule"`
TopicID int `json:"topic_id"`
Summary string `json:"summary"`
Price int `json:"price"`
Icon string `json:"icon"`
Size int `json:"size"`
Etag string `json:"etag"`
Token string `json:"token"`
ShareSummary string `json:"share_summary"`
Collection int `json:"collection"`
Count int `json:"count"`
BoredCount int `json:"bored_count"`
AudioType int `json:"audio_type"`
DrmVersion int `json:"drm_version"`
SourceID int `json:"source_id"`
SourceType int `json:"source_type"`
SourceIcon string `json:"source_icon"`
SourceName string `json:"source_name"`
ListenProgress float64 `json:"listen_progress"`
ListenFinished bool `json:"listen_finished"`
DdArticleID string `json:"dd_article_id"`
AudioListIcon string `json:"audio_list_icon"`
ClassCourseID string `json:"class_course_id"`
ClassArticleID string `json:"class_article_id"`
LogID string `json:"log_id"`
LogType string `json:"log_type"`
LogInterface string `json:"log_interface"`
Trackinfo string `json:"trackinfo"`
UsedDrm int `json:"used_drm"`
IndexImg string `json:"index_img"`
Reader string `json:"reader"`
ReaderName string `json:"reader_name"`
}

type Video struct {
Expand Down
26 changes: 22 additions & 4 deletions utils/chromedp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"io/ioutil"
"log"
"math/rand"
"time"

"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/device"
)

// ColumnPrintToPDF print pdf
Expand All @@ -30,15 +30,32 @@ func ColumnPrintToPDF(aid string, filename string, cookies map[string]string) er

err := chromedp.Run(ctx,
chromedp.Tasks{
chromedp.Emulate(device.IPadProlandscape),
// chromedp.Emulate(device.IPadProlandscape),
enableLifeCycleEvents(),
setCookies(cookies),
navigateAndWaitFor(`https://www.dedao.cn/courseArticle/`+aid, "networkIdle"),
chromedp.ActionFunc(func(ctx context.Context) error {
s := `
var node = document.querySelector('.geetest_panel');
if (node != null ) {
node.style.display='none';
}
`
_, exp, err := runtime.Evaluate(s).Do(ctx)
if err != nil {
return err
}

if exp != nil {
return exp
}

return nil
}),
chromedp.ActionFunc(func(ctx context.Context) error {
s := `
document.querySelector('.iget-header').style.display='none';
document.querySelector('.pageControl').style.display='none';
// document.querySelector('.article-wrap').style.margin="0px 0px";
`
_, exp, err := runtime.Evaluate(s).Do(ctx)
if err != nil {
Expand Down Expand Up @@ -78,7 +95,8 @@ func ColumnPrintToPDF(aid string, filename string, cookies map[string]string) er
}),

chromedp.ActionFunc(func(ctx context.Context) error {
// time.Sleep(time.Second * 5)
// sleep 防止 496 NoCertificate
time.Sleep(time.Second * time.Duration(rand.Int31n(6)+8))
var err error
buf, _, err = page.PrintToPDF().WithPrintBackground(true).Do(ctx)
return err
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func FileName(name string, ext string) string {
name = rep.Replace(name)

if runtime.GOOS == "windows" {
rep := strings.NewReplacer("\"", " ", "?", " ", "*", " ", "\\", " ", "<", " ", ">", " ", ":", " ", ":", " ")
rep = strings.NewReplacer("\"", " ", "?", " ", "*", " ", "\\", " ", "<", " ", ">", " ", ":", " ", ":", " ")
name = rep.Replace(name)
}

Expand Down

0 comments on commit 354e7d0

Please sign in to comment.