summaryrefslogtreecommitdiffstats
path: root/bin/tools/generate_explore.php
blob: 766e82db7d376f424c3a65d72124be699062485c (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
#!/usr/bin/env php
<?php
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright Bernhard Posselt 2016
 */
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../../../lib/base.php';

use FeedIo\FeedIo;
use Favicon\Favicon;
use OCA\News\AppInfo\Application;

$generator = new ExploreGenerator();
$generator->parse_argv($argv);
print(json_encode($generator->read(), JSON_PRETTY_PRINT));
print("\n");

/**
 * This is used for generating a JSON config section for a feed by executing:
 * php -f generate_authors.php www.feed.com
 * @deprecated Use ./occ news:generate-explore instead.
 */
class ExploreGenerator
{
    /**
     * Feed and favicon fetcher.
     */
    protected $reader;
    protected $favicon;

    /**
     * Argument data
     */
    protected $url;
    protected $votes;

    /**
     * Set up class.
     */
    public function __construct()
    {
        $app = new Application();
        $container = $app->getContainer();

        $this->reader  = $container->query(FeedIo::class);
        $this->favicon = new Favicon();
    }

    /**
     * Parse required arguments.
     * @param array $argv Arguments to the script.
     * @return void
     */
    public function parse_argv($argv = [])
    {
        if (count($argv) < 2 || count($argv) > 3)
        {
            print('Usage: php -f generate_explore http://path.com/feed [vote_count]');
            print("\n");
            exit(1);
        }

        $this->votes = (count($argv) === 3) ? $argv[2] : 100;
        $this->url = $argv[1];
    }

    /**
     * Read the provided feed and return the important data.
     * @return array Object representation of the feed
     */
    public function read()
    {
        try {
            $resource = $this->reader->read($this->url);
            $feed = $resource->getFeed();
            $result = [
                'title'       => $feed->getTitle(),
                'favicon'     => $this->favicon->get($feed->getLink()),
                'url'         => $feed->getLink(),
                'feed'        => $this->url,
                'description' => $feed->getDescription(),
                'votes'       => $this->votes,
            ];

            return $result;
        } catch (\Throwable $ex) {
            return [ 'error' => $ex->getMessage() ];
        }
    }

}