aboutsummaryrefslogtreecommitdiff
path: root/bridges/MagellantvBridge.php
blob: 0a225160d8f5cd5ab6ff015eb32663481ac1ff97 (plain) (blame)
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
<?php

class MagellantvBridge extends BridgeAbstract
{
    const NAME = 'Magellantv articles';
    const URI = 'https://www.magellantv.com/articles';
    const DESCRIPTION = 'Articles of the documentery streaming service Magellantv';
    const MAINTAINER = 'Vincentvd';
    const CACHE_TIMEOUT = 60; // 15 minutes
    const PARAMETERS = [
        [
            'topic' => [
                'type' => 'list',
                'name' => 'Article topic',
                'values' => [
                    'All topics' => 'all',
                    'Ancient history' => 'ancient-history',
                    'Art & culture' => 'art-culture',
                    'Biography' => 'biography',
                    'Current history' => 'current-history',
                    'Early modern' => 'early-modern',
                    'Earth' => 'earth',
                    'Mind & body' => 'mind-body',
                    'Nature' => 'nature',
                    'Science & tech' => 'science-tech',
                    'Short takes' => 'short-takes',
                    'Space' => 'space',
                    'Travel & adventure' => 'travel-adventure',
                    'True crime' => 'true-crime',
                    'War & military' => 'war-military'
                ],
            ]
        ]
    ];

    public function getIcon()
    {
        return 'https://www.magellantv.com/favicon-32x32.png';
    }

    private function retrieveTags($article)
    {
        // Retrieve all tags from an article and store in array
        $article_tags_list = $article->find('div.articleCategory_article-category-tag__uEAXz > a');
        $tags = [];
        foreach ($article_tags_list as $tag) {
            array_push($tags, $tag->plaintext);
        }

        return $tags;
    }

    public function collectData()
    {
        // Determine URL based on topic
        $topic = $this->getInput('topic');
        if ($topic == 'all') {
            $url = 'https://www.magellantv.com/articles';
        } else {
            $url = sprintf('https://www.magellantv.com/articles/category/%s', $topic);
        }
        $dom = getSimpleHTMLDOM($url);

        // Check whether items exists
        $article_list = $dom->find('div.articlePreview_preview-card__mLMOm');
        if (count($article_list) == 0) {
            throw new Exception(sprintf('Unable to find css selector on `%s`', $url));
        }

        // Loop over each article and store article information
        foreach ($article_list as $article) {
            $article = defaultLinkTo($article, $this->getURI());
            $meta_information = $article->find('div.articlePreview_article-metas__kD1i7', 0);
            $title = $article->find('div.articlePreview_article-title___Ci5V > h2 > a', 0);
            $tags_list = $this->retrieveTags($article);

            $item = [
                'title' => $title->plaintext,
                'uri' => $title->href,
                'timestamp' => strtotime($meta_information->find('div.articlePreview_article-date__8Jyfn', 0)->plaintext),
                'author' => $meta_information->find('div.articlePreview_article-author__Ie0_u > span', 1)->plaintext,
                'categories' => $tags_list
            ];

            $this->items[] = $item;
        }
    }
}