more clever check for only new toots
This commit is contained in:
@@ -5,18 +5,22 @@ $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) {
|
||||
error_log("[getcomments.php] " . print_r($data, TRUE));
|
||||
function debug($data) {
|
||||
global $debug_on;
|
||||
if ($debug_on === true) {
|
||||
error_log("[getcomments.php] " . print_r($data, TRUE));
|
||||
}
|
||||
}
|
||||
|
||||
/* CACHE FUNCTIONS */
|
||||
@@ -28,25 +32,45 @@ 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);
|
||||
}
|
||||
/* 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);
|
||||
|
||||
preg_match("|$searchurl.+?(?=\")|i", $json['content'], $matches);
|
||||
|
||||
if(!empty($matches)) {
|
||||
return(strtolower($matches[0]));
|
||||
} else {
|
||||
return("");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Collect all the toots */
|
||||
$toots = array();
|
||||
/* 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;
|
||||
@@ -56,51 +80,29 @@ if (!empty($toots['0']['id'])) {
|
||||
$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");
|
||||
$json = json_decode($raw, true);
|
||||
|
||||
preg_match("|$searchurl.+?(?=\")|i", $json['content'], $matches);
|
||||
|
||||
if(!empty($matches)) {
|
||||
return(strtolower($matches[0]));
|
||||
} else {
|
||||
return("");
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
||||
$toots = array();
|
||||
foreach ($ids as $id) {
|
||||
$toots[] = array('id' => $id, 'url' => analyzeToot($instance, $id, $searchurl));
|
||||
}
|
||||
|
||||
write_db($dbt, $toots);
|
||||
} else {
|
||||
out("No new toots found");
|
||||
}
|
||||
|
||||
/* 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);
|
||||
|
||||
Reference in New Issue
Block a user