Skip to content

Commit

Permalink
[AutoPodcaster] style fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
boyska committed Mar 16, 2024
1 parent 5da4de3 commit fb41120
Showing 1 changed file with 62 additions and 49 deletions.
111 changes: 62 additions & 49 deletions bridges/AutoPodcasterBridge.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
class AutoPodcasterBridge extends FeedExpander {
const MAINTAINER='boyska';
const NAME='Auto Podcaster';

class AutoPodcasterBridge extends FeedExpander
{
const MAINTAINER = 'boyska';
const NAME = 'Auto Podcaster';
const URI = '';
const CACHE_TIMEOUT = 300; // 5 minuti
const DESCRIPTION='Make a "multimedia" podcast out of a normal feed';
const PARAMETERS = array(array(
const DESCRIPTION = 'Make a "multimedia" podcast out of a normal feed';
const PARAMETERS = ['url' => [
'url' => [
'name' => 'URL',
'required' => true
Expand All @@ -15,49 +17,56 @@ class AutoPodcasterBridge extends FeedExpander {
'type' => 'checkbox',
'required' => false,
]
));
]];

private function archiveIsAudioFormat($formatString) {
private function archiveIsAudioFormat($formatString)
{
return strpos($formatString, 'MP3') !== false ||
strpos($formatString, 'Ogg') === 0;
}

private function extractAudio($dom) {
private function extractAudio($dom)
{
$audios = [];
foreach($dom->find('audio') as $audioEl) {
foreach ($dom->find('audio') as $audioEl) {
$sources = [];
if($audioEl->src !== false) {
if ($audioEl->src !== false) {
$sources[] = $audioEl->src;
}
foreach($audioEl->find('source') as $sourceEl) {
foreach ($audioEl->find('source') as $sourceEl) {
$sources[] = $sourceEl->src;
}
if($sources) {
if ($sources) {
$audios[$sources[0]] = ['sources' => $sources];
}
}
return $audios;
}
private function extractIframeArchive($dom) {
private function extractIframeArchive($dom)
{
$audios = [];

foreach($dom->find('iframe') as $iframeEl) {
if(strpos($iframeEl->src, "https://archive.org/embed/") === 0) {
$listURL = preg_replace("/\/embed\//", "/details/", $iframeEl->src, 1) . "?output=json";
$baseURL = preg_replace("/\/embed\//", "/download/", $iframeEl->src, 1);
foreach ($dom->find('iframe') as $iframeEl) {
if (strpos($iframeEl->src, 'https://archive.org/embed/') === 0) {
$listURL = preg_replace('/\/embed\//', '/details/', $iframeEl->src, 1) . '?output=json';
$baseURL = preg_replace('/\/embed\//', '/download/', $iframeEl->src, 1);
$list = json_decode(file_get_contents($listURL));
$audios = [];
foreach($list->files as $name =>$data) {
if($data->source === 'original' &&
$this->archiveIsAudioFormat($data->format)) {
foreach ($list->files as $name => $data) {
if (
$data->source === 'original' &&
$this->archiveIsAudioFormat($data->format)
) {
$audios[$baseURL . $name] = ['sources' => [$baseURL . $name]];
}
}
foreach($list->files as $name =>$data) {
if($data->source === 'derivative' &&
foreach ($list->files as $name => $data) {
if (
$data->source === 'derivative' &&
$this->archiveIsAudioFormat($data->format) &&
isset($audios[$baseURL . "/" . $data->original])) {
$audios[$baseURL . "/" . $data->original]['sources'][] = $baseURL . $name;
isset($audios[$baseURL . '/' . $data->original])
) {
$audios[$baseURL . '/' . $data->original]['sources'][] = $baseURL . $name;
}
}
}
Expand All @@ -66,10 +75,11 @@ private function extractIframeArchive($dom) {
return $audios;
}

protected function parseItem($newItem){
$item = parent::parseItem($newItem);
protected function parseItem($newItem)
{
$item = parent::parseItem($newItem);

if(! $this->getInput('feed_only')) {
if (! $this->getInput('feed_only')) {
$dom = getSimpleHTMLDOMCached($item['uri']);
// $dom will be false in case of errors
} else {
Expand All @@ -82,43 +92,46 @@ protected function parseItem($newItem){

/* 2nd extraction method: by "iframe" tag */
$audios = array_merge($audios, $this->extractIframeArchive($dom));
}
elseif($item['content'] !== NULL) {
} elseif ($item['content'] !== null) {
$item_dom = str_get_html($item['content']);
/* 1st extraction method: by "audio" tag */
$audios = array_merge($audios, $this->extractAudio($item_dom));

/* 2nd extraction method: by "iframe" tag */
$audios = array_merge($audios,
$this->extractIframeArchive($item_dom));
$audios = array_merge(
$audios,
$this->extractIframeArchive($item_dom)
);
}

if(count($audios) === 0) {
if (count($audios) === 0) {
return null;
}
$item['enclosures'] = [];
foreach(array_values($audios) as $audio) {
foreach (array_values($audios) as $audio) {
$item['enclosures'][] = $audio['sources'][0];
}
return $item;
}
public function collectData(){
if($this->getInput('url') && substr($this->getInput('url'), 0, strlen('http')) !== 'http') {
// just in case someone find a way to access local files by playing with the url
returnClientError('The url parameter must either refer to http or https protocol.');
}
}
public function collectData()
{
if ($this->getInput('url') && substr($this->getInput('url'), 0, strlen('http')) !== 'http') {
// just in case someone find a way to access local files by playing with the url
returnClientError('The url parameter must either refer to http or https protocol.');
}
$this->collectExpandableDatas($this->getURI());
}
public function getName(){
if(!is_null($this->getInput('url'))) {
return self::NAME . ' : ' . $this->getInput('url');
}

return parent::getName();
}
public function getURI(){
return $this->getInput('url');
}
public function getName()
{
if (!is_null($this->getInput('url'))) {
return self::NAME . ' : ' . $this->getInput('url');
}

return parent::getName();
}
public function getURI()
{
return $this->getInput('url');
}
}

0 comments on commit fb41120

Please sign in to comment.