Skip to content

Instantly share code, notes, and snippets.

@gucheen
Created December 27, 2018 06:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gucheen/61c954dc2ea8b5af612c5c2914a9cde4 to your computer and use it in GitHub Desktop.
Save gucheen/61c954dc2ea8b5af612c5c2914a9cde4 to your computer and use it in GitHub Desktop.
const request = require('request');
const util = require('util');
const fs = require('fs');
const path = require('path');
const os = require('os');
const get = util.promisify(request.get);
const post = util.promisify(request.post);
const sleep = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
const getResources = async ({ uid }) => {
let page = 1;
const count = 25;
let total = 0;
let amount = 0;
const urls = [];
const data = [];
let user;
while(true) {
const url = util.format('https://m.weibo.cn/api/container/getIndex?count=%s&page=%s&containerid=107603%s', count, page, uid);
const response = await get({
uri: url,
json: true,
});
if (!response || response.statusCode !== 200) {
continue;
}
const body = response.body;
if (body.ok === 0) {
break;
}
if (total === 0) {
total = body.data.cardlistInfo.total;
}
const cards = body.data.cards;
cards.forEach((card) => {
if (!user && card.user && card.user.id === uid) {
user = card.user;
}
if (card.mblog) {
amount += 1;
const mblog = {
pics: [],
};
const props = ['created_at', 'id', 'text', 'thumbnail_pic'];
props.forEach((prop) => {
mblog[prop] = card.mblog[prop];
});
// console.log('analysing weibos... %s', );
if (card.mblog.pics) {
card.mblog.pics.forEach((pic) => {
if (pic.large) {
urls.push(pic.large.url);
mblog.pics.push({
id: pic.pid,
url: pic.large.url,
});
}
});
} else if (card.mblog.video && card.mblog.page_info) {
if (card.mblog.page_info.media_info && card.mblog.page_info.media_info.stream_url) {
urls.push(card.mblog.page_info.media_info && card.mblog.page_info.media_info.stream_url);
}
}
data.push(mblog);
}
});
page += 1;
await sleep(1000);
}
console.log(`--> ${new Date().toJSON()} [requested]: ${uid}`);
console.log(urls.join('\n'));
return {
urls,
data,
user,
};
};
const saveResourcesToTxt = async ({ uid, aria2 = false }) => {
console.log(`--> ${new Date().toJSON()} [start]: ${uid}`);
const { urls, data, user } = await getResources({ uid });
fs.writeFile(path.join(os.homedir(), `data/${uid}.txt`), urls.join('\n'), () => {
console.log(`--> ${new Date().toJSON()} [saved]: ${uid}`);
});
if (aria2) {
const aria2Params = urls.map((url) => ({
methodName: 'aria2.addUri',
params: [
'token:feifei824',
[
url,
],
{
dir: path.join(os.homedir(), `pics/${uid}`),
},
],
}));
const rpcRequest = {
uri: 'http://localhost:6800/jsonrpc',
body: {
jsonrpc: '2.0',
id: uid,
method: 'system.multicall',
params: [
aria2Params,
],
},
json: true,
};
downloadUseAria2(rpcRequest);
}
};
const downloadUseAria2 = (rpcRequest) => {
post(rpcRequest)
.then((response) => {
console.log(`--> ${new Date().toJSON()} [aria2]: `, JSON.stringify(response.body));
})
.catch((error) => {
console.error(`--> ${new Date().toJSON()} [aria2-error]: `, JSON.stringify(error));
});
}
module.exports = {
getResources,
saveResourcesToTxt,
downloadUseAria2,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment