diff --git a/bridges/NACSouthGermanyMediaLibraryBridge.php b/bridges/NACSouthGermanyMediaLibraryBridge.php new file mode 100644 index 00000000..fff6c554 --- /dev/null +++ b/bridges/NACSouthGermanyMediaLibraryBridge.php @@ -0,0 +1,123 @@ + 1, + 'Februar' => 2, + 'März' => 3, + 'April' => 4, + 'Mai' => 5, + 'Juni' => 6, + 'Juli' => 7, + 'August' => 8, + 'September' => 9, + 'Oktober' => 10, + 'November' => 11, + 'Dezember' => 12, + ]; + + public function getIcon() + { + return 'https://www.nak-stuttgart.de/static/themes/nak_sued/images/nak-logo.png'; + } + + private static function parseTimestamp($title) + { + if (preg_match('/([0-9]+)\.\s*([^\s]+)\s*([0-9]+)/', $title, $matches)) { + $day = $matches[1]; + $month = self::MONTHS[$matches[2]]; + $year = $matches[3]; + return $year . '-' . $month . '-' . $day; + } else { + return ''; + } + } + + private static function collectDataForSWR1($parent, $item) + { + # Find link + $sourceURI = $parent->find('a', 1)->href; + $item['enclosures'] = [self::BASE_URI . $sourceURI]; + + # Add time to timestamp + $item['timestamp'] .= ' 07:27'; + + # Find author + if (preg_match('/\((.*?)\)/', html_entity_decode($item['content']), $matches)) { + $item['author'] = $matches[1]; + } + + return $item; + } + + private static function collectDataForBayern2($parent, $item) + { + # Find link + $playerDom = getSimpleHTMLDOMCached(self::BASE_URI . $parent->find('a', 0)->href); + $sourceURI = $playerDom->find('source', 0)->src; + $item['enclosures'] = [self::BASE_URI . $sourceURI]; + + # Add time to timestamp + $item['timestamp'] .= ' 06:45'; + + return $item; + } + + private function collectDataInList($pageURI, $customizeItemCall) + { + $page = getSimpleHTMLDOM(self::BASE_URI . $pageURI); + + foreach ($page->find('div.grids') as $parent) { + # Find title + $title = $parent->find('h2', 0)->plaintext; + + # Find content + $contentBlock = $parent->find('ul.contentlist', 0); + $content = ''; + foreach ($contentBlock->find('li') as $li) { + $content .= '

' . $li->plaintext . '

'; + } + + $item = [ + 'title' => $title, + 'content' => $content, + 'timestamp' => self::parseTimestamp($title), + ]; + $this->items[] = $customizeItemCall($parent, $item); + } + } + + private function collectDataFromAllPages($rootURI, $customizeItemCall) + { + $rootPage = getSimpleHTMLDOM($rootURI); + $pages = $rootPage->find('div#tabmenu', 0); + foreach ($pages->find('a') as $page) { + self::collectDataInList($page->href, [$this, $customizeItemCall]); + } + } + + public function collectData() + { + # Collect items + self::collectDataFromAllPages(self::BAYERN2_ROOT_URI, 'collectDataForBayern2'); + self::collectDataFromAllPages(self::SWR1_ROOT_URI, 'collectDataForSWR1'); + + # Sort items by decreasing timestamp + usort($this->items, function ($a, $b) { + return strtotime($b['timestamp']) <=> strtotime($a['timestamp']); + }); + } +}