Skip to content

Instantly share code, notes, and snippets.

@come25136
Last active May 8, 2018 11:34
Show Gist options
  • Save come25136/56bef097d0acdd09878e13d42faa3d14 to your computer and use it in GitHub Desktop.
Save come25136/56bef097d0acdd09878e13d42faa3d14 to your computer and use it in GitHub Desktop.
const twit = require("twit"),
twitter = new twit({
consumer_key: "",
consumer_secret: "",
access_token: "",
access_token_secret: ""
}),
stream = twitter.stream("user");
function block(user) {
if (!/line|LINE/.test(user.description)) return; // プロフィールにlineまたはLINEが含まれていなければここより下のコードが実行されない
twitter.post(`blocks/create`, { user_id: user.id_str });
}
twitter.get("account/verify_credentials", (err, { id_str }) => {
// 起動時にフォロワーをループでぶん回してblock関数に投げる
twitter.get("followers/ids", (err, { ids }) => {
if (err) throw err;
const count = 100,
ids100 = [];
for (let i = 0; i < Math.ceil(ids.length / count); i++) {
const index = i * count;
ids100.push(ids.slice(index, index + count));
}
ids100.forEach(ids => twitter.get("users/lookup", { user_id: ids.toString() }, (err, users) => {
if (err) throw err;
users.forEach(user => block(user));
})
);
});
// フォローされた時
stream.on("follow", ({ source: user }) => block(user));
// RTされた時(ライブラリからイベントが発火されてなかったので独自実装)
stream.on("tweet", ({ retweeted_status, user }) => retweeted_status && id_str === retweeted_status.user.id_str ? block(user) : null);
// いいねされた時(自身から自身へのいいねも拾ってしまうので三項演算子いれてます)
stream.on("favorite", ({ source: user }) => id_str !== source.id_str ? block(user) : null);
});
{
"name": "twitter_line_block",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MPL-2.0",
"dependencies": {
"twit": "^2.2.9"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment