<?php require_once __DIR__ . '/config.php'; require_once __DIR__ . '/lib/utils.php'; header('Content-type: text/plain; charset=UTF-8'); $posts = get_posts(); $cached_posts = get_cached_posts(); foreach ($posts as $slug => $post) { if (isset($cached_posts[$slug])) { continue; } $imgsize = getimagesize($post->image->src); if ($imgsize) { $cached_posts[$slug] = array('width' => $imgsize[0], 'height' => $imgsize[1], 'html_attributes' => $imgsize[3]); } else { $cached_posts[$slug] = array('width' => NULL, 'height' => NULL, 'html_attributes' => ''); } echo "Fetched dimensions of “{$post->title}”.\n"; } if (write_cached_posts($cached_posts)) { echo "Cache successfully written."; } else { echo "Can not write the cache file."; }
function get_posts() { $raw_posts = file_get_contents(__DIR__ . '/../posts.txt'); $raw_posts = trim(str_replace("\n\n", "\n", str_replace("\n\r", "\n", $raw_posts))); $posts_lines = explode("\n", $raw_posts); $posts = array(); foreach ($posts_lines as $key => $value) { // Title if ($key % 2 == 0) { $post_key = slugify($value); $posts[$post_key] = array('title' => $value, 'slug' => $post_key); // URL } else { $post_key = slugify($posts_lines[$key - 1]); $doc_url = $value; $archslug = get_url_data($doc_url); $xml_data_url = "https://archive.org/download" . $archslug . $archslug . "_meta.xml"; $xml_files_url = "https://archive.org/download" . $archslug . $archslug . "_files.xml"; // Uncomment for debug //$xml_data_url = "http://localhost/PERSO/archonaut-WBM/archonaut/debug/An_Animation_Of_Me_lol_meta.xml"; //$xml_files_url = "http://localhost/PERSO/archonaut-WBM/archonaut/debug/An_Animation_Of_Me_lol_files.xml"; $xml_data = xml_eye($xml_data_url, "data"); $xml_files = xml_eye($xml_files_url, "files"); $posts[$post_key]['archurl'] = (object) array('title' => $xml_data[0], 'authorname' => $xml_data[1], 'creation_date' => $xml_data[4], 'main_file_url' => "https://archive.org/download/" . $archslug . "/" . $xml_files, 'doc_url' => $doc_url, 'data_url' => $xml_data_url, 'files_url' => $xml_files_url); $posts[$post_key] = (object) $posts[$post_key]; } } // Cached dimensions $cached_posts = get_cached_posts(); array_walk($posts, function (&$post, $slug) use($cached_posts) { if (isset($cached_posts[$slug])) { $post->image->width = $cached_posts[$slug]['width']; $post->image->height = $cached_posts[$slug]['height']; $post->image->html_attributes = $cached_posts[$slug]['html_attributes']; } }); return $posts; }