v0.3:
[Marketplaylister.git] / mpfuncs.php
index a35c89f..051619a 100644 (file)
@@ -1,12 +1,12 @@
 <?php\r
+\r
+    const DATE_FORM = 'm/d/Y';\r
+\r
     \r
-    function parseEpisodePage(DomNodeList $divs, int &$numDays): array {\r
+    function parseEpisodePage(DomNodeList $divs): array {\r
         $episodePage = [];\r
         foreach ($divs as $div) {\r
             if ($div->hasAttribute('class') && $div->getAttribute('class') === 'episode-music') {\r
-                if (!$numDays) {\r
-                    break;\r
-                }\r
                 $songs = [];\r
                 foreach ($div->childNodes as $row) {\r
                     $children = $row->childNodes[0]->childNodes;\r
@@ -15,7 +15,6 @@
                         'artist' => $children[1]->nodeValue\r
                     ];\r
                 }\r
-                $numDays--;\r
                 $episodePage[] = $songs;\r
             }\r
             \r
         \r
         return $episodePage;\r
         \r
+    }\r
+    \r
+    /**\r
+     * Go through the DOM elements provided and pull out the Dates of all marketplace\r
+     * pod episodes in the provided list.\r
+     * \r
+     * @param DomNodeList $headers The elements with a header tag from the DOM\r
+     * @param DateTime $lastDate The date of the most recent episode from the DB\r
+     */\r
+    function parseEpisodeDate(DomNodeList $headers, DateTime $lastDate): array {\r
+      $episodeDates = [];\r
+      foreach ($headers as $header) {\r
+        if ($header->hasAttribute('class') && $header->getAttribute('class') === 'river--hed') {\r
+          $dateStringParts = explode('/', explode(':', $header->nodeValue)[0]);\r
+          if ( strlen($dateStringParts[2]) === 2 ) {\r
+            $dateStringParts[2] = '20' . $dateStringParts[2];\r
+          }\r
+          $episodeDate = DateTime::createFromFormat(DATE_FORM, implode("/", $dateStringParts));\r
+          if ($episodeDate < $lastDate) {\r
+            break;\r
+          }\r
+          $episodeDates[] = $episodeDate;\r
+        }\r
+      }\r
+      return $episodeDates;\r
     }
\ No newline at end of file