refactor json caching

This commit is contained in:
quenousimporte 2024-07-09 16:42:49 +02:00
parent df3c954ab4
commit ce0f0ec37a
1 changed files with 19 additions and 15 deletions

View File

@ -94,6 +94,23 @@
return $innerHTML; return $innerHTML;
} }
function get_json($url)
{
// cache json in case url expires
$tempjsonpath = 'temp/' . hash('md5', $url) . '.json';
$json = '';
if (file_exists($tempjsonpath))
{
$json = file_get_contents($tempjsonpath);
}
else
{
$json = file_get_contents($url);
file_put_contents($tempjsonpath, $json);
}
return json_decode($json);
}
date_default_timezone_set('Europe/Paris'); date_default_timezone_set('Europe/Paris');
$date = (new DateTime('today'))->format('Ymd'); $date = (new DateTime('today'))->format('Ymd');
@ -110,19 +127,7 @@
} }
// cache json in case url expires // cache json in case url expires
$tempjsonpath = 'temp/' . hash('md5', $url) . '.json'; $publication = get_json($url);
$json = '';
if (file_exists($tempjsonpath))
{
$json = file_get_contents($tempjsonpath);
}
else
{
$json = file_get_contents($url);
file_put_contents($tempjsonpath, $json);
}
$publication = json_decode($json);
$pubname = $publication->metadata->title; $pubname = $publication->metadata->title;
$epub = new TPEpubCreator(); $epub = new TPEpubCreator();
@ -135,8 +140,7 @@
foreach ($list as $entry) foreach ($list as $entry)
{ {
$articleurl = str_replace('material', 'article/' . $entry->hash . '.json', $url); $articleurl = str_replace('material', 'article/' . $entry->hash . '.json', $url);
$articlejson = file_get_contents($articleurl); $article = get_json($articleurl);
$article = json_decode($articlejson);
$content = '<h1>' . $article->title . '</h1>'; $content = '<h1>' . $article->title . '</h1>';
//$content .= '<b>' . $article->abstract . '</b>'; //$content .= '<b>' . $article->abstract . '</b>';