Bing.php 2.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php

namespace app\Models\parserSkripte;

use App\Models\Searchengine;
use Log;

class Bing extends Searchengine
{
    public $results = [];

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

    public function loadResults($result)
    {
        try {
            $results = json_decode($result);
            $this->totalResults = $results->webPages->totalEstimatedMatches;
22 23 24 25 26 27 28

            # Check if the query got altered
            if (!empty($results->{"queryContext"}) && !empty($results->{"queryContext"}->{"alteredQuery"}) && !empty($results->{"queryContext"}->{"alterationOverrideQuery"})) {
                $this->alteredQuery = $results->{"queryContext"}->{"alteredQuery"};
                $this->alterationOverrideQuery = $results->{"queryContext"}->{"alterationOverrideQuery"};
            }

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
            $results = $results->webPages->value;

            foreach ($results as $result) {
                $title = $result->name;
                $link = $result->url;
                $anzeigeLink = $result->displayUrl;
                $descr = $result->snippet;
                $this->counter++;
                $this->results[] = new \App\Models\Result(
                    $this->engine,
                    $title,
                    $link,
                    $anzeigeLink,
                    $descr,
                    $this->engine->{"display-name"}, $this->engine->homepage,
                    $this->counter,
                    []
                );

            }

        } catch (\Exception $e) {
            Log::error("A problem occurred parsing results from $this->name:");
            Log::error($e->getMessage());
            return;
        }
    }

    public function getNext(\App\MetaGer $metager, $result)
    {
        try {
            $results = json_decode($result);

            $totalMatches = $results->webPages->totalEstimatedMatches;

            $newEngine = unserialize(serialize($this->engine));

            $perPage = $newEngine->{"get-parameter"}->count;

            $offset = 0;
            if (empty($newEngine->{"get-parameter"}->offset)) {
                $offset = $perPage;
            } else {
                $offset = $newEngine->{"get-parameter"}->offset + $perPage;
            }

            if ($totalMatches < ($offset + $perPage)) {
                return;
            } else {
                $newEngine->{"get-parameter"}->offset = $offset;
            }

            $next = new Bing($this->name, $newEngine, $metager);
            $this->next = $next;

        } catch (\Exception $e) {
            Log::error("A problem occurred parsing results from $this->name:");
            Log::error($e->getMessage());
            return;
        }

    }
}