Europeana.php 2.47 KB
Newer Older
1 2 3
<?php

namespace app\Models\parserSkripte;
4

5
use App\Models\Searchengine;
6
use Log;
7

8
class Europeana extends Searchengine
9
{
10 11
    public $results = [];

12
    public function __construct($name, \StdClass $engine, \App\MetaGer $metager)
13
    {
14
        parent::__construct($name, $engine, $metager);
15
    }
16

17 18 19 20 21
    public function loadResults($result)
    {
        $result = preg_replace("/\r\n/si", "", $result);
        try {
            $content = json_decode($result);
22 23 24 25 26 27 28 29 30 31 32 33 34 35
            if (!$content) {
                return;
            }

            $results = $content->items;
            foreach ($results as $result) {
                if (isset($result->edmPreview)) {
                    $title = $result->title[0];
                    if (preg_match("/(.+)\?.*/si", $result->guid, $match)) {
                        $link = $match[1];
                    } else {
                        $link = "";
                    }
                    $anzeigeLink = $link;
36 37
                    $descr = "";
                    $image = urldecode($result->edmPreview[0]);
38 39 40 41 42 43 44
                    $this->counter++;
                    $this->results[] = new \App\Models\Result(
                        $this->engine,
                        $title,
                        $link,
                        $anzeigeLink,
                        $descr,
45
                        $this->engine->{"display-name"}, $this->engine->homepage,
46
                        $this->counter,
47
                        ['image' => $image]
48
                    );
49 50
                }
            }
51
        } catch (\Exception $e) {
52 53
            Log::error("A problem occurred parsing results from $this->name:");
            Log::error($e->getMessage());
54
            return;
55 56
        }
    }
57 58 59

    public function getNext(\App\MetaGer $metager, $result)
    {
60 61
        try {
            $content = json_decode($result);
62 63 64 65 66 67 68 69
            if (!$content) {
                return;
            }

            $start = ($metager->getPage()) * 10 + 1;
            if ($start > $content->totalResults) {
                return;
            }
70
            $next = new Europeana($this->engine, $metager);
71
            $next->getString .= "&start=" . $start;
72
            $next->hash = md5($next->engine->host . $next->getString . $next->engine->port . $next->name);
73
            $this->next = $next;
74
        } catch (\Exception $e) {
75 76
            Log::error("A problem occurred parsing results from $this->name:");
            Log::error($e->getMessage());
77 78 79
            return;
        }
    }
80
}