more clever check for only new toots

This commit is contained in:
2019-10-16 22:06:59 +02:00
parent 467122b957
commit fa19a5b6fc

View File

@@ -5,18 +5,22 @@ $instance = $config['mastodon-instance'];
$uid = $config['user-id']; $uid = $config['user-id'];
$searchurl = $config['search-url']; $searchurl = $config['search-url'];
$search = isset($_GET['search']) ? $_GET['search'] : ''; $search = isset($_GET['search']) ? $_GET['search'] : '';
$debug_on = true;
/* cache files */ /* cache files */
$dbt = "cache-toots.json"; $dbt = "cache-toots.json";
/* Exit if search empty */ /* Exit if search empty */
if (empty($search)) { if (empty($search)) {
out("No proper search given"); debug("No proper search given");
die(); die();
} }
/* MISC FUNCTIONS */ /* MISC FUNCTIONS */
function out($data) { function debug($data) {
global $debug_on;
if ($debug_on === true) {
error_log("[getcomments.php] " . print_r($data, TRUE)); error_log("[getcomments.php] " . print_r($data, TRUE));
}
} }
/* CACHE FUNCTIONS */ /* CACHE FUNCTIONS */
@@ -28,52 +32,28 @@ function write_db($db, $data) {
} }
/* access data from file */ /* access data from file */
function read_db($db, &$data) { function read_db($db, &$data) {
// if DB does not exist, create it with empty array
if (! file_exists($db)) { if (! file_exists($db)) {
touch($db); touch($db);
write_db($db, array());
} }
$file = file_get_contents($db, true); $file = file_get_contents($db, true);
$data = json_decode($file, true); $data = json_decode($file, true);
} }
/* TOOT FUNCTIONS */ /* TOOT FUNCTIONS */
function collectToots($instance, $uid, $min_id) { function collectToots($instance, $uid, $min_id, $searchurl) {
$raw = file_get_contents("$instance/api/v1/accounts/$uid/statuses?exclude_reblogs=true&exclude_replies=true&limit=50&min_id=$min_id"); $raw = file_get_contents("$instance/api/v1/accounts/$uid/statuses?exclude_reblogs=true&exclude_replies=true&limit=50&min_id=$min_id");
$json = json_decode($raw, true); $json_complete = json_decode($raw, true);
$json = array();
foreach ($json_complete as $toot) {
$json[] = array('id' => $toot['id'], 'date' => $toot['created_at'] ,'url' => analyzeToot($instance, $toot['id'], $searchurl));
}
return($json); return($json);
} }
/* Find out if a toot contains the searched URL */
function analyzeToot($instance, $id, $searchurl) {
/* Collect all the toots */ debug("Searching for $searchurl in $id");
$toots = array();
/* get id of latest cached toot, and set as $min_id */
read_db($dbt, $toots);
if (!empty($toots['0']['id'])) {
$min_id_cached = $toots['0']['id'];
$min_id = $min_id_cached;
} else {
/* if cached toots do not exist, start from oldest toot */
$min_id = "0";
$min_id_cached = "0";
}
/* test whether there are new toots available */
$min_id_new = "1";
while ($min_id_new !== $min_id) {
$min_id_new = $min_id;
$toots = array_merge(collectToots($instance, $uid, $min_id), $toots);
$min_id = $toots['0']['id'];
}
/* if newer toot has been found, find new URLs */
// TODO: only look up newly found toots
if ($min_id !== $min_id_cached) {
out("New toots found");
$ids = array_column($toots, 'id');
/* Find out if a toot contains the searched URL */
function analyzeToot($instance, $id, $searchurl) {
$raw = file_get_contents("$instance/api/v1/statuses/$id"); $raw = file_get_contents("$instance/api/v1/statuses/$id");
$json = json_decode($raw, true); $json = json_decode($raw, true);
@@ -84,23 +64,45 @@ if ($min_id !== $min_id_cached) {
} else { } else {
return(""); return("");
} }
} }
$toots = array();
foreach ($ids as $id) {
$toots[] = array('id' => $id, 'url' => analyzeToot($instance, $id, $searchurl));
}
write_db($dbt, $toots); /* Collect all the toots */
/* get id of latest cached toot, and set as $min_id */
read_db($dbt, $toots);
$toots_cached = $toots;
if (!empty($toots['0']['id'])) {
$min_id_cached = $toots['0']['id'];
$min_id = $min_id_cached;
} else { } else {
out("No new toots found"); /* if cached toots do not exist, start from oldest toot */
$min_id = "0";
$min_id_cached = "0";
}
/* test whether there are new toots available */
// Search for toots older than the cached latest toot ID ($min_id)
$uptodate = false;
while ($uptodate === false) {
$toots = array_merge(collectToots($instance, $uid, $min_id, $searchurl), $toots);
$min_id_new = $toots['0']['id']; // the latest ID of the recent search
if ($min_id_new === $min_id) {
// min_id is the latest, let's write the new DB and end this loop
$uptodate = true;
debug("Rewrite cache DB.");
write_db($dbt, $toots);
} else {
// next round looks for toots newer than the newly found ID
debug("Newer toots than in cache found. Starting another search for new toots");
$min_id = $min_id_new;
}
} }
/* check if URL from $search exists in $toots */ /* check if URL from $search exists in $toots */
// if multiple exist, take the oldest one (highest array position) // if multiple exist, take the oldest one (highest array position)
$id = array_keys(array_column($toots, 'url'), strtolower($search)); $id = array_keys(array_column($toots, 'url'), strtolower($search));
if (empty($id)) { if (empty($id)) {
out("Blog URL \"$search\" has not been found"); debug("Blog URL \"$search\" has not been found");
die(); die();
} }
$id = $toots[end($id)]['id']; $id = $toots[end($id)]['id'];
@@ -143,7 +145,7 @@ function filterStats($stats) {
} }
function tootStats($instance, $id, &$result) { function tootStats($instance, $id, &$result) {
out("Checking ID $id"); debug("Checking ID $id");
$raw = file_get_contents("$instance/api/v1/statuses/$id"); $raw = file_get_contents("$instance/api/v1/statuses/$id");
$json = json_decode($raw, true); $json = json_decode($raw, true);
$newStats = filterStats($json); $newStats = filterStats($json);