RSS-Bridge.rss-bridge/formats/M3uFormat.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
2.8 KiB
PHP
Raw Normal View History

2024-03-04 15:30:22 +01:00
<?php
/**
* M3U
*
*/
class M3uFormat extends FormatAbstract
{
const MIME_TYPE = 'application/mpegurl';
2024-03-07 15:21:23 +01:00
private $item_duration = null;
private $item_title = null;
private $item_url = null;
private $item_bytes = null;
private function resetItem()
{
$this->item_duration = null;
$this->item_title = null;
$this->item_url = null;
$this->item_bytes = null;
}
private function itemIsEmpty(): bool
{
return $this->item_url === null;
}
private function itemRender(): string
{
if ($this->itemIsEmpty()) {
return '';
}
$text = "\n";
$commentParts = [];
if ($this->item_duration !== null && $this->item_duration > 0) {
$commentParts[] = $this->item_duration;
}
if ($this->item_title !== null) {
$commentParts[] = $this->item_title;
}
if (count($commentParts) !== 0) {
$text .= '#EXTINF:' . implode(',', $commentParts) . "\n";
}
$text .= $this->item_url . "\n";
return $text;
}
2024-03-04 15:30:22 +01:00
public function stringify()
{
$contents = "#EXTM3U\n";
foreach ($this->getItems() as $item) {
2024-03-07 15:21:23 +01:00
$this->resetItem();
2024-03-04 15:30:22 +01:00
$itemArray = $item->toArray();
2024-03-04 15:52:05 +01:00
if (isset($itemArray['enclosure'])) {
2024-03-07 15:21:23 +01:00
$this->item_url = $itemArray['enclosure']['url'];
$this->item_bytes = $itemArray['enclosure']['length'];
if (isset($itemArray['itunes']) && isset($itemArray['itunes']['duration'])) {
2024-03-07 15:21:23 +01:00
$this->item_duration = self::parseDuration($itemArray['itunes']['duration']);
}
2024-03-04 15:52:05 +01:00
}
if (isset($itemArray['title'])) {
2024-03-07 15:21:23 +01:00
$item->item_title = $itemArray['title'];
2024-03-04 15:30:22 +01:00
}
2024-03-07 15:21:23 +01:00
if (! $this->itemIsEmpty()) {
$contents .= $this->itemRender();
} else {
foreach ($item->enclosures as $url) {
2024-03-07 15:21:23 +01:00
$this->resetItem();
$this->item_url = $url;
if (isset($itemArray['title'])) {
2024-03-07 15:21:23 +01:00
$this->item_title = $itemArray['title'];
}
2024-03-07 15:21:23 +01:00
$contents .= $this->itemRender();
}
}
2024-03-04 15:30:22 +01:00
}
return mb_convert_encoding($contents, $this->getCharset(), 'UTF-8');
}
/*
* parseDuration converts a string like "00:4:20" to 260
* allowing to convert duration as used in the itunes:duration tag to the number of seconds
*/
private static function parseDuration(string $duration_string): int
{
$seconds = 0;
$parts = explode(':', $duration_string);
for ($i = 0; $i < count($parts); $i++) {
$seconds += intval($parts[count($parts) - $i - 1]) * pow(60, $i);
}
return $seconds;
2024-03-04 15:52:05 +01:00
}
}