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,19 +5,23 @@ $instance = $config['mastodon-instance'];
$uid = $config['user-id'];
$searchurl = $config['search-url'];
$search = isset($_GET['search']) ? $_GET['search'] : '';
$debug_on = true;
/* cache files */
$dbt = "cache-toots.json";
/* Exit if search empty */
if (empty($search)) {
out("No proper search given");
debug("No proper search given");
die();
}
/* MISC FUNCTIONS */
function out($data) {
function debug($data) {
global $debug_on;
if ($debug_on === true) {
error_log("[getcomments.php] " . print_r($data, TRUE));
}
}
/* CACHE FUNCTIONS */
/* write data to file */
@@ -28,52 +32,28 @@ function write_db($db, $data) {
}
/* access data from file */
function read_db($db, &$data) {
// if DB does not exist, create it with empty array
if (! file_exists($db)) {
touch($db);
write_db($db, array());
}
$file = file_get_contents($db, true);
$data = json_decode($file, true);
}
/* 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");
$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);
}
/* Collect all the toots */
$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) {
debug("Searching for $searchurl in $id");
$raw = file_get_contents("$instance/api/v1/statuses/$id");
$json = json_decode($raw, true);
@@ -86,21 +66,43 @@ if ($min_id !== $min_id_cached) {
}
}
$toots = array();
foreach ($ids as $id) {
$toots[] = array('id' => $id, 'url' => analyzeToot($instance, $id, $searchurl));
/* 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 {
/* 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 {
out("No new toots found");
// 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 */
// if multiple exist, take the oldest one (highest array position)
$id = array_keys(array_column($toots, 'url'), strtolower($search));
if (empty($id)) {
out("Blog URL \"$search\" has not been found");
debug("Blog URL \"$search\" has not been found");
die();
}
$id = $toots[end($id)]['id'];
@@ -143,7 +145,7 @@ function filterStats($stats) {
}
function tootStats($instance, $id, &$result) {
out("Checking ID $id");
debug("Checking ID $id");
$raw = file_get_contents("$instance/api/v1/statuses/$id");
$json = json_decode($raw, true);
$newStats = filterStats($json);