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