Pixabay.php 1.92 KB
Newer Older
Phil Höfer's avatar
Phil Höfer committed
1
2
3
<?php

namespace app\Models\parserSkripte;
4

Phil Höfer's avatar
Phil Höfer committed
5
6
use App\Models\Searchengine;

7
class Pixabay extends Searchengine
Phil Höfer's avatar
Phil Höfer committed
8
{
9
10
11
12
13
14
    public $results = [];

    public function __construct(\SimpleXMLElement $engine, \App\MetaGer $metager)
    {
        parent::__construct($engine, $metager);
    }
Phil Höfer's avatar
Phil Höfer committed
15

16
17
18
19
20
21
    public function loadResults($result)
    {
        $result = preg_replace("/\r\n/si", "", $result);
        try {
            $content = json_decode($result);
        } catch (\Exception $e) {
22
23
            Log::error("Results from $this->name are not a valid json string");
            return;
24
        }
Phil Höfer's avatar
Phil Höfer committed
25

26
27
28
        if (!$content) {
            return;
        }
Phil Höfer's avatar
Phil Höfer committed
29

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
        $results = $content->hits;
        foreach ($results as $result) {
            $title       = $result->tags;
            $link        = $result->pageURL;
            $anzeigeLink = $link;
            $descr       = "";
            $image       = $result->previewURL;
            $this->counter++;
            $this->results[] = new \App\Models\Result(
                $this->engine,
                $title,
                $link,
                $anzeigeLink,
                $descr,
                $this->gefVon,
                $this->counter,
                false,
                $image
            );
        }
    }
51
52
53

    public function getNext(\App\MetaGer $metager, $result)
    {
54
55
56
57
58
59
60
61
62
63
        $page = $metager->getPage() + 1;
        try {
            $content = json_decode($result);
        } catch (\Exception $e) {
            Log::error("Results from $this->name are not a valid json string");
            return;
        }
        if (!$content) {
            return;
        }
64
65
66
67
68
69
70
71
        if ($page * 20 > $content->total) {
            return;
        }
        $next = new Pixabay(simplexml_load_string($this->engine), $metager);
        $next->getString .= "&page=" . $page;
        $next->hash = md5($next->host . $next->getString . $next->port . $next->name);
        $this->next = $next;
    }
Phil Höfer's avatar
Phil Höfer committed
72
}