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,10 +131,15 @@ function tootStats($instance, $id, &$result) {
* START PROGRAM * START PROGRAM
***************/ ***************/
/* check whether the cached file containing all toots is older than max. cache time */
// this at the same time loads the cached DB, either way
$cachebreak = false;
read_db($dbt, $toots, $cachebreak);
if ($cachebreak) {
/* Collect all the toots */ /* Collect all the toots */
/* get id of latest cached toot, and set as $min_id */ /* get id of latest cached toot, and set as $min_id */
read_db($dbt, $toots); debug("Toots cache oudated. Checking for new toots");
$toots_cached = $toots;
if (!empty($toots['0']['id'])) { if (!empty($toots['0']['id'])) {
$min_id_cached = $toots['0']['id']; $min_id_cached = $toots['0']['id'];
$min_id = $min_id_cached; $min_id = $min_id_cached;
@@ -152,6 +167,9 @@ while ($uptodate === false) {
$min_id = $min_id_new; $min_id = $min_id_new;
} }
} }
} else {
debug("Toots cache is OK");
}
/* prepare $result array */ /* prepare $result array */
$result = ['comments' => [], 'stats' => ['reblogs' => 0, 'favs' => 0, 'replies' => 0, 'url' => '', 'root' => 0]]; $result = ['comments' => [], 'stats' => ['reblogs' => 0, 'favs' => 0, 'replies' => 0, 'url' => '', 'root' => 0]];