Created
July 17, 2017 10:55
-
-
Save 599316527/ec61b7bdc0afc8d8e8363a110cdd8c43 to your computer and use it in GitHub Desktop.
批量删微博
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 在 m.weibo.cn 的 console 里跑 | |
*/ | |
let startPageNo = 1 | |
let pageLength = 90 | |
let delInterval = 1543 | |
let options = { | |
credentials: 'include' | |
} | |
let seq = Array.from(new Array(pageLength), function (a, i) { | |
return i + startPageNo | |
}).map(function (i) { | |
return function (previousCards = []) { | |
return fetch('https://m.weibo.cn/api/container/getIndex' | |
+ '?containerid=2304131713075833_-_WEIBO_SECOND_PROFILE_WEIBO&page_type=03' | |
+ '&page=' + i, | |
options | |
).then(function (res) { | |
return res.json() | |
}).then(function ({ok, cards}) { | |
if (!ok) { | |
return Promise.resolve([]) | |
} | |
cards = cards.filter(function (card) { | |
return card.card_type === 9 | |
}).map(function ({mblog}) { | |
return mblog.mid | |
}) | |
console.log('获取mid... %d条', cards.length) | |
return previousCards.concat(cards) | |
}) | |
} | |
}) | |
waterfall(seq).then(function (mids) { | |
console.log(mids.join(',')) | |
console.log('预计在 %s 结束', (new Date(Date.now() + mids.length * (delInterval + 400))).toString()) | |
return waterfall(getDelTasks(mids)) | |
}).catch(function (err) { | |
console.log(err) | |
}) | |
function getDelTasks(mids) { | |
return mids.map(function (mid) { | |
return function () { | |
let body = new FormData() | |
body.append('st', 'a346b3') // <-- 这个先删一条抓包看下是啥 | |
body.append('id', mid) | |
return new Promise(function (resolve, reject) { | |
return fetch('https://m.weibo.cn/mblogDeal/delMyMblog', Object.assign({ | |
method: 'POST', | |
body | |
}, options)).then(function (res) { | |
return res.json() | |
}).then(function ({ok, msg}) { | |
console.log('[%s] %s', mid, msg) | |
setTimeout(function () { | |
// 删太快了会出一段时间的 403 | |
resolve() | |
}, delInterval) | |
}).catch(function (err) { | |
console.log('[%s] 删除失败 %s', mid, err.message) | |
reject() | |
}) | |
}) | |
} | |
}) | |
} | |
// https://github.com/dotSlashLu/promise-waterfall | |
function isPromise(obj) { | |
return obj && typeof obj.then === 'function'; | |
} | |
function waterfall(list) { | |
// malformed argument | |
list = Array.prototype.slice.call(list); | |
if (!Array.isArray(list) // not an array | |
|| typeof list.reduce !== "function" // update your javascript engine | |
|| list.length < 1 // empty array | |
) { | |
return Promise.reject("Array with reduce function is needed."); | |
} | |
if (list.length == 1) { | |
if (typeof list[0] != "function") | |
return Promise.reject("First element of the array should be a function, got " + typeof list[0]); | |
return Promise.resolve(list[0]()); | |
} | |
return list.reduce(function(l, r){ | |
// first round | |
// execute function and return promise | |
var isFirst = (l == list[0]); | |
if (isFirst) { | |
if (typeof l != "function") | |
return Promise.reject("List elements should be function to call."); | |
var lret = l(); | |
if (!isPromise(lret)) | |
return Promise.reject("Function return value should be a promise."); | |
else | |
return lret.then(r); | |
} | |
// other rounds | |
// l is a promise now | |
// priviousPromiseList.then(nextFunction) | |
else { | |
if (!isPromise(l)) | |
Promise.reject("Function return value should be a promise."); | |
else | |
return l.then(r); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment