Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MerchantAndMillsBridge] Add new bridge #4373

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions bridges/MerchantAndMillsBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

class MerchantAndMillsBridge extends BridgeAbstract {
const NAME = 'Merchant and Mills Blog';
const URI = 'https://merchantandmills.com';
const DESCRIPTION = 'The latest blog posts from Merchant and Mills.';
const MAINTAINER = 'caseykulm';
const CACHE_TIMEOUT = 43200; // 12h
const PARAMETERS = [[
'selected_country_id' => [
'name' => 'Country',
'type' => 'list',
'values' => [
'European Union' => 0,
'United Kingdom' => 1,
'United States' => 2,
'Other' => 3
]
]
]];

private function getCountryBlogPath($countryName): string
{
if ($countryName === 'European Union') {
return '/eu/blog';
}

if ($countryName === 'United Kingdom') {
return '/uk/blog';
}

if ($countryName === 'United States') {
return '/us/blog';
}

return '/rw/blog';
}

public function collectData()
{
$selectedCountryKey = $this->getKey('selected_country_id');
$selectedCountryBlogPath = $this->getCountryBlogPath($selectedCountryKey);
$url = self::URI . $selectedCountryBlogPath;
$html = getSimpleHTMLDOM($url)
or returnServerError('Could not request ' . $url);

foreach ($html->find('.products .post') as $post) {
$item = [];

// Extract image
$image = $post->find('.post_image img', 0);
if ($image) {
// Resolve the relative image URL
$imageUrl = $image->src;
if (!str_starts_with($imageUrl, 'http')) {
$imageUrl = rtrim(self::URI, '/') . '/' . ltrim($imageUrl, '/');
}
$item['image'] = $imageUrl;
} else {
$item['image'] = '';
}

// Extract title
$titleLink = $post->find('.post_name a', 0);
$item['title'] = $titleLink ? trim($titleLink->plaintext) : '';
$item['uri'] = $titleLink ? self::URI . $titleLink->href : '';

// Extract date and views
$dateAndViews = $post->find('.post_date span');
$item['date'] = isset($dateAndViews[0]) ? trim($dateAndViews[0]->plaintext) : '';
$item['views'] = isset($dateAndViews[1]) ? trim(str_replace('Viewed:', '', $dateAndViews[1]->plaintext)) : '';

// Extract description
$description = $post->find('.post_desc', 0);
$item['content'] = '';

// Add the image and description to content
if (!empty($item['image'])) {
$item['content'] .= '<div style="text-align: center; max-width: 100%; overflow: hidden;">'
. '<img src="' . $item['image'] . '" alt="' . htmlspecialchars($titleLink->plaintext ?? '') . '" '
. 'style="max-width: 100%; height: auto; max-height: 700px;" />'
. '</div><br />';
}
if ($description) {
$item['content'] .= trim($description->plaintext);
}

// Add item to the feed
$this->items[] = $item;
}
}
}
Loading