-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
executable file
·120 lines (101 loc) · 3.43 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Created by: Luojinghui/[email protected]
* Date: 2017/2/7
* Time: 上午10:53
*/
//依赖模块
var fs = require('fs');
var request = require("request");
var cheerio = require("cheerio");
var mkdirp = require('mkdirp');
//目标网址
//在此处替换你的user_id,和token
// var baseUrl = 'https://api.instagram.com/v1/users/YOUR_USER_ID/media/recent/?access_token=YOUR_USER_TOKEN';
var baseUrl = 'https://api.instagram.com/v1/users/7408520229/media/recent/?access_token=7408520229.13f8fba.07c1b0169eaa4272b861000e0f76b289';
//本地存储目录
//注意,我的最终目录是在source目录底下
var dir = './img/';
//创建目录
mkdirp(dir, function (err) {
if (err) {
console.log(err);
}
});
//发送请求
request({uri: baseUrl,'proxy':'http://localhost:9667'}, function (error, response, body) {
if (!error && response.statusCode == 200) {
let resData = JSON.parse(body).data;
resData.forEach(function (value, index) {
let i=0;
let imgCar = value.carousel_media;
if(imgCar){
imgCar.forEach(function (value1, index) {
let imgSrc = value1.images.standard_resolution.url;
console.log('正在下载原图' + imgSrc);
download(imgSrc, dir, value.id+'_'+i);
i++;
console.log('下载完成');
});
} else {
let imgSrc = value.images.standard_resolution.url;
console.log('正在下载原图' + imgSrc);
download(imgSrc, dir, value.id+'_'+i);
console.log('下载完成');
}
});
resData.forEach(function (value, index) {
let j=0;
let imgCar = value.carousel_media;
if(imgCar){
imgCar.forEach(function (value1, index) {
let thumbnailSrc = value1.images.thumbnail.url;
console.log('正在下载压缩图' + thumbnailSrc);
download(thumbnailSrc, dir, value.id +'_' + j + '.min');
j++;
console.log('下载完成');
});
} else {
let thumbnailSrc = value.images.thumbnail.url;
console.log('正在下载压缩图' + thumbnailSrc);
download(thumbnailSrc, dir, value.id +'_' + j + '.min');
console.log('下载完成');
}
});
//获取的json数据保存到本地备用
fs.writeFile('../../source/instagram/ins.json',body,function(err){
if(err) throw err;
console.log('write JSON into TEXT');
});
}
});
/*
//发送请求
request({uri: baseUrl,'proxy':'http://localhost:9667'}, function (error, response, body) {
if (!error && response.statusCode == 200) {
let resData = JSON.parse(body).data;
resData.forEach(function (value, index) {
let imgSrc = value.images.standard_resolution.url;
console.log('正在下载原图' + imgSrc);
download(imgSrc, dir, value.id);
console.log('下载完成');
});
resData.forEach(function (value, index) {
let thumbnailSrc = value.images.thumbnail.url;
console.log('正在下载压缩图' + thumbnailSrc);
download(thumbnailSrc, dir, value.id + '.min');
console.log('下载完成');
});
//获取的json数据保存到本地备用
fs.writeFile('../ins.json',body,function(err){
if(err) throw err;
console.log('write JSON into TEXT');
});
}
});
*/
//下载方法
var download = function (url, dir, filename) {
request.head(url, function (err, res, body) {
request({uri: url,'proxy':'http://localhost:9667'}).pipe(fs.createWriteStream(dir + "/" + filename + ".jpg"));
});
};