Add embed cache

This commit is contained in:
Jay Trees 2022-01-27 12:53:39 +01:00
parent 3d3ae77f8b
commit 82402aca98
5 changed files with 82 additions and 6 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
/src/cache/*
/vendor/bin
/vendor/composer
/vendor/squizlabs/php_codesniffer

View file

@ -7,6 +7,7 @@
*/
define('VERSION', '0.3.0');
define('ROOT', __DIR__);
/**
* Include

View file

@ -0,0 +1,64 @@
<?php
/**
* cache.php
*
* Cache embed requests.
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
namespace wishthis;
class EmbedCache
{
private $directory = ROOT . '/src/cache';
public function __construct()
{
}
public function get(string $url): mixed
{
$info = null;
$identifier = md5($url);
$filepath = $this->directory . '/' . $identifier;
if (file_exists($filepath)) {
$info = json_decode(file_get_contents($filepath));
} else {
/**
* @link https://github.com/oscarotero/Embed
*/
$embed = new \Embed\Embed();
$info = $embed->get($url);
$info_simplified = new \stdClass();
$info_simplified->authorName = (string) $info->authorName;
$info_simplified->authorUrl = (string) $info->authorUrl;
$info_simplified->cms = (string) $info->cms;
$info_simplified->code = (string) $info->code;
$info_simplified->description = (string) $info->description;
$info_simplified->favicon = (string) $info->favicon;
$info_simplified->feeds = (array) $info->feeds;
$info_simplified->icon = (string) $info->icon;
$info_simplified->image = (string) $info->image;
$info_simplified->keywords = (array) $info->keywords;
$info_simplified->language = (string) $info->language;
$info_simplified->languages = (array) $info->languages;
$info_simplified->license = (string) $info->license;
$info_simplified->providerName = (string) $info->providerName;
$info_simplified->providerUrl = (string) $info->providerUrl;
$info_simplified->publishedTime = $info->publishedTime ? $info->publishedTime->format('d.m.Y') : '';
$info_simplified->redirect = (string) $info->redirect;
$info_simplified->title = (string) $info->title;
$info_simplified->url = (string) $info->url;
$info = $info_simplified;
file_put_contents($filepath, json_encode($info));
}
return $info;
}
}

View file

@ -79,11 +79,8 @@ class Wishlist
<div class="ui three column stackable grid wishlist-cards">
<?php foreach ($products as $product) {
/**
* @link https://github.com/oscarotero/Embed
*/
$embed = new \Embed\Embed();
$info = $embed->get($product['url']);
$cache = new EmbedCache();
$info = $cache->get($product['url']);
?>
<div class="column">
<div class="ui fluid card" data-id="<?= $product['id'] ?>">
@ -124,7 +121,7 @@ class Wishlist
<div class="extra content">
<?php if ($info->publishedTime) { ?>
<span class="right floated">
<?= $info->publishedTime->format('d.m.Y') ?>
<?= $info->publishedTime ?>
</span>
<?php } ?>
<?php if ($info->providerName) { ?>

View file

@ -63,6 +63,18 @@ switch ($step) {
break;
case 2:
/**
* Cache
*/
$cacheDirectory = 'src/cache';
if (!file_exists($cacheDirectory)) {
mkdir($cacheDirectory);
}
/**
* Config
*/
$configDirectory = 'src/config';
$configPath = $configDirectory . '/config.php';
$configSamplePath = $configDirectory . '/config-sample.php';