Yandex.php 3.21 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
    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 = simplexml_load_string($result);
22 23 24
            if (!$content) {
                return;
            }
25

26 27 28 29
            # let's check if the query got unquoted
            # in that case we will ignore all results because that would mean
            # a string search (query between "") was wished and no results for that foudn
            $reask = $content->xpath("//yandexsearch/response/reask");
30
            if (sizeof($reask) !== 0 && $reask[0]->{"rule"}->__toString()) {
31 32 33
                return;
            }

34 35
            $results = $content->xpath("//yandexsearch/response/results/grouping/group");
            foreach ($results as $result) {
36 37
                $title = strip_tags($result->{"doc"}->{"title"}->asXML());
                $link = $result->{"doc"}->{"url"}->__toString();
38
                $anzeigeLink = $link;
39
                $descr = strip_tags($result->{"doc"}->{"headline"}->asXML());
40 41 42 43 44 45 46 47 48 49
                if (!$descr) {
                    $descr = strip_tags($result->{"doc"}->{"passages"}->asXML());
                }
                $this->counter++;
                $this->results[] = new \App\Models\Result(
                    $this->engine,
                    $title,
                    $link,
                    $anzeigeLink,
                    $descr,
50
                    $this->engine->{"display-name"}, $this->engine->homepage,
51 52
                    $this->counter
                );
53
            }
54
        } catch (\Exception $e) {
Dominik Hebeler's avatar
Dominik Hebeler committed
55
            Log::error("A problem occurred parsing results from $this->name:\n" . $e->getMessage() . "\n" . $result);
56
            return;
57 58
        }
    }
59 60 61

    public function getNext(\App\MetaGer $metager, $result)
    {
62
        $result = preg_replace("/\r\n/si", "", $result);
63
        try {
64
            $content = simplexml_load_string($result);
65 66 67
            if (!$content) {
                return;
            }
Dominik Hebeler's avatar
Dominik Hebeler committed
68
            $resultCount = $content->xpath('//yandexsearch/response/results/grouping/found[@priority="all"]');
69
            if (!$resultCount || sizeof($resultCount) <= 0) {
Dominik Hebeler's avatar
Dominik Hebeler committed
70 71 72
                return;
            }
            $resultCount = intval($resultCount[0]->__toString());
73 74
            $pageLast = $content->xpath('//yandexsearch/response/results/grouping/page')[0];
            $pageLast = intval($pageLast["last"]->__toString());
75 76 77
            if (count($this->results) <= 0 || $pageLast >= $resultCount) {
                return;
            }
78
            $next = new Yandex($this->name, $this->engine, $metager);
79
            $next->getString .= "&page=" . ($metager->getPage() + 1);
80
            $next->hash = md5($next->engine->host . $next->getString . $next->engine->port . $next->name);
81
        } catch (\Exception $e) {
82
            Log::error("A problem occurred parsing results from $this->name:\n" . $e->getMessage() . "\n" . $result);
83 84
            return;
        }
85
    }
Dominik Hebeler's avatar
Dominik Hebeler committed
86
}