Skip to content

Instantly share code, notes, and snippets.

@Lycolia
Last active June 6, 2023 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lycolia/11e132dd6407af9a70e45dfc3e296875 to your computer and use it in GitHub Desktop.
Save Lycolia/11e132dd6407af9a70e45dfc3e296875 to your computer and use it in GitHub Desktop.
Misskey notes remover
<?php
# =====================================================
# Confirmed work with Misskey 13.12.2
# Requires openssl extension and PHP 7.4.0 or later
# Edit php.ini
# ;extension=openssl -> extension=openssl
# =====================================================
set_time_limit(0);
# ==================== Config Start ===================
# 1. Chrome devtools -> Network
# 2. filter `users/notes`
# 3. Payload tab -> `userId` value
define("USER_ID", "xxxxxxxxxx");
# 1. Chrome devtools -> Network
# 2. filter `users/notes`
# 3. Payload tab -> `i` value
define("TIMELINE_CHANNNEL", "XXXXXXXXXXXXXXXX");
# On misskey website
# 設定 -> API -> アクセストークン -> 全て有効にする
define("API_TOKEN", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
define("MISSKEY_HOST", "example.com");
define("REQUEST_INTERVAL_SEC", 12);
# ===================== Config End ====================
function post_http_request($endpoint, $content) {
$context = array(
"http" => array(
"ignore_errors" => true,
"method" => "POST",
"header" => "Content-Type: application/json",
"content" => $content
)
);
$resp = file_get_contents("https://" . MISSKEY_HOST . $endpoint, false, stream_context_create($context));
preg_match("/HTTP\/... (\d+)/", $http_response_header[0], $mat);
$respCode = $mat[1];
if ($respCode < 200 || $respCode > 399) {
echo ">> = An HTTP error occurred = <<\n";
echo "HTTP status code: $respCode\n";
echo "$resp\n";
exit(1);
}
return json_decode($resp);
}
function del_post($id) {
$data = array(
"i" => API_TOKEN,
"noteId" => $id
);
$content = json_encode($data);
post_http_request("/api/notes/delete", $content);
$timestamp = date("Y/m/d H:i:s");
echo "deleted $id -- $timestamp\n";
}
function get_post_ids() {
$data = array(
"i" => TIMELINE_CHANNNEL,
"excludeNsfw" => false,
"limit" => 100,
"userId" => USER_ID
);
$content = json_encode($data);
$resp = post_http_request("/api/users/notes", $content);
$postIds = array_map(fn($obj) => $obj->id, $resp);
$count = count($postIds);
echo "<< = POST COUNT [ $count ] = >>\n";
return $postIds;
}
$postIds = get_post_ids();
while (count($postIds) > 0) {
foreach ($postIds as $id) {
del_post($id);
ob_flush();
flush();
sleep(REQUEST_INTERVAL_SEC);
}
$postIds = get_post_ids();
}
echo "<< = Finish notes deletion = >>\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment