|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +class DjMagDotComBridge extends BridgeAbstract |
| 6 | +{ |
| 7 | + const NAME = 'DJMag.com News'; |
| 8 | + const URI = 'https://www.djmag.com/'; |
| 9 | + const DESCRIPTION = 'News from DJMag.com'; |
| 10 | + const MAINTAINER = 'skrollme'; |
| 11 | + const CACHE_TIMEOUT = 60 * 60; // 1 hours |
| 12 | + const PARAMETERS = [[ |
| 13 | + 'limit' => [ |
| 14 | + 'name' => 'Limit', |
| 15 | + 'type' => 'number', |
| 16 | + 'title' => 'The number of news to get (max: 20)', |
| 17 | + 'defaultValue' => 10 |
| 18 | + ] |
| 19 | + ] |
| 20 | + ]; |
| 21 | + |
| 22 | + public function getIcon() |
| 23 | + { |
| 24 | + return 'https://djmag.com/sites/default/files/favicons/favicon-32x32.png?v=2024'; |
| 25 | + } |
| 26 | + |
| 27 | + public function getURI() |
| 28 | + { |
| 29 | + return self::URI . 'news'; |
| 30 | + } |
| 31 | + |
| 32 | + private function parseDateString($dateString) |
| 33 | + { |
| 34 | + // Expect formats like "30 December 2025, 12:10" |
| 35 | + $dateString = trim($dateString); |
| 36 | + |
| 37 | + // Try a strict parse first: day (no leading zero) monthname year, 24h:minute |
| 38 | + $dt = DateTime::createFromFormat('j F Y, H:i', $dateString); |
| 39 | + if ($dt instanceof DateTime) { |
| 40 | + return $dt->getTimestamp(); |
| 41 | + } |
| 42 | + |
| 43 | + // Try with leading zero day |
| 44 | + $dt = DateTime::createFromFormat('d F Y, H:i', $dateString); |
| 45 | + if ($dt instanceof DateTime) { |
| 46 | + return $dt->getTimestamp(); |
| 47 | + } |
| 48 | + |
| 49 | + // Fallback to strtotime which handles many human-readable formats |
| 50 | + $ts = strtotime($dateString); |
| 51 | + if ($ts !== false) { |
| 52 | + return $ts; |
| 53 | + } |
| 54 | + |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + private function fetchArticleDetails($uri, $image, $title) |
| 59 | + { |
| 60 | + $itemHtml = getSimpleHTMLDOM($uri); |
| 61 | + |
| 62 | + $content = '<h2>' . $itemHtml->find('article div.article--standfirst p', 0)->plaintext . '</h2><br>'; |
| 63 | + $content .= '<img src="' . $image . '" alt="' . htmlentities($title) . '" /><p/>'; |
| 64 | + $content .= '<p>' . trim(nl2br(htmlentities($itemHtml->find('article div.content-column-wrap-oh > div > div.field--name-field-content > div', 0)->plaintext))) . '</p>'; |
| 65 | + |
| 66 | + $metaFields = $itemHtml->find('article div.pane-author-info', 1); |
| 67 | + // contains a timestamp in a format like 30 December 2025, 12:10 |
| 68 | + $rawTimestamp = $metaFields->find('div', 1)->plaintext; |
| 69 | + $timestamp = $this->parseDateString($rawTimestamp); |
| 70 | + |
| 71 | + $author = trim($metaFields->find('div', 0)->plaintext); |
| 72 | + |
| 73 | + return [$timestamp, $content, $author]; |
| 74 | + } |
| 75 | + |
| 76 | + public function collectData() |
| 77 | + { |
| 78 | + $limit = max(0, min($this->getInput('limit'), 20)); |
| 79 | + $url = $this->getUri(); |
| 80 | + |
| 81 | + $mainHtml = getSimpleHTMLDOM($url); |
| 82 | + |
| 83 | + // fetch first/latest news item separately as it is structured differently due to being featured |
| 84 | + $firstNewsItemHtml = $mainHtml->find('div.attachment-before div.view-content', 0); |
| 85 | + $title = trim($firstNewsItemHtml->find('h1 > a', 0)->plaintext); |
| 86 | + $uri = self::URI . $firstNewsItemHtml->find('h1 > a', 0)->href; |
| 87 | + $image = rtrim(self::URI, '/') . $firstNewsItemHtml->find('.teaser-media source', 0)->srcset; |
| 88 | + |
| 89 | + list($timestamp, $content, $author) = $this->fetchArticleDetails($uri, $image, $title); |
| 90 | + |
| 91 | + $this->items[] = [ |
| 92 | + 'title' => $title, |
| 93 | + 'uri' => $uri, |
| 94 | + 'uid' => sha1($uri), |
| 95 | + 'thumbnail' => $image, |
| 96 | + 'content' => $content, |
| 97 | + 'timestamp' => $timestamp, |
| 98 | + 'author' => $author, |
| 99 | + 'categories' => ['NEWS'], |
| 100 | + 'enclosures' => [$image], |
| 101 | + ]; |
| 102 | + |
| 103 | + // continue with the rest of the news items |
| 104 | + foreach ($mainHtml->find('div#views-bootstrap-listing-news-page > div.row article') as $newsItem) { |
| 105 | + if ($limit-- <= 0) { |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + $title = trim($newsItem->find('h1 > a', 0)->plaintext); |
| 110 | + $uri = self::URI . $newsItem->find('a', 0)->href; |
| 111 | + $image = rtrim(self::URI, '/') . $newsItem->find('source', 0)->srcset; |
| 112 | + |
| 113 | + list($timestamp, $content, $author) = $this->fetchArticleDetails($uri, $image, $title); |
| 114 | + |
| 115 | + $this->items[] = [ |
| 116 | + 'title' => $title, |
| 117 | + 'uri' => $uri, |
| 118 | + 'uid' => sha1($uri), |
| 119 | + 'thumbnail' => $image, |
| 120 | + 'content' => $content, |
| 121 | + 'timestamp' => $timestamp, |
| 122 | + 'author' => $author, |
| 123 | + 'categories' => ['NEWS'], |
| 124 | + 'enclosures' => [$image], |
| 125 | + ]; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments