|
| 1 | +<?php |
| 2 | + |
| 3 | +class MerchantAndMillsBridge extends BridgeAbstract { |
| 4 | + const NAME = 'Merchant and Mills Blog'; |
| 5 | + const URI = 'https://merchantandmills.com'; |
| 6 | + const DESCRIPTION = 'The latest blog posts from Merchant and Mills.'; |
| 7 | + const MAINTAINER = 'caseykulm'; |
| 8 | + const CACHE_TIMEOUT = 43200; // 12h |
| 9 | + const PARAMETERS = [[ |
| 10 | + 'selected_country_id' => [ |
| 11 | + 'name' => 'Country', |
| 12 | + 'type' => 'list', |
| 13 | + 'values' => [ |
| 14 | + 'European Union' => 0, |
| 15 | + 'United Kingdom' => 1, |
| 16 | + 'United States' => 2, |
| 17 | + 'Other' => 3 |
| 18 | + ] |
| 19 | + ] |
| 20 | + ]]; |
| 21 | + |
| 22 | + private function getCountryBlogPath($countryName): string |
| 23 | + { |
| 24 | + if ($countryName === 'European Union') { |
| 25 | + return '/eu/blog'; |
| 26 | + } |
| 27 | + |
| 28 | + if ($countryName === 'United Kingdom') { |
| 29 | + return '/uk/blog'; |
| 30 | + } |
| 31 | + |
| 32 | + if ($countryName === 'United States') { |
| 33 | + return '/us/blog'; |
| 34 | + } |
| 35 | + |
| 36 | + return '/rw/blog'; |
| 37 | + } |
| 38 | + |
| 39 | + public function collectData() |
| 40 | + { |
| 41 | + $selectedCountryKey = $this->getKey('selected_country_id'); |
| 42 | + $selectedCountryBlogPath = $this->getCountryBlogPath($selectedCountryKey); |
| 43 | + $url = self::URI . $selectedCountryBlogPath; |
| 44 | + $html = getSimpleHTMLDOM($url) |
| 45 | + or returnServerError('Could not request ' . $url); |
| 46 | + |
| 47 | + foreach ($html->find('.products .post') as $post) { |
| 48 | + $this->constructContent($post); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param $post simple_html_dom |
| 54 | + */ |
| 55 | + private function constructContent(simple_html_dom $post): void |
| 56 | + { |
| 57 | + $item = []; |
| 58 | + |
| 59 | + // Extract image |
| 60 | + $image = $post->find('.post_image img', 0); |
| 61 | + if ($image) { |
| 62 | + // Resolve the relative image URL |
| 63 | + $imageUrl = $image->src; |
| 64 | + if (!str_starts_with($imageUrl, 'http')) { |
| 65 | + $imageUrl = rtrim(self::URI, '/') . '/' . ltrim($imageUrl, '/'); |
| 66 | + } |
| 67 | + $item['image'] = $imageUrl; |
| 68 | + } else { |
| 69 | + $item['image'] = ''; |
| 70 | + } |
| 71 | + |
| 72 | + // Extract title |
| 73 | + $titleLink = $post->find('.post_name a', 0); |
| 74 | + $item['title'] = $titleLink ? trim($titleLink->plaintext) : ''; |
| 75 | + $item['uri'] = $titleLink ? self::URI . $titleLink->href : ''; |
| 76 | + |
| 77 | + // Extract date and views |
| 78 | + $dateAndViews = $post->find('.post_date span'); |
| 79 | + $item['date'] = isset($dateAndViews[0]) ? trim($dateAndViews[0]->plaintext) : ''; |
| 80 | + $item['views'] = isset($dateAndViews[1]) ? trim(str_replace('Viewed:', '', $dateAndViews[1]->plaintext)) : ''; |
| 81 | + |
| 82 | + // Extract description |
| 83 | + $description = $post->find('.post_desc', 0); |
| 84 | + $item['content'] = ''; |
| 85 | + |
| 86 | + // Add the image and description to content |
| 87 | + if (!empty($item['image'])) { |
| 88 | + $item['content'] .= '<div style="text-align: center; max-width: 100%; overflow: hidden;">' |
| 89 | + . '<img src="' . $item['image'] . '" alt="' . htmlspecialchars($titleLink->plaintext ?? '') . '" ' |
| 90 | + . 'style="max-width: 100%; height: auto; max-height: 512px;" />' |
| 91 | + . '</div><br />'; |
| 92 | + } |
| 93 | + if ($description) { |
| 94 | + $item['content'] .= trim($description->plaintext); |
| 95 | + } |
| 96 | + |
| 97 | + // Add item to the feed |
| 98 | + $this->items[] = $item; |
| 99 | + } |
| 100 | +} |
0 commit comments