aboutsummaryrefslogtreecommitdiff
path: root/bridges/DoujinStyleBridge.php
blob: 0df96280d10b7fb4105858e14a73b9dc55aab7ca (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

class DoujinStyleBridge extends BridgeAbstract
{
    const NAME = 'DoujinStyle Bridge';
    const URI = 'https://doujinstyle.com/';
    const DESCRIPTION = 'Returns submissions from DoujinStyle';
    const MAINTAINER = 'mrtnvgr';

    // TODO: "Games" support

    const PARAMETERS = [
        'Most recent submissions' => [],
        'Randomly selected items' => [],
        'From search results' => [
            'query' => [
                'name' => 'Search query',
                'required' => true,
                'exampleValue' => 'FELT',
            ],
            'flac' => [
                'name' => 'Include FLAC',
                'type' => 'checkbox',
                'defaultValue' => false,
            ],
            'mp3' => [
                'name' => 'Include MP3',
                'type' => 'checkbox',
                'defaultValue' => false,
            ],
            'tta' => [
                'name' => 'Include TTA',
                'type' => 'checkbox',
                'defaultValue' => false,
            ],
            'opus' => [
                'name' => 'Include Opus',
                'type' => 'checkbox',
                'defaultValue' => false,
            ],
            'ogg' => [
                'name' => 'Include OGG',
                'type' => 'checkbox',
                'defaultValue' => false,
            ]
        ]
    ];

    public function collectData()
    {
        $html = getSimpleHTMLDOM($this->getURI());
        $html = defaultLinkTo($html, $this->getURI());

        $submissions = $html->find('.gridBox .gridDetails');
        foreach ($submissions as $submission) {
            $item = [];

            $item['uri'] = $submission->find('a', 0)->href;

            $content = getSimpleHTMLDOM($item['uri']);
            $content = defaultLinkTo($content, $this->getURI());

            $title = $content->find('h2', 0)->plaintext;

            $cover = $content->find('#imgClick a', 0);
            if (is_null($cover)) {
                $cover = $content->find('.coverWrap', 0)->src;
            } else {
                $cover = $cover->href;
            }

            $item['content'] = "<img src='$cover'/>";

            $keys = [];
            foreach ($content->find('.pageWrap .pageSpan1') as $key) {
                $keys[] = $key->plaintext;
            }

            $values = $content->find('.pageWrap .pageSpan2');
            $metadata = array_combine($keys, $values);

            $format = 'Unknown';

            foreach ($metadata as $key => $value) {
                switch ($key) {
                    case 'Artist':
                        $artist = $value->find('a', 0)->plaintext;
                        $item['title'] = "$artist - $title";
                        $item['content'] .= "<br>Artist: $artist";
                        break;
                    case 'Tags:':
                        $item['categories'] = [];
                        foreach ($value->find('a') as $tag) {
                            $tag = str_replace('&#45;', '-', $tag->plaintext);
                            $item['categories'][] = $tag;
                        }

                        $item['content'] .= '<br>Tags: ' . join(', ', $item['categories']);
                        break;
                    case 'Format:':
                        $item['content'] .= "<br>Format: $value->plaintext";
                        break;
                    case 'Date Added:':
                        $item['timestamp'] = $value->plaintext;
                        break;
                    case 'Provided By:':
                        $item['author'] = $value->find('a', 0)->plaintext;
                        break;
                }
            }

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

    public function getURI()
    {
        $url = self::URI;

        switch ($this->queriedContext) {
            case 'From search results':
                $url .= '?p=search&type=blanket';
                $url .= '&result=' . $this->getInput('query');

                if ($this->getInput('flac') == 1) {
                    $url .= '&format0=on';
                }
                if ($this->getInput('mp3') == 1) {
                    $url .= '&format1=on';
                }
                if ($this->getInput('tta') == 1) {
                    $url .= '&format2=on';
                }
                if ($this->getInput('opus') == 1) {
                    $url .= '&format3=on';
                }
                if ($this->getInput('ogg') == 1) {
                    $url .= '&format4=on';
                }
                break;
            case 'Randomly selected items':
                $url .= '?p=random';
                break;
        }

        return $url;
    }
}