| 1 | <?php\r |
| 2 | \r |
| 3 | const DATE_FORM = 'mdY';\r |
| 4 | \r |
| 5 | \r |
| 6 | function parseEpisodePage(DomNodeList $divs): array {\r |
| 7 | $episodePage = [];\r |
| 8 | foreach ($divs as $div) {\r |
| 9 | if ($div->hasAttribute('class') && $div->getAttribute('class') === 'episode-music') {\r |
| 10 | $songs = [];\r |
| 11 | foreach ($div->childNodes as $row) {\r |
| 12 | $children = $row->childNodes[0]->childNodes;\r |
| 13 | $songs[] = [\r |
| 14 | 'title' => $children[0]->nodeValue,\r |
| 15 | 'artist' => $children[1]->nodeValue\r |
| 16 | ];\r |
| 17 | }\r |
| 18 | $episodePage[] = $songs;\r |
| 19 | }\r |
| 20 | \r |
| 21 | }\r |
| 22 | \r |
| 23 | return $episodePage;\r |
| 24 | \r |
| 25 | }\r |
| 26 | \r |
| 27 | /**\r |
| 28 | * Go through the DOM elements provided and pull out the Dates of all marketplace\r |
| 29 | * pod episodes in the provided list.\r |
| 30 | * \r |
| 31 | * @param DomNodeList $headers The elements with a header tag from the DOM\r |
| 32 | * @param DateTime $lastDate The date of the most recent episode from the DB\r |
| 33 | */\r |
| 34 | function parseEpisodeDate(DomNodeList $headers, DateTime $lastDate): array {\r |
| 35 | $episodeDates = [];\r |
| 36 | foreach ($headers as $header) {\r |
| 37 | if ($header->hasAttribute('class') && $header->getAttribute('class') === 'river--hed') {\r |
| 38 | $episodeAnchorHref = $header->firstChild->getAttribute('href');\r |
| 39 | $dateString = explode('/', $episodeAnchorHref)[3];\r |
| 40 | $episodeDate = DateTime::createFromFormat(DATE_FORM, $dateString);\r |
| 41 | if ($episodeDate < $lastDate) {\r |
| 42 | break;\r |
| 43 | }\r |
| 44 | $episodeDates[] = $episodeDate;\r |
| 45 | }\r |
| 46 | }\r |
| 47 | return $episodeDates;\r |
| 48 | }\r |