/** * @param Carbon $date * @param string $data * @return ProgrammingCollection * @throws \Exception */ public static function parseDataToProgrammingCollection(Carbon $date, $data) { $startPos = 0; $res = new ProgrammingCollection(); do { $findStart = '<div class="tabla_topic">'; $findEnd = "</div> \n</div>"; $startPos = strpos($data, $findStart, $startPos); if ($startPos === false) { // done break; } $endPos = strpos($data, $findEnd, $startPos); if ($endPos === false) { throw new \Exception("parse error: didn't find end pos"); } $chunk = substr($data, $startPos, $endPos - $startPos); $programming = self::parseChannelChunk($date, $chunk); $res->addProgramming($programming); $startPos++; } while (1); return $res; }
/** * @param ProgrammingCollection $collection * @param string $channelName * @param bool $showFullProgramming show all programs for the day * @param int $maxLength * @return string */ public static function printChannel(ProgrammingCollection $collection, $channelName, $showFullProgramming = false, $maxLength = 100) { $consoleColor = new ConsoleColor(); $res = ''; $programming = $collection->getProgrammingsByChannel($channelName); $res .= $consoleColor->apply('blue', '==> ') . $consoleColor->apply('white', $programming->getChannelName() . $consoleColor->apply('blue', ' <==') . "\n"); $now = Carbon::now(); $nextTwoHours = Carbon::now()->addHours(2); foreach ($programming->getEvents() as $event) { $color = null; if ($event->starts_at->lte($now) && $event->ends_at->gte($now)) { $color = 'green'; } else { if (!$showFullProgramming && $event->starts_at->gte($now) && $event->starts_at->lte($nextTwoHours)) { $color = 'default'; } else { if ($showFullProgramming) { $color = 'default'; } } } if (!$color) { continue; } $title = $event->render(); if (mb_strlen($title) > $maxLength) { $title = mb_substr($title, 0, $maxLength - 2) . '..'; } $res .= $consoleColor->apply($color, $title); $res .= "\n"; } return $res; }