This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
lemmy-api-php [2023/06/26 22:45] admin |
lemmy-api-php [2023/06/28 13:55] (current) neil |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Lemmy API PHP Command Line Client ====== | ====== Lemmy API PHP Command Line Client ====== | ||
| - | 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. | + | 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 easily in a loop. | 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. | ||
| Line 11: | Line 11: | ||
| <code bash> | <code bash> | ||
| for i in 145 156 162; do ./lemmy-api.php purgePerson $i; done | 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 71: | 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 76: | 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 102: | 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> | ||