Browse Source

增加图片下载

cr 4 years ago
parent
commit
71061e911f
4 changed files with 89 additions and 4 deletions
  1. 2 1
      app.js
  2. 79 0
      tools/down-img.js
  3. 6 2
      tools/page-init.js
  4. 2 1
      tools/save-file.js

+ 2 - 1
app.js

@@ -7,6 +7,7 @@ const { baseData } = require('./tools/base-data');
7 7
 const { skuData } = require('./tools/sku-data');
8 8
 const { videoData } = require('./tools/video-data');
9 9
 const { imgsData } = require('./tools/imgs-data');
10
+const { downImg } = require('./tools/down-img');
10 11
 
11 12
 
12 13
 (async function example() {
@@ -61,7 +62,7 @@ const { imgsData } = require('./tools/imgs-data');
61 62
     console.log('开始保存')
62 63
     // video TODO: 视频用的blob暂不处理
63 64
     // data.video = await videoData(driver);
64
-
65 65
     saveFile(`test.html`, JSON.stringify(data, null, 4));
66
+    downImg(data)
66 67
 
67 68
 })();

+ 79 - 0
tools/down-img.js

@@ -1 +1,80 @@
1
+const https = require('https');
2
+const fs = require('fs');
3
+const { format } = require('path');
1 4
 
5
+/**
6
+ * 下载图片
7
+ * @param {*请求图片地址} url 
8
+ * @param {*保存图片名称} name 
9
+ */
10
+let downImg = (obj) => {
11
+    if (type(obj) === 'string') {
12
+        // 暂定图片都在img.alicdn.com下
13
+        if (obj.indexOf('img.alicdn.com') !== -1) {
14
+            if(obj.indexOf('//') === 0){
15
+                obj = 'https:'+obj;
16
+            }
17
+            updataImg(obj);
18
+        }
19
+    } else if (type(obj) === 'object') {
20
+        for (const n in obj) {
21
+            downImg(obj[n])
22
+        }
23
+    } else if (type(obj) === 'array') {
24
+        for (let item of obj) {
25
+            downImg(item)
26
+        }
27
+    }
28
+}
29
+
30
+
31
+function type(obj) {
32
+    var toString = Object.prototype.toString;
33
+    var map = {
34
+        '[object Boolean]': 'boolean',
35
+        '[object Number]': 'number',
36
+        '[object String]': 'string',
37
+        '[object Function]': 'function',
38
+        '[object Array]': 'array',
39
+        '[object Date]': 'date',
40
+        '[object RegExp]': 'regExp',
41
+        '[object Undefined]': 'undefined',
42
+        '[object Null]': 'null',
43
+        '[object Object]': 'object'
44
+    };
45
+    return map[toString.call(obj)];
46
+}
47
+
48
+function updataImg(url) {
49
+    //先访问图片
50
+    https.get(url, (res) => {
51
+
52
+        //用来存储图片二进制编码
53
+        let imgData = '';
54
+
55
+        let name = url.replace(/.+\/(.+)$/igm, '$1');
56
+
57
+        //设置图片编码格式
58
+        res.setEncoding("binary");
59
+
60
+        //检测请求的数据
61
+        res.on('data', (chunk) => {
62
+            imgData += chunk;
63
+        })
64
+
65
+        //请求完成执行的回调
66
+        res.on('end', () => {
67
+
68
+            // 通过文件流操作保存图片
69
+            fs.writeFile(`./output/imgs/${name}`, imgData, 'binary', (error) => {
70
+                // if (error) {
71
+                //     console.log('下载失败');
72
+                // } else {
73
+                //     console.log('下载成功!')
74
+                // }
75
+            })
76
+        })
77
+    })
78
+}
79
+
80
+exports.downImg = downImg;

+ 6 - 2
tools/page-init.js

@@ -5,6 +5,7 @@ const webdriver = require('selenium-webdriver');
5 5
 const { saveFile } = require('../tools/save-file');
6 6
 const { baseData } = require('../tools/base-data');
7 7
 const { skuData } = require('../tools/sku-data');
8
+const { downImg } = require('./down-img');
8 9
 
9 10
 function pageInit(driver) {
10 11
 
@@ -26,7 +27,7 @@ function pageInit(driver) {
26 27
         driver.sleep(20000 / 50 * 10 + 1000).then(async () => {
27 28
 
28 29
             console.log('自动滚动完成');
29
-            
30
+
30 31
             // 判断是否有验证框
31 32
             try {
32 33
                 const dialog = await driver.findElement(webdriver.By.className('baxia-dialog'));
@@ -48,8 +49,11 @@ function pageInit(driver) {
48 49
                 skuData(driver).then(sku => {
49 50
                     data.sku = sku;
50 51
                     console.log('sku获取完成')
52
+
51 53
                     saveFile(`/json/${data.title}.json`, JSON.stringify(data, null, 4));
52
-                    const random = parseInt(Math.random() * 6 + 3)*1000;
54
+                    downImg(data);
55
+                    
56
+                    const random = parseInt(Math.random() * 6 + 3) * 1000;
53 57
                     setTimeout(() => {
54 58
                         console.log('完成一个');
55 59
                         resolve();

+ 2 - 1
tools/save-file.js

@@ -1,12 +1,13 @@
1 1
 // 保存文件
2 2
 const fs = require('fs');
3 3
 const path = require('path');
4
+const { log_w } = require('./log-w');
4 5
 
5 6
 function saveFile(fileName, con) {
6 7
 
7 8
     fs.writeFile(`./output/${fileName}`, con, { 'flag': 'w' }, function (err) {
8 9
         if (err) throw err;
9
-        console.log(`写入${fileName}成功`);
10
+        log_w(`写入${fileName}成功`);
10 11
     });
11 12
     
12 13
 }