-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (129 loc) · 5.06 KB
/
index.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//hello world!
const axios = require('axios');
const {JSDOM} = require('jsdom');
const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
var Jetty = require("jetty");
var jetty = new Jetty(process.stdout);
const { document } = (new JSDOM(`<html><script></script></html>`)).window
let nameList = process.argv[2].split(',');
let col = process.stdout.columns;
let row = process.stdout.rows;
let mid = Math.floor(row/2);
jetty.clear();
async function downloadImage(remoteURL, directoryName){
let rx = /([a-z0-9]+_[0-9]\..+)/;
let file = remoteURL.match(rx)[0];
if(!fs.existsSync(__dirname + '/' + 'images/'))
fs.mkdirSync(__dirname + '/' + 'images/');
if(!fs.existsSync(__dirname + '/' + 'images/' + directoryName))
fs.mkdirSync(__dirname + '/' + 'images/' + directoryName);
const filePath = path.resolve(__dirname, 'images', directoryName, file);
// Create writer. This is what dynamically stores data to the disk.
const writer = fs.createWriteStream(filePath);
// Get the data from ifunny.
jetty.moveTo([0,0]);
jetty.text(chalk.gray('-->') + ' ' + chalk.white('GET') + ' ' + chalk.yellow(`${remoteURL} `));
jetty.moveTo([3,0]);
const response = await axios({
url: remoteURL,
method: 'GET',
responseType: 'stream'
}).then(function(res){
//jetty.moveTo([5,0]);
jetty.text(chalk.gray('<--') + ' ' + chalk.white('GET') + ' ' + chalk.yellow(`${remoteURL} `));
jetty.moveTo([mid,0]);
jetty.text(printResCode(res.status));
return res;
}).catch(function(err){
jetty.text(chalk.red('XXX') + ' ' + chalk.white('GET') + ' ' + chalk.yellow(`${remoteURL} `));
jetty.moveTo([mid,0]);
jetty.text(printResCode(err.response.status));
});
// Start writing data to the disk
response.data.pipe(writer);
}
// Prints the status code with some color
function printResCode(code){
if(code >= 500)
return chalk.red(`status ${code}`);
if(code >= 400)
return chalk.magenta(`status ${code}`);
if(code >= 300)
return chalk.yellow(`status ${code}`);
if(code >= 200)
return chalk.green(`status ${code}`);
if(code >= 100)
return chalk.red(`status ${code}`);
}
async function mainFunction(address){
// jetty.text(address);
let nextPageKey;
let pageAddress = address;
let dirName = address.match(/\/([^\/]+?)$/)[1];
let pageCounter = 0;
let counter = 0;
let total = 0;
do{
let col = process.stdout.columns;
let row = process.stdout.rows;
let mid = Math.floor(row/2);
jetty.moveTo([0,0]);
jetty.text(chalk.gray('-->') + ' ' + chalk.white('GET') + ' ' + chalk.yellow(`${pageAddress} `));
jetty.moveTo([3,0]);
let res = await axios.get(pageAddress).then(function(res){
jetty.text(chalk.gray('<--') + ' ' + chalk.white('GET') + ' ' + chalk.yellow(`${pageAddress} `));
jetty.moveTo([mid,0]);
jetty.text(printResCode(res.status));
return res;
}).catch(function(err){
jetty.text(chalk.red('XXX') + ' ' + chalk.white('GET') + ' ' + chalk.yellow(`${pageAddress} `));
jetty.moveTo([mid,0]);
jetty.text(printResCode(err.response.status));
});
let page = document.createElement('div');
page.innerHTML = res.data;
total = res.data.match(/"total_posts":(\d*),/)[1];
// Get the next page key and address
nextPageKey = page.querySelector('div.feed__list > ul > li').getAttribute('data-next');
pageCounter++;
pageAddress = `https://ifunny.co/user/${dirName}/timeline/${nextPageKey}?page=${pageCounter}&mode=list`;
// DOM ELEMENT IMAGES
let images = page.querySelectorAll('div.feed__list > ul > li > div > div > a > img');
let imageUrls = [];
for(var i = 0; i < images.length; i++){
let img = images[i];
let imageURL = img.getAttribute('data-src');
imageURL = imageURL.replace(/(?<=ifunny.co\/)(crop|resize):[^\/]+(?=\/)/g, 'crop:x-20');
imageUrls.push(imageURL);
}
for(var i = 0; i < imageUrls.length; i++){
try{
await downloadImage(imageUrls[i], dirName);
counter ++;
let per = counter/total*100;
per = Number((per).toFixed(1));
jetty.moveTo([row,0]);
jetty.text(chalk.green(`${address} | (${i+1}/10)|(${counter}/${total})|${per}% `));
//jetty.text('\n');
if (counter == total) {
jetty.moveTo([row,0]);
jetty.text("\ndone! ")
}
}
catch(ex){
console.log(ex);
}
}
}
while(nextPageKey)
}
jetty.clear();
(async() => {
let nameList = process.argv[2].split(',');
for(var i = 0; i < nameList.length; i++){
await mainFunction("https://ifunny.co/user/" + nameList[i]);
jetty.clear();
}
})()