1
0
Files
Bridges/FunstockEvercadeBridge.php
2025-11-21 15:15:48 +01:00

116 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
class FunstockEvercadeBridge extends BridgeAbstract
{
const MAINTAINER = 'Akamaru';
const NAME = 'Funstock Evercade';
const URI = 'https://funstock.eu/collections/evercade';
const CACHE_TIMEOUT = 3600; // 1h
const DESCRIPTION = 'New Evercade products from Funstock (consoles, cartridges, accessories)';
const PARAMETERS = [
[
'limit' => [
'name' => 'Number of products',
'type' => 'number',
'defaultValue' => 20,
'required' => false,
],
],
];
public function getIcon()
{
return 'https://www.google.com/s2/favicons?domain=funstock.eu&sz=32';
}
public function collectData()
{
$limit = $this->getInput('limit') ?: 20;
$apiUrl = 'https://funstock.eu/collections/evercade/products.json?sort_by=created-descending&limit=' . $limit;
$json = getContents($apiUrl);
if (!$json) {
returnServerError('Could not fetch data from Funstock');
}
$data = json_decode($json, true);
if (!$data || !isset($data['products'])) {
returnServerError('Invalid JSON response from Funstock');
}
foreach ($data['products'] as $product) {
$item = [];
$item['title'] = $product['title'];
$item['uri'] = 'https://funstock.eu/products/' . $product['handle'];
$item['uid'] = (string) $product['id'];
// Timestamp (creation date)
if (isset($product['created_at'])) {
$item['timestamp'] = strtotime($product['created_at']);
}
// Author/Vendor
if (isset($product['vendor']) && !empty($product['vendor'])) {
$item['author'] = $product['vendor'];
}
// Categories/Tags
if (isset($product['tags']) && is_array($product['tags'])) {
$item['categories'] = $product['tags'];
}
// Price from first variant
$price = '';
if (isset($product['variants'][0]['price'])) {
$price = $product['variants'][0]['price'] . ' EUR';
}
// Product type
$productType = $product['product_type'] ?? '';
// Build content
$content = '';
// Product image
if (isset($product['images'][0]['src'])) {
$imageUrl = $product['images'][0]['src'];
$content .= '<div style="text-align: center;"><img src="' . htmlspecialchars($imageUrl) . '" alt="' . htmlspecialchars($product['title']) . '" style="max-width: 400px;"></div><br>';
$item['enclosures'][] = $imageUrl;
}
// Price and type
if ($price || $productType) {
$content .= '<p>';
if ($price) {
$content .= '<strong>Price:</strong> ' . htmlspecialchars($price);
}
if ($price && $productType) {
$content .= ' | ';
}
if ($productType) {
$content .= '<strong>Type:</strong> ' . htmlspecialchars($productType);
}
$content .= '</p>';
}
// Description (clean HTML)
if (isset($product['body_html']) && !empty($product['body_html'])) {
$description = $product['body_html'];
// Remove empty paragraphs
$description = preg_replace('/<p>\s*(&nbsp;|\s)*<\/p>/', '', $description);
$content .= '<hr>' . $description;
}
$item['content'] = $content;
$this->items[] = $item;
}
}
}