Skip to content

Commit

Permalink
🐛 fixed #241
Browse files Browse the repository at this point in the history
  • Loading branch information
yann0917 committed Nov 28, 2024
1 parent 60e77e9 commit d88d8e9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,12 @@ Available Commands:
+----+-------+----------------------------------------------------------+---------------------+----------+
```

`dedao-dl dl 123 -t 1 -m -c` 下载课程ID 123 的所有课程
`dedao-dl dl 123 -t 1 -m -c -o` 下载课程ID 123 的所有课程

* -t 下载格式, 1:mp3, 2:PDF文档, 3:markdown文档 (default 1)
* -m 是否合并课程内容(针对markdown文档),默认不合并
* -c 是否下载热门留言(针对markdown文档),默认不下载
* -o 是否按顺序展示, 如果为true, 则文件名前缀会加上序号, 如 `00x.`

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

Expand Down
35 changes: 23 additions & 12 deletions cmd/app/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type CourseDownload struct {
AID int
IsMerge bool
IsComment bool
IsOrder bool
ClassName string
}

Expand All @@ -45,14 +46,14 @@ func (d *CourseDownload) Download() error {
if err != nil {
return err
}
articles, err := ArticleList(d.ID, "")
if err != nil {
return err
}

switch d.DownloadType {
case 1: // mp3
downloadData := extractDownloadData(course, articles, d.AID, 1)
articles, err := ArticleList(d.ID, "")
if err != nil {
return err
}
downloadData := extractDownloadData(course, articles, d.AID, 1, d.IsOrder)
errs := make([]error, 0)

path, err := utils.Mkdir(OutputDir, utils.FileName(course.ClassInfo.Name, ""), "MP3")
Expand Down Expand Up @@ -214,22 +215,22 @@ func Download(downloader DeDaoDownloader) error {
}

// 生成下载数据
func extractDownloadData(course *services.CourseInfo, articles *services.ArticleList, aid int, flag int) downloader.Data {
func extractDownloadData(course *services.CourseInfo, articles *services.ArticleList, aid int, flag int, isOrder bool) downloader.Data {

downloadData := downloader.Data{
Title: course.ClassInfo.Name,
}

if course.HasAudio() {
downloadData.Type = "audio"
downloadData.Data = extractCourseDownloadData(articles, aid, flag)
downloadData.Data = extractCourseDownloadData(articles, aid, flag, isOrder)
}

return downloadData
}

// 生成课程下载数据
func extractCourseDownloadData(articles *services.ArticleList, aid int, flag int) []downloader.Datum {
func extractCourseDownloadData(articles *services.ArticleList, aid int, flag int, isOrder bool) []downloader.Datum {
data := downloader.EmptyData
audioIds := map[int]string{}

Expand All @@ -255,12 +256,16 @@ func extractCourseDownloadData(articles *services.ArticleList, aid int, flag int
if len(article.Audio.AliasID) == 0 {
isCanDL = false
}
name := article.Title
if isOrder {
name = fmt.Sprintf("%03d.%s", article.OrderNum, name)
}
datum := &downloader.Datum{
ID: article.ID,
Enid: article.Enid,
ClassEnid: article.ClassEnid,
ClassID: article.ClassID,
Title: article.Title,
Title: name,
IsCanDL: isCanDL,
M3U8URL: article.Audio.Mp3PlayURL,
Streams: streams,
Expand Down Expand Up @@ -539,8 +544,11 @@ func DownloadMarkdownCourse(d *CourseDownload, path string) error {
}

name = utils.FileName(v.Title, "md")
if d.IsOrder {
name = fmt.Sprintf("%03d.%s", v.OrderNum, name)
}
fileName = filepath.Join(path, name)
fmt.Printf("正在生成文件:【\033[37;1m%s\033[0m】 ", v.Title)
fmt.Printf("正在生成文件:【\033[37;1m%s\033[0m】 ", name)
_, exist, err := utils.FileSize(fileName)

if err != nil {
Expand Down Expand Up @@ -628,8 +636,11 @@ func DownloadPdfCourse(d *CourseDownload, path string) error {
}

name = utils.FileName(v.Title, "pdf")
if d.IsOrder {
name = fmt.Sprintf("%03d.%s", v.OrderNum, name)
}
fileName = filepath.Join(path, name)
fmt.Printf("正在生成文件:【\033[37;1m%s\033[0m】 ", v.Title)
fmt.Printf("正在生成文件:【\033[37;1m%s\033[0m】 ", name)
_, exist, err := utils.FileSize(fileName)

if err != nil {
Expand All @@ -650,7 +661,7 @@ func DownloadPdfCourse(d *CourseDownload, path string) error {
res += articleCommentsToMarkdown(commentList.List)
}
}
err = utils.Md2Pdf(path, v.Title, []byte(res))
err = utils.Md2Pdf(path, name, []byte(res))
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/yann0917/dedao-dl/cmd/app"
)

var downloadType, courseMerge, courseComment = 1, false, false
var downloadType, courseMerge, courseComment, courseOrder = 1, false, false, false

var downloadCmd = &cobra.Command{
Use: "dl",
Expand Down Expand Up @@ -39,6 +39,7 @@ var downloadCmd = &cobra.Command{
AID: aid,
IsMerge: courseMerge,
IsComment: courseComment,
IsOrder: courseOrder,
}
err = app.Download(d)

Expand Down Expand Up @@ -103,6 +104,7 @@ func init() {
downloadCmd.PersistentFlags().IntVarP(&downloadType, "downloadType", "t", 1, "下载格式, 1:mp3, 2:PDF文档, 3:markdown文档")
downloadCmd.PersistentFlags().BoolVarP(&courseMerge, "merge", "m", false, "是否合并课程章节")
downloadCmd.PersistentFlags().BoolVarP(&courseComment, "comment", "c", false, "是否下载课程热门留言, 仅针对 markdown 文档")
downloadCmd.PersistentFlags().BoolVarP(&courseOrder, "order", "o", false, "是否按顺序展示, 如果为true, 则文件名前缀会加上序号, 如 00x.")

dlOdobCmd.PersistentFlags().IntVarP(&downloadType, "downloadType", "t", 1, "下载格式, 1:mp3, 2:PDF文档, 3:markdown文档")
dlEbookCmd.PersistentFlags().IntVarP(&downloadType, "downloadType", "t", 1, "下载格式, 1:html, 2:PDF文档, 3:epub")
Expand Down

0 comments on commit d88d8e9

Please sign in to comment.