aboutsummaryrefslogtreecommitdiff
path: root/bridges/PriviblurBridge.php
blob: 6b442e7503421dc5cb49c48ecccc2e5a5df69b16 (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
<?php

class PriviblurBridge extends BridgeAbstract
{
    const NAME = 'Priviblur';
    const MAINTAINER = 'phantop';
    const URI = 'https://github.com/syeopite/priviblur';
    const DESCRIPTION = 'Returns Tumblr posts from a Priviblur link';
    const PARAMETERS = [
        [
            'url' => [
                'name' => 'URL',
                'exampleValue' => 'https://priviblur.fly.dev',
                'required' => true,
            ]
        ]
    ];

    private $title;
    private $favicon = 'https://www.tumblr.com/favicon.ico';

    public function collectData()
    {
        $url = $this->getURI();
        $html = getSimpleHTMLDOM($url);
        $html = defaultLinkTo($html, $url);
        $this->title = $html->find('head title', 0)->innertext;

        if ($html->find('#blog-header img.avatar', 0)) {
            $icon = $html->find('#blog-header img.avatar', 0)->src;
            $this->favicon = str_replace('pnj', 'png', $icon);
        }

        $elements = $html->find('.post');
        foreach ($elements as $element) {
            $item = [];
            $item['author'] = $element->find('.primary-post-author .blog-name', 0)->innertext;
            $item['comments'] = $element->find('.interaction-buttons > a', 1)->href;
            $item['content'] = $element->find('.post-body', 0);
            $item['timestamp'] = $element->find('.primary-post-author time', 0)->innertext;
            $item['title'] = $item['author'] . ': ' . $item['timestamp'];
            $item['uid'] = $item['comments']; // tumblr url is canonical
            $item['uri'] = $element->find('.interaction-buttons > a', 0)->href;

            if ($element->find('.post-tags', 0)) {
                $tags = html_entity_decode($element->find('.post-tags', 0)->plaintext);
                $tags = explode('#', $tags);
                $tags = array_map('trim', $tags);
                array_shift($tags);
                $item['categories'] = $tags;
            }

            $heading = $element->find('h1', 0);
            if ($heading) {
                $item['title'] = $heading->innertext;
            }

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

    public function getName()
    {
        $name = parent::getName();
        if (isset($this->title)) {
            $name = $this->title;
        }
        return $name;
    }

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

    public function getIcon()
    {
        return $this->favicon;
    }
}