Yandex.php 2.67 KB
Newer Older
1
2
3
<?php

namespace app\Models\parserSkripte;
4

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

8
class Yandex extends Searchengine
9
{
10
11
12
13
14
15
    public $results = [];

    public function __construct(\SimpleXMLElement $engine, \App\MetaGer $metager)
    {
        parent::__construct($engine, $metager);
    }
16

17
18
19
20
21
22
    public function loadResults($result)
    {
        $result = preg_replace("/\r\n/si", "", $result);
        try {
            $content = simplexml_load_string($result);
        } catch (\Exception $e) {
23
24
            Log::error("Results from $this->name are not a valid json string");
            return;
25
26
27
28
        }
        if (!$content) {
            return;
        }
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
        $results = $content;
        try {
            $results = $results->xpath("//yandexsearch/response/results/grouping/group");
        } catch (\ErrorException $e) {
            return;
        }
        foreach ($results as $result) {
            $title       = strip_tags($result->{"doc"}->{"title"}->asXML());
            $link        = $result->{"doc"}->{"url"}->__toString();
            $anzeigeLink = $link;
            $descr       = strip_tags($result->{"doc"}->{"headline"}->asXML());
            if (!$descr) {
                $descr = strip_tags($result->{"doc"}->{"passages"}->asXML());
            }
            $this->counter++;
            $this->results[] = new \App\Models\Result(
                $this->engine,
                $title,
                $link,
                $anzeigeLink,
                $descr,
                $this->gefVon,
                $this->counter
            );
        }
    }
56
57
58

    public function getNext(\App\MetaGer $metager, $result)
    {
59
60
        # Wir müssen herausfinden, ob es überhaupt noch weitere Ergebnisse von Yandex gibt:
        try {
61
            $content = simplexml_load_string($result);
62
63
64
65
66
67
68
69
70
71
72
73
74
75
            if (!$content) {
                return;
            }

            $resultCount = intval($content->xpath('//yandexsearch/response/results/grouping/found[@priority="all"]')[0]->__toString());
            $pageLast    = $content->xpath('//yandexsearch/response/results/grouping/page')[0];
            $pageLast    = intval($pageLast["last"]->__toString());
            if (count($this->results) <= 0 || $pageLast >= $resultCount) {
                return;
            }
            $next = new Yandex(simplexml_load_string($this->engine), $metager);
            $next->getString .= "&page=" . ($metager->getPage() + 1);
            $next->hash = md5($next->host . $next->getString . $next->port . $next->name);
            $this->next = $next;
76
        } catch (\Exception $e) {
77
78
79
            Log::error("Results from $this->name are not a valid json string");
            return;
        }
80

81
    }
Dominik Hebeler's avatar
Dominik Hebeler committed
82
}