aboutsummaryrefslogtreecommitdiff
path: root/bridges/YeggiBridge.php
blob: 07f1dd4dbe68c0d4a56ba44adb6833112078cf1c (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
<?php

class YeggiBridge extends BridgeAbstract
{
    const NAME = 'Yeggi Search';
    const URI = 'https://www.yeggi.com';
    const DESCRIPTION = 'Returns 3D Models from Thingiverse, MyMiniFactory, Cults3D, and more';
    const MAINTAINER = 'AntoineTurmel';
    const PARAMETERS = [
        [
            'query' => [
                'name' => 'Search query',
                'type' => 'text',
                'required' => true,
                'title' => 'Insert your search term here',
                'exampleValue' => 'vase'
            ],
            'sortby' => [
                'name' => 'Sort by',
                'type' => 'list',
                'required' => false,
                'values' => [
                    'Best match' => '0',
                    'Popular' => '1',
                    'Latest' => '2',
                ],
                'defaultValue' => 'newest'
            ],
            'show' => [
                'name' => 'Show',
                'type' => 'list',
                'required' => false,
                'values' => [
                    'All' => '0',
                    'Free' => '1',
                    'For sale' => '2',
                ],
                'defaultValue' => 'all'
            ],
            'showimage' => [
                'name' => 'Show image in content',
                'type' => 'checkbox',
                'required' => false,
                'title' => 'Activate to show the image in the content',
                'defaultValue' => 'checked'
            ]
        ]
    ];

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

        $results = $html->find('div.item_1_A');

        foreach ($results as $result) {
            $item = [];
            $title = $result->find('.item_3_B_2', 0)->plaintext;
            $explodeTitle = explode('&nbsp;  ', $title);
            if (count($explodeTitle) == 2) {
                $item['title'] = $explodeTitle[1];
            } else {
                $item['title'] = $explodeTitle[0];
            }
            $item['uri'] = self::URI . $result->find('a', 0)->href;
            $item['author'] = 'Yeggi';

            $text = $result->find('i');
            $item['content'] = $text[0]->plaintext . ' on ' . $text[1]->plaintext;
            $item['uid'] = hash('md5', $item['title']);

            foreach ($result->find('.item_3_B_2 > a[href^=/q/]') as $tag) {
                $item['tags'][] = $tag->plaintext;
            }

            $image = $result->find('img', 0)->src;

            if ($this->getInput('showimage')) {
                $item['content'] .= '<br><img src="' . $image . '">';
            }

            $item['enclosures'] = [$image];

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

    public function getURI()
    {
        if (!is_null($this->getInput('query'))) {
            $uri = self::URI . '/q/' . urlencode($this->getInput('query')) . '/';
            $uri .= '?o_f=' . $this->getInput('show');
            $uri .= '&o_s=' . $this->getInput('sortby');

            return $uri;
        }

        return parent::getURI();
    }
}