public static function parse($chapters_string) { // remove UTF8 BOM if it exists $chapters_string = str_replace("", "", $chapters_string); if (!is_string($chapters_string)) { return NULL; } if (!($chapters_string = trim($chapters_string))) { return NULL; } if (!($xml = self::get_simplexml($chapters_string))) { return NULL; } $xml->registerXPathNamespace('psc', 'http://podlove.org/simple-chapters'); if (!($chapters_xpath = $xml->xpath("//psc:chapter"))) { return NULL; } $chapters = new Chapters(); foreach ($chapters_xpath as $chapter) { $simplexml_attributes = (array) $chapter->attributes(); $attributes = $simplexml_attributes['@attributes']; $chapters->addChapter(new Chapter(NormalPlayTime\Parser::parse($attributes['start']), isset($attributes['title']) ? $attributes['title'] : '', isset($attributes['href']) ? $attributes['href'] : '', isset($attributes['image']) ? $attributes['image'] : '')); } return $chapters; }
public static function parse($chapters_string) { // remove UTF8 BOM if it exists $chapters_string = str_replace("", "", $chapters_string); $chapters_string = trim($chapters_string); if (!strlen($chapters_string)) { return NULL; } $chapters = new Chapters(); $invalid_lines = 0; foreach (preg_split("/((\r?\n)|(\r\n?))/", $chapters_string) as $line) { $valid = preg_match('/^([\\d.:]+)(.*)$/', trim($line), $matches); if (!$valid) { $invalid_lines++; continue; } $time_string = $matches[1]; $title = $matches[2]; $timestamp_milliseconds = NormalPlayTime\Parser::parse($time_string); if ($timestamp_milliseconds === false) { continue; } $link = ''; $title = preg_replace_callback('/\\s?<[^>]+>\\s?/', function ($matches) use(&$link) { $link = trim($matches[0], ' < >'); return ' '; }, $title); $chapters->addChapter(new Chapter($timestamp_milliseconds, trim($title), $link)); } return $invalid_lines <= count($chapters) ? $chapters : NULL; }
public function testAllowLink() { $result = Mp4chaps::parse("3.45 Intro <http://example.com>"); $chapters = new Chapters(); $chapters->addChapter(new Chapter(3450, 'Intro', 'http://example.com')); $this->assertEquals($chapters, $result); }
public function testPrinter() { $expected_print = trim("\n00:00:01.234 Intro <http://example.com>\n00:12:34.000 About us\n01:02:03.000 Later\n"); $chapters = new Chapters(); $chapters->addChapter(new Chapter(1234, 'Intro', 'http://example.com')); $chapters->addChapter(new Chapter(754000, 'About us')); $chapters->addChapter(new Chapter(3723000, 'Later')); $chapters->setPrinter(new Printer\Mp4chaps()); $this->assertEquals($expected_print, (string) $chapters); }
public function testSpecialCharsInTitle() { $expected_print = trim(' <psc:chapters xmlns:psc="http://podlove.org/simple-chapters" version="1.2"> <psc:chapter start="00:00:01.234" title=""Intro""/> <psc:chapter start="00:00:01.235" title="Törich\'"/> </psc:chapters>'); $chapters = new Chapters(); $chapters->addChapter(new Chapter(1234, '"Intro"')); $chapters->addChapter(new Chapter(1235, 'Törich\'')); $chapters->setPrinter(new Printer\PSC()); $this->assertEquals($expected_print, (string) $chapters); }
public static function parse($chapters_string) { // remove UTF8 BOM if it exists $chapters_string = str_replace("", "", $chapters_string); $chapters = new Chapters(); $json = json_decode(trim($chapters_string)); if (!$json) { return $chapters; } foreach ($json as $chapter) { $chapters->addChapter(new Chapter(NormalPlayTime\Parser::parse($chapter->start), $chapter->title, $chapter->href, $chapter->image)); } return $chapters; }
public function testPrinter() { $expected_print = json_encode(json_decode('[ { "start": "00:00:01.234", "title": "Intro", "href": "http://example.com", "image": "" }, { "start": "00:12:34.000", "title": "About us", "href": "", "image": "" }, { "start": "01:02:03.000", "title": "Later", "href": "", "image": "http://example.com/foo.jpg" } ]')); $chapters = new Chapters(); $chapters->addChapter(new Chapter(1234, 'Intro', 'http://example.com')); $chapters->addChapter(new Chapter(754000, 'About us')); $chapters->addChapter(new Chapter(3723000, 'Later', '', 'http://example.com/foo.jpg')); $chapters->setPrinter(new Printer\JSON()); $this->assertEquals($expected_print, (string) $chapters); }
public function do_print(\Podlove\Chapters\Chapters $chapters) { $xml = new \SimpleXMLElement('<psc:chapters version="1.2" xmlns:psc="http://podlove.org/simple-chapters" />'); $xml = array_reduce($chapters->toArray(), function ($xml, $chapter) { $child = $xml->addChild('psc:chapter'); $child->addAttribute('start', $chapter->get_time()); $child->addAttribute('title', $chapter->get_title()); if ($chapter->get_link()) { $child->addAttribute('href', $chapter->get_link()); } if ($chapter->get_image()) { $child->addAttribute('image', $chapter->get_image()); } return $xml; }, $xml); $xml_string = $xml->asXML(); $xml_string = $this->format_xml($xml_string); $xml_string = $this->remove_xml_header($xml_string); return $xml_string; }
public function testMultipleChapter() { $chapters = new Chapters(); $chapters->addChapter(new Chapter(3450, 'Intro')); $chapters->addChapter(new Chapter(13450, 'Later', 'http://example.com', 'http://example.com/foo.jpg')); $chapters->setPrinter(new Printer\JSON()); $chapters_string = (string) $chapters; $chapters->setPrinter(new Printer\Nullprinter()); $this->assertEquals($chapters, Parser\JSON::parse($chapters_string)); }
public function do_print(\Podlove\Chapters\Chapters $chapters) { return implode("\n", array_map(function ($chapter) { return $chapter->get_time() . ' ' . $chapter->get_title() . ($chapter->get_link() ? ' <' . $chapter->get_link() . '>' : ''); }, $chapters->toArray())); }
function hxmlppp_get_chapters_object($chapters, $mime_type, $chapters_file, $chapter_manager) { // Only run on our mime type for Hindenburg XML if ('application/zip' !== $mime_type) { return $chapters; } // Cache key $cache_key = 'podlove_chapters_string_' . $chapter_manager->episode->id; // Parse Hindenburg XML if (false === ($chapters = get_transient($cache_key))) { // Create new chapters object $chapters = new Chapters(); // Download Hindenburg XML as temp file require_once ABSPATH . 'wp-admin/includes/file.php'; add_filter('http_request_host_is_external', '__return_true'); $file = download_url($chapters_file->get_file_url()); // Init ZipArchive $zip = new ZipArchive(); // Check if it is a valid ZIP file if (true !== ($error_code = $zip->open($file, ZipArchive::CHECKCONS))) { return $chapters; } // Check if the ZIP constains a File5.xml $hindenburg_xml_file = $zip->getFromName('File5.xml'); if (false === $hindenburg_xml_file) { return $chapters; } // Parse Hindenburg XML from ZIP $hindenburg_xml = new SimpleXMLElement($hindenburg_xml_file); // Upload directory $episode_post = get_post($chapter_manager->episode->post_id); $upload_dir = wp_upload_dir(date('Y/m', strtotime($episode_post->post_date))); // Get chapters foreach ($hindenburg_xml->Chapters->children() as $chapter) { // Find chapter link $link = ''; foreach ($chapter->children() as $content) { if ('link' == $content->attributes()->type) { $link = (string) $content->attributes()->url; break; } } // Add chapter image $image = ''; if ('true' == $chapter->attributes()->cover) { $id = (int) $chapter->attributes()->id; $image_contents = $zip->getFromName("Images/Chapters/{$id}/Cover.jpg"); $filename = "/chapter_{$chapter_manager->episode->id}_{$id}.jpg"; file_put_contents($upload_dir['path'] . $filename, $image_contents); $image = $upload_dir['url'] . $filename; } // Add new chapter $chapters->addChapter(new Chapter((int) $chapter->attributes()->start, (string) $chapter->attributes()->title, $link, $image)); } // Remove tmp file @unlink($file); // Save to cache set_transient($cache_key, $chapters, 60 * 60 * 24 * 365); } return $chapters; }
public function do_print(\Podlove\Chapters\Chapters $chapters) { return json_encode(array_map(function ($chapter) { return (object) array('start' => $chapter->get_time(), 'title' => $chapter->get_title(), 'href' => $chapter->get_link(), 'image' => $chapter->get_image()); }, $chapters->toArray())); }