From ce0f0ec37a45f8ef4b9b5088ad047c496ec887b5 Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Tue, 9 Jul 2024 16:42:49 +0200 Subject: [PATCH] refactor json caching --- epub.php | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/epub.php b/epub.php index 7957d6b..ec98f0c 100644 --- a/epub.php +++ b/epub.php @@ -94,6 +94,23 @@ 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 = (new DateTime('today'))->format('Ymd'); @@ -110,19 +127,7 @@ } // 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); - } - - $publication = json_decode($json); + $publication = get_json($url); $pubname = $publication->metadata->title; $epub = new TPEpubCreator(); @@ -135,8 +140,7 @@ foreach ($list as $entry) { $articleurl = str_replace('material', 'article/' . $entry->hash . '.json', $url); - $articlejson = file_get_contents($articleurl); - $article = json_decode($articlejson); + $article = get_json($articleurl); $content = '

' . $article->title . '

'; //$content .= '' . $article->abstract . '';