only collect new toots if file too old

This commit is contained in:
2019-10-17 00:12:53 +02:00
parent a5924efd91
commit 62e47ea4d0
2 changed files with 51 additions and 32 deletions

View File

@@ -3,5 +3,6 @@ $config = [
'mastodon-instance' => 'https://mastodon.social', 'mastodon-instance' => 'https://mastodon.social',
'user-id' => 379833, 'user-id' => 379833,
'search-url' => 'https?://fsfe.org', // please use https?:// as schema 'search-url' => 'https?://fsfe.org', // please use https?:// as schema
'threshold' => 300 'cache_toots' => 300,
'debug' => true
]; ];

View File

@@ -7,10 +7,9 @@ $searchurl = $config['search-url'];
$search = isset($_GET['search']) ? $_GET['search'] : ''; $search = isset($_GET['search']) ? $_GET['search'] : '';
$debug_on = $config['debug']; $debug_on = $config['debug'];
/* cache files */ /* cache files */
$ctt = $config['cache_toots'];;
$dbt = "cache-toots.json"; $dbt = "cache-toots.json";
$timestamp = time();
/* Exit if search empty */ /* Exit if search empty */
if (empty($search)) { if (empty($search)) {
debug("No proper search given"); debug("No proper search given");
@@ -35,14 +34,25 @@ function write_db($db, $data) {
file_put_contents($db, $encoded, LOCK_EX); file_put_contents($db, $encoded, LOCK_EX);
} }
/* access data from file */ /* access data from file */
function read_db($db, &$data) { function read_db($db, &$data, &$cachebreak) {
global $ctt;
// if DB does not exist, create it with empty array // 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()); write_db($db, array());
$cachebreak = true;
} }
$file = file_get_contents($db, true); $file = file_get_contents($db, true);
$data = json_decode($file, true)['toots']; $data = json_decode($file, true);
debug($data['timestamp']);
// check if timestamp in cache file too old
if (empty($data['timestamp']) || ($data['timestamp'] + $ctt < time())) {
$cachebreak = true;
}
$data = $data['toots'];
} }
/* TOOT FUNCTIONS */ /* TOOT FUNCTIONS */
@@ -121,36 +131,44 @@ function tootStats($instance, $id, &$result) {
* START PROGRAM * START PROGRAM
***************/ ***************/
/* Collect all the toots */ /* check whether the cached file containing all toots is older than max. cache time */
/* get id of latest cached toot, and set as $min_id */ // this at the same time loads the cached DB, either way
read_db($dbt, $toots); $cachebreak = false;
$toots_cached = $toots; read_db($dbt, $toots, $cachebreak);
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 */ if ($cachebreak) {
// Search for toots older than the cached latest toot ID ($min_id) /* Collect all the toots */
$uptodate = false; /* get id of latest cached toot, and set as $min_id */
while ($uptodate === false) { debug("Toots cache oudated. Checking for new toots");
$toots = array_merge(collectToots($instance, $uid, $min_id, $searchurl), $toots); if (!empty($toots['0']['id'])) {
$min_id_new = $toots['0']['id']; // the latest ID of the recent search $min_id_cached = $toots['0']['id'];
$min_id = $min_id_cached;
if ($min_id_new === $min_id) {
// min_id is the latest, let's write the new DB and end this loop
$uptodate = true;
debug("Toots up-to-date. Rewrite cache DB.");
write_db($dbt, $toots);
} else { } else {
// next round looks for toots newer than the newly found ID /* if cached toots do not exist, start from oldest toot */
debug("Newer toots than in cache found. Starting another search for new toots"); $min_id = "0";
$min_id = $min_id_new; $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("Toots up-to-date. 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;
}
}
} else {
debug("Toots cache is OK");
} }
/* prepare $result array */ /* prepare $result array */