aboutsummaryrefslogtreecommitdiff
path: root/bridges/DerpibooruBridge.php
blob: 2d650d57e461660d6d5f9aa29663e38dd20e33f8 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php

class DerpibooruBridge extends BridgeAbstract
{
    const NAME = 'Derpibooru Bridge';
    const URI = 'https://derpibooru.org/';
    const DESCRIPTION = 'Returns newest images from a Derpibooru search';
    const CACHE_TIMEOUT = 300; // 5min
    const MAINTAINER = 'Roliga';

    const PARAMETERS = [
        [
            'f' => [
                'name' => 'Filter',
                'type' => 'list',
                'values' => [
                    'Everything' => 56027,
                    '18+ R34' => 37432,
                    'Legacy Default' => 37431,
                    '18+ Dark' => 37429,
                    'Maximum Spoilers' => 37430,
                    'Default' => 100073
                ],
                'defaultValue' => 56027

            ],
            'q' => [
                'name' => 'Query',
                'required' => true,
                'exampleValue' => 'dog',
            ]
        ]
    ];

    public function detectParameters($url)
    {
        $params = [];

        // Search page e.g. https://derpibooru.org/search?q=cute
        $regex = '/^(https?:\/\/)?(www\.)?derpibooru.org\/search.+q=([^\/&?\n]+)/';
        if (preg_match($regex, $url, $matches) > 0) {
            $params['q'] = urldecode($matches[3]);
            return $params;
        }

        // Tag page, e.g. https://derpibooru.org/tags/artist-colon-devinian
        $regex = '/^(https?:\/\/)?(www\.)?derpibooru.org\/tags\/([^\/&?\n]+)/';
        if (preg_match($regex, $url, $matches) > 0) {
            $params['q'] = str_replace('-colon-', ':', urldecode($matches[3]));
            return $params;
        }

        return null;
    }

    public function getName()
    {
        if (!is_null($this->getInput('q'))) {
            return 'Derpibooru search for: '
                . $this->getInput('q');
        } else {
            return parent::getName();
        }
    }

    public function getURI()
    {
        if (!is_null($this->getInput('f')) && !is_null($this->getInput('q'))) {
            return self::URI
            . 'search?filter_id='
            . urlencode($this->getInput('f'))
            . '&q='
            . urlencode($this->getInput('q'));
        } else {
            return parent::getURI();
        }
    }

    public function collectData()
    {
        $url = self::URI . 'api/v1/json/search/images?filter_id=' . urlencode($this->getInput('f')) . '&q=' . urlencode($this->getInput('q'));

        $queryJson = json_decode(getContents($url));

        foreach ($queryJson->images as $post) {
            $item = [];

            $postUri = self::URI . $post->id;

            $item['uri'] = $postUri;
            $item['title'] = $post->name;
            $item['timestamp'] = strtotime($post->created_at);
            $item['author'] = $post->uploader;
            $item['enclosures'] = [$post->view_url];
            $item['categories'] = $post->tags;

            $item['content'] = '<p><a href="' // image preview
                . $postUri
                . '"><img src="'
                . $post->representations->medium
                . '"></a></p><p>' // description
                . $post->description
                . '</p><p><b>Size:</b> ' // image size
                . $post->width
                . 'x'
                . $post->height;
            // source link
            if ($post->source_url != null) {
                $item['content'] .= '<br><b>Source:</b> <a href="'
                . $post->source_url
                . '">'
                . $post->source_url
                . '</a></p>';
            };
            $this->items[] = $item;
        }
    }
}