summaryrefslogtreecommitdiffstats
path: root/lib/Command/ExploreGenerator.php
blob: 01ef2a866714fa42ae3c17620025aa765250630c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Sean Molenaar <sean@seanmolenaar.eu>
 * @copyright Sean Molenaar 2019
 */
namespace OCA\News\Command;

use FeedIo\FeedIo;
use Favicon\Favicon;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * This is used for generating a JSON config section for a feed by executing:
 * ./occ news:generate-explore www.feed.com
 */
class ExploreGenerator extends Command
{
    /**
     * Feed and favicon fetcher.
     */
    protected $reader;
    protected $favicon;

    /**
     * Set up class.
     *
     * @param FeedIo  $reader  Feed reader
     * @param Favicon $favicon Favicon fetcher
     */
    public function __construct(FeedIo $reader, Favicon $favicon)
    {
        $this->reader  = $reader;
        $this->favicon = $favicon;
        parent::__construct();
    }

    /**
     * @return void
     */
    protected function configure()
    {
        $result = [
            'title'       => 'Feed - Title',
            'favicon'     => 'www.web.com/favicon.ico',
            'url'         => 'www.web.com',
            'feed'        => 'www.web.com/rss.xml',
            'description' => 'description is here',
            'votes'       => 100,
        ];

        $this->setName('news:generate-explore')
            ->setDescription(
                'Prints a JSON string which represents the given ' .
                'feed URL and votes, e.g.: ' . json_encode($result)
            )
            ->addArgument('feed', InputArgument::REQUIRED, 'Feed to parse')
            ->addOption('votes', null, InputOption::VALUE_OPTIONAL, 'Votes for the feed, defaults to 100');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $url   = $input->getArgument('feed');
        $votes = $input->getOption('votes');
        if (!$votes) {
            $votes = 100;
        }

        try {
            $resource = $this->reader->read($url);
            $feed = $resource->getFeed();
            $result = [
                'title'       => $feed->getTitle(),
                'favicon'     => $this->favicon->get($feed->getLink()),
                'url'         => $feed->getLink(),
                'feed'        => $url,
                'description' => $feed->getDescription(),
                'votes'       => $votes,
            ];

            $output->writeln(json_encode($result, JSON_PRETTY_PRINT));
            return 0;
        } catch (\Throwable $ex) {
            $output->writeln('<error>Failed to fetch feed info:</error>');
            $output->writeln($ex->getMessage());
            return 1;
        }
    }
}