|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ownCloud - Music app |
|
5
|
|
|
* |
|
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
7
|
|
|
* later. See the COPYING file. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Pauli Järvinen <[email protected]> |
|
10
|
|
|
* @copyright Pauli Järvinen 2021 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace OCA\Music\Command; |
|
14
|
|
|
|
|
15
|
|
|
use OCA\Music\BusinessLayer\PodcastChannelBusinessLayer; |
|
16
|
|
|
use OCA\Music\BusinessLayer\PodcastEpisodeBusinessLayer; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
use Doctrine\Instantiator\Exception\InvalidArgumentException; |
|
21
|
|
|
|
|
22
|
|
|
class PodcastAdd extends BaseCommand { |
|
23
|
|
|
|
|
24
|
|
|
/** @var PodcastChannelBusinessLayer */ |
|
25
|
|
|
private $channelBusinessLayer; |
|
26
|
|
|
/** @var PodcastEpisodeBusinessLayer */ |
|
27
|
|
|
private $episodeBusinessLayer; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct( |
|
30
|
|
|
\OCP\IUserManager $userManager, |
|
31
|
|
|
\OCP\IGroupManager $groupManager, |
|
32
|
|
|
PodcastChannelBusinessLayer $channelBusinessLayer, |
|
33
|
|
|
PodcastEpisodeBusinessLayer $episodeBusinessLayer) { |
|
34
|
|
|
$this->channelBusinessLayer = $channelBusinessLayer; |
|
35
|
|
|
$this->episodeBusinessLayer = $episodeBusinessLayer; |
|
36
|
|
|
parent::__construct($userManager, $groupManager); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function doConfigure() { |
|
40
|
|
|
$this |
|
41
|
|
|
->setName('music:podcast-add') |
|
42
|
|
|
->setDescription('add a podcast channel from an RSS feed') |
|
43
|
|
|
->addOption( |
|
44
|
|
|
'rss', |
|
45
|
|
|
null, |
|
46
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
|
47
|
|
|
'URL to an RSS feed' |
|
48
|
|
|
) |
|
49
|
|
|
; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function doExecute(InputInterface $input, OutputInterface $output, $users) { |
|
53
|
|
|
$rssUrls = $input->getOption('rss'); |
|
54
|
|
|
|
|
55
|
|
|
if (!$rssUrls) { |
|
56
|
|
|
throw new \InvalidArgumentException("The named argument <error>rss</error> must be given!"); |
|
57
|
|
|
} |
|
58
|
|
|
\assert(\is_array($rssUrls)); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($rssUrls as $rss) { |
|
61
|
|
|
$this->addPodcast($rss, $input, $output, $users); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function addPodcast(string $rss, InputInterface $input, OutputInterface $output, $users) : void { |
|
66
|
|
|
$content = \file_get_contents($rss); |
|
67
|
|
|
if ($content === false) { |
|
68
|
|
|
throw new \InvalidArgumentException("Invalid URL <error>$rss</error>!"); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$xmlTree = \simplexml_load_string($content, \SimpleXMLElement::class, LIBXML_NOCDATA); |
|
72
|
|
|
if ($xmlTree === false || !$xmlTree->channel) { |
|
73
|
|
|
throw new \InvalidArgumentException("The document at URL <error>$rss</error> is not a valid podcast RSS feed!"); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($input->getOption('all')) { |
|
77
|
|
|
$this->userManager->callForAllUsers(function($user) use ($output, $rss, $content, $xmlTree) { |
|
78
|
|
|
$this->addPodcastForUser($user->getUID(), $rss, $content, $xmlTree->channel, $output); |
|
79
|
|
|
}); |
|
80
|
|
|
} else { |
|
81
|
|
|
foreach ($users as $userId) { |
|
82
|
|
|
$this->addPodcastForUser($userId, $rss, $content, $xmlTree->channel, $output); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function addPodcastForUser(string $userId, string $rss, string $content, \SimpleXMLElement $xmlNode, OutputInterface $output) : void { |
|
88
|
|
|
$output->writeln("Adding podcast channel <info>{$xmlNode->title}</info> for user <info>$userId</info>"); |
|
89
|
|
|
try { |
|
90
|
|
|
$channel = $this->channelBusinessLayer->create($userId, $rss, $content, $xmlNode); |
|
91
|
|
|
|
|
92
|
|
|
// loop the episodes from XML in reverse order to get chronological order |
|
93
|
|
|
$items = $xmlNode->item; |
|
94
|
|
|
for ($count = \count($items), $i = $count-1; $i >= 0; --$i) { |
|
95
|
|
|
if ($items[$i] !== null) { |
|
96
|
|
|
$this->episodeBusinessLayer->addOrUpdate($userId, $channel->getId(), $items[$i]); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} catch (\OCA\Music\AppFramework\Db\UniqueConstraintViolationException $ex) { |
|
100
|
|
|
$output->writeln('User already has this podcast channel, skipping'); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|