File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,11 @@ const superagent = require('superagent');
4
4
const utils = require ( './utils' ) ;
5
5
const path = require ( 'path' ) ;
6
6
const generaterPdf = require ( './generaterPdf.js' ) ;
7
+ const downloadAudio = require ( './downloadAudio.js' ) ;
7
8
9
+ /**
10
+ * 执行方法
11
+ */
8
12
( async function getColumnArticleList ( firstArticalId ) {
9
13
await utils . createDir ( 'geektime_' + config . columnName ) ;
10
14
console . log ( '专栏文章链接开始获取' ) ;
@@ -46,6 +50,14 @@ const generaterPdf = require('./generaterPdf.js');
46
50
columnArticle . article_title + '.pdf' ,
47
51
path . resolve ( __dirname , 'geektime_' + config . columnName )
48
52
) ;
53
+ // 是否下载音频
54
+ if ( config . isdownloadVideo ) {
55
+ await downloadAudio (
56
+ columnArticle . audio_download_url ,
57
+ columnArticle . article_title + '.mp3' ,
58
+ path . resolve ( __dirname , 'geektime_' + config . columnName )
59
+ ) ;
60
+ } ;
49
61
// 判断是否还有下一篇文章
50
62
let neighborRight = columnArticle . neighbors . right ;
51
63
if ( neighborRight && neighborRight . id ) {
Original file line number Diff line number Diff line change
1
+ const path = require ( 'path' ) ;
2
+ const superagent = require ( 'superagent' ) ;
3
+ const fs = require ( 'fs' ) ;
4
+ /**
5
+ * 下载对应的音频文件
6
+ * @param {String } url 音频文件的地址
7
+ * @param {String } fileName 文件名称
8
+ * @param {String } fileDir 保存文件夹地址
9
+ */
10
+ const downloadAudio = async ( url , fileName , fileDir = __dirname ) => {
11
+ console . log ( '开始下载 音频文件: ' , fileName ) ;
12
+ if ( ! url ) throw '请传入一个音频地址' ;
13
+ if ( path . extname ( fileName ) !== '.mp3' ) { // 判断传的文件后缀是否是 mp3
14
+ fileName = fileName + '.mp3' ;
15
+ } ;
16
+ let filePath = path . resolve ( fileDir , fileName ) ;
17
+ let writeStream = fs . createWriteStream ( filePath ) ;
18
+ superagent . get ( url ) . pipe ( writeStream ) ;
19
+ console . log ( '结束下载 音频文件: ' , fileName ) ;
20
+ } ;
21
+
22
+ // downloadAudio(
23
+ // 'https://static001.geekbang.org/resource/audio/e7/b1/e7ffca8ca5b09224969b3237723a0bb1.mp3',
24
+ // '305 | 学会几个系统调用:咱们公司能接哪些类型的项目?.mp3',
25
+ // '/Users/jiang/Project/geektime2pdf/geektime_趣谈Linux操作系统')
26
+
27
+ module . exports = downloadAudio ;
You can’t perform that action at this time.
0 commit comments