Last active
July 18, 2023 19:54
-
-
Save eai04191/0d6d6e01102f098765f672a0b78deacd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* pixivのフォローを全員非公開にするやつ | |
* フォロー一覧ページのコンソールに貼り付けると動きます | |
* | |
* このスクリプトはpixivのAPIを使用しています。 | |
* このスクリプトを使用したことにより使用者になんらかの不都合が発生しても、開発者は一切の責任を負いません。 | |
* 自己責任で使用してください。 | |
* | |
* また、悪意のあるスクリプトの実行は重篤なセキュリティリスクを招きます。 | |
* 悪意のあるスクリプトの実行を防ぐため、このスクリプトが何をしているのかわからない場合は絶対に使用しないでください。 | |
* 念の為そのままでは動かせないように細工がしてあります。使用する場合は編集してください。 | |
* | |
* Copyright (c) 2022 Eai | |
* Released under the MIT license | |
* https://opensource.org/licenses/mit-license.php | |
*/ | |
/** | |
* フォロー取得時、1回のリクエストで指定できるlimit値の最大 | |
*/ | |
const LIMIT_MAX = 100; | |
/** | |
* リクエストのインターバル (ms) | |
*/ | |
const REQUEST_INTERVAL = 1000; | |
function sleep(ms) { | |
return new Promise((resolve) => setTimeout(resolve, ms)); | |
} | |
function createURLSearchParams(data) { | |
const params = new URLSearchParams(); | |
Object.keys(data).forEach((key) => { | |
params.append(key, data[key]); | |
}); | |
return params; | |
} | |
/** | |
* csrfトークンを取得する | |
*/ | |
async function fetchCsrfToken() { | |
return ( | |
await ( | |
await fetch( | |
"https://www.pixiv.net/bookmark_add.php?type=illust&illust_id=95087894" | |
) | |
).text() | |
).match(/token = "([a-zA-Z0-9]{32})"/)[1]; | |
} | |
/** | |
* すべての公開フォローを取得する | |
*/ | |
async function fetchPublicFollowings() { | |
const followings = new Set(); | |
const rest = "show"; | |
let index = 0; | |
while (true) { | |
const offset = index * LIMIT_MAX; | |
const limit = LIMIT_MAX; | |
console.log( | |
`${ | |
rest === "show" ? "公開" : "非公開" | |
}フォローを取得中... offset: ${offset}` | |
); | |
const response = await fetch( | |
`https://www.pixiv.net/ajax/user/${userID}/following?offset=${offset}&limit=${limit}&rest=${rest}&lang=ja` | |
); | |
const json = await response.json(); | |
json.body.users.forEach((user) => { | |
followings.add(user); | |
}); | |
if (json.body.users.length !== 100) break; | |
index++; | |
await sleep(REQUEST_INTERVAL); | |
} | |
console.log(`${rest === "show" ? "公開" : "非公開"}フォロー取得完了`); | |
return followings; | |
} | |
/** | |
* フォローのrestrictを変更する | |
* @param {string} user_id 変更するuser_id | |
* @param {string} restrict 0: 公開 or 1: 非公開 | |
*/ | |
async function changeRestrict(user_id, restrict) { | |
const response = await fetch( | |
`https://www.pixiv.net/ajax/following/user/restrict_change`, | |
{ | |
method: "POST", | |
headers: { | |
accept: "application/json", | |
"Content-Type": | |
"application/x-www-form-urlencoded; charset=utf-8", | |
"x-csrf-token": csrfToken, | |
}, | |
body: createURLSearchParams({ | |
user_id, | |
restrict, | |
}), | |
} | |
); | |
const json = await response.json(); | |
if (json.error === false) { | |
console.log( | |
`ID: ${user_id}のフォローを${ | |
restrict === 0 ? "公開" : "非公開" | |
}に設定しました` | |
); | |
return true; | |
} | |
return false; | |
} | |
throw new Error("使用する前に説明を読んでください。"); | |
const csrfToken = await fetchCsrfToken(); | |
const userID = dataLayer[0].user_id; | |
console.log(`公開フォローを取得します`); | |
const [...publicFollowings] = await fetchPublicFollowings(); | |
console.log("公開フォローを取得しました", publicFollowings); | |
console.log(`公開フォローを全て非公開フォローに変更します`); | |
if (publicFollowings.length > 0) { | |
for (const user of publicFollowings) { | |
await changeRestrict(Number(user.userId), 1); | |
await sleep(REQUEST_INTERVAL); | |
} | |
} else { | |
console.log(`公開フォローしているユーザーはいませんでした`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment