User Tools

Site Tools


lemmy-api-php

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
lemmy-api-php [2023/06/21 16:18]
neil
lemmy-api-php [2023/06/28 14:55] (current)
neil
Line 1: Line 1:
 ====== Lemmy API PHP Command Line Client ====== ====== Lemmy API PHP Command Line Client ======
-After the recent (Jun 2023) influx of Lemmy spammers, there isn't an easy way to delete the hundreds of spam accounts that showed up over night.+After the recent (June 2023) influx of Lemmy spam accounts, there isn't an easy way to delete the hundreds of spam accounts that showed up over night (before I felt forced to enable captchas and email verification etc).
  
-I built a small API command line client to basically allow me to run the [[https://​join-lemmy.org/​api/​classes/​LemmyHttp.html#​purgePerson|purgePerson]] API command.  While working on that, I discovered the API command doesn'​t actually work - something to do with the order of routes. ​ Working with [[https://​matrix.to/#/​@ksynwa:​matrix.org|@ksynwa:​matrix.org]] ​in the Lemmy Development Matrix room, he found fix and [[https://​github.com/​LemmyNet/​lemmy/​pull/​3244/​commits/​53c9efc09fe1caced310da18ac9588b3d2fd2b01|made a PR]] (so this should be working in subsequent version of Lemmy (post v0.17.4).+I built a small API command line client to basically allow me to run the [[https://​join-lemmy.org/​api/​classes/​LemmyHttp.html#​purgePerson|purgePerson]] API command ​easily ​in a loop.
  
 You can run it very simply with something like this: You can run it very simply with something like this:
 <code bash> <code bash>
 ./​lemmy-api.php purgePerson 116 ./​lemmy-api.php purgePerson 116
 +</​code>​
 +or to iterate over a number of ids:
 +<code bash>
 +for i in 145 156 162; do ./​lemmy-api.php purgePerson $i; done
 +</​code>​
 +and to work through all IDs from 170-240:
 +<code bash>
 +for i in `seq 170 240`; do ./​lemmy-api.php purgePerson $i; done 
 </​code>​ </​code>​
 +
 +You can view the code on my repo at https://​git.mckillop.org/​gitweb/?​p=lemmy-api-php or checkout directly with git using:
 +<code bash>
 +git clone https://​git.mckillop.org/​lemmy-api-php
 +</​code>​
 +
 +Made a copy of the config file and edit ''​config.php''​ to add your domain, username and password to get started.
 +
 +<code bash>
 +cd lemmy-api-php
 +cp config.example.php config.php
 +</​code>​
 +
  
 Here is some example code for a PHP command line tool that will let you purge local users. Here is some example code for a PHP command line tool that will let you purge local users.
 +
 +<code php config.php>​
 +<?php
 +$lemmy_domain = "";​ // e.g. lemmy.ml
 +$username = "";​
 +$password = "";​
 +$token_file = "​.lemmy-api-token";​ // this is where your token is cached, can be any location
 +?>
 +</​code>​
  
 <code php lemmy-api.php>​ <code php lemmy-api.php>​
 #​!/​usr/​bin/​php #​!/​usr/​bin/​php
 <?php <?php
-$lemmy_api_url = "https://​lemmy.glasgow.social/​api/​v3/";​ +require("config.php");
-$username = "​username";​ +
-$password = "​password";​ +
-$token_file = "​.lemmy-api-token";+
  
 $implemented_commands = array( $implemented_commands = array(
Line 67: Line 94:
                 echo "​Attempting to purge person $person_id...";​                 echo "​Attempting to purge person $person_id...";​
                 $data = json_decode(http_post($lemmy_api_url."​admin/​purge/​person",​ array('​auth'​ => $token, '​person_id'​=>​intval($person_id),​ '​reason'​=>​$reason)),​ true);                 $data = json_decode(http_post($lemmy_api_url."​admin/​purge/​person",​ array('​auth'​ => $token, '​person_id'​=>​intval($person_id),​ '​reason'​=>​$reason)),​ true);
 +                $status = "​ERROR";​
 +                if(!empty($data['​success'​]) and $data['​success'​] == 1)
 +                        $status = "​OK";​
 +                echo $status."​\n";​
                 break;                 break;
         case "​deletePost":​         case "​deletePost":​
Line 72: Line 103:
                 echo "​Attempting to delete post ID $post_id...";​                 echo "​Attempting to delete post ID $post_id...";​
                 $data = json_decode(http_post($lemmy_api_url."​post/​delete",​ array('​auth'​ => $token, '​post_id'​=>​intval($post_id),​ '​deleted'​=>​true)),​ true);                 $data = json_decode(http_post($lemmy_api_url."​post/​delete",​ array('​auth'​ => $token, '​post_id'​=>​intval($post_id),​ '​deleted'​=>​true)),​ true);
-                if(!empty($data['​post_view'​]) and $data['​post_view'​]['​post'​]['​deleted'​] == 1) { +                ​$status = "​ERROR";​ 
-                        ​echo "OK\n"; +                ​if(!empty($data['​post_view'​]) and $data['​post_view'​]['​post'​]['​deleted'​] == 1) 
-                ​} else { +                        ​$status = "​OK";​ 
-                        ​echo "ERROR\n"; +                echo $status."​\n";​
-                }+
                 break;                 break;
         default:         default:
Line 98: Line 128:
         return $result;         return $result;
 } }
 +
  
 ?> ?>
 </​code>​ </​code>​
  
-That should be enough to get you started using the API from the command line.  If I get time I'll put a more complete version of this with some other useful commands (like banPerson) on my repo.+That should be enough to get you started using the API from the command line.  If I get time I'll put a more complete version of this with some other useful commands (like banPerson) on my repo as I find a use for them.
  
 ===== Getting Local Lemmy Users ===== ===== Getting Local Lemmy Users =====
  
-To get the list of local users on your Lemmy instance, connect to the postgres database. ​ For me, in docker, this is something like:+To get the list of local users (person_id column) ​on your Lemmy instance, connect to the postgres database. ​ For me, in docker, this is something like:
  
 <code bash> <code bash>
Line 113: Line 144:
 select * from local_user; select * from local_user;
 </​code>​ </​code>​
 +
 +===== Pre v0.18 =====
 +While working on this, I discovered the API purgePerson command didn't actually work - something to do with the order of routes. ​ Working with [[https://​matrix.to/#/​@ksynwa:​matrix.org|@ksynwa:​matrix.org]] in the Lemmy Development Matrix room, he found a fix and [[https://​github.com/​LemmyNet/​lemmy/​pull/​3244/​commits/​53c9efc09fe1caced310da18ac9588b3d2fd2b01|made a PR]] that was accepted into v0.18 (so this should be working in subsequent version of Lemmy (after v0.17.4).
lemmy-api-php.1687360728.txt.gz · Last modified: 2023/06/21 16:18 by neil