OvertureAds.php 4.02 KB
Newer Older
1
2
3
4
5
<?php

namespace App\Models\parserSkripte;

use App\Models\Searchengine;
6
use Log;
7
8
9
10

class OvertureAds extends Searchengine
{

11
    public function __construct($name, \StdClass $engine, \App\MetaGer $metager)
12
    {
13
        parent::__construct($name, $engine, $metager);
14
15
16
        # We need some Affil-Data for the advertisements
        $this->getString .= $this->getOvertureAffilData($metager->getUrl());
        $this->hash = md5($this->engine->host . $this->getString . $this->engine->port . $this->name);
17
18
19
20
21
22
23
    }

    public function loadResults($result)
    {
        $result = preg_replace("/\r\n/si", "", $result);
        try {
            $content = simplexml_load_string($result);
24
25
26
27
28
29
            if (!$content) {
                return;
            }

            $ads = $content->xpath('//Results/ResultSet[@id="searchResults"]/Listing');
            foreach ($ads as $ad) {
30
31
                $title = $ad["title"];
                $link = $ad->{"ClickUrl"}->__toString();
32
                $anzeigeLink = $ad["siteHost"];
33
                $descr = $ad["description"];
34
35
36
37
38
39
40
                $this->counter++;
                $this->ads[] = new \App\Models\Result(
                    $this->engine,
                    $title,
                    $link,
                    $anzeigeLink,
                    $descr,
41
42
43
44
                    $this->engine->{"display-name"},
                    $this->engine->{"homepage"},
                    $this->counter,
                    []
45
46
                );
            }
47
        } catch (\Exception $e) {
48
49
            Log::error("A problem occurred parsing results from $this->name:");
            Log::error($e->getMessage());
50
51
52
53
            return;
        }
    }

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
92
93
94
95
96
97
98
    public function getNext(\App\MetaGer $metager, $result)
    {
        $result = preg_replace("/\r\n/si", "", $result);
        try {
            $content = simplexml_load_string($result);
            if (!$content) {
                return;
            }
        } catch (\Exception $e) {
            Log::error("A problem occurred parsing results from $this->name:");
            Log::error($e->getMessage());
            return;
        }

        if (!$content) {
            return;
        }

        // Yahoo liefert, wenn es keine weiteren Ergebnisse hat immer wieder die gleichen Ergebnisse
        // Wir müssen also überprüfen, ob wir am Ende der Ergebnisse sind
        $resultCount = $content->xpath('//Results/ResultSet[@id="inktomi"]/MetaData/TotalHits');
        $results = $content->xpath('//Results/ResultSet[@id="inktomi"]/Listing');
        if (isset($resultCount[0]) && sizeof($results) > 0) {
            $resultCount = intval($resultCount[0]->__toString());
            $lastResultOnPage = intval($results[sizeof($results) - 1]["rank"]);
            if ($resultCount <= $lastResultOnPage) {
                return;
            }
        } else {
            return;
        }

        $nextArgs = $content->xpath('//Results/NextArgs');
        if (isset($nextArgs[0])) {
            $nextArgs = $nextArgs[0]->__toString();
        } else {
            $nextArgs = $content->xpath('//Results/ResultSet[@id="inktomi"]/NextArgs');
            if (isset($nextArgs[0])) {
                $nextArgs = $nextArgs[0]->__toString();
            } else {
                return;
            }
        }

        # Erstellen des neuen Suchmaschinenobjekts und anpassen des GetStrings:
Aria Givi's avatar
Aria Givi committed
99
        $next = new OvertureAds($this->name, $this->engine, $metager);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
        $next->getString = preg_replace("/&Keywords=.*?&/si", "&", $next->getString) . "&" . $nextArgs;
        $next->hash = md5($next->engine->host . $next->getString . $next->engine->port . $next->name);
        $this->next = $next;
    }

    # Liefert Sonderdaten für Yahoo
    private function getOvertureAffilData($url)
    {
        $affil_data = 'ip=' . $this->ip;
        $affil_data .= '&ua=' . $this->useragent;
        $affilDataValue = $this->urlEncode($affil_data);
        # Wir benötigen die ServeUrl:
        $serveUrl = $this->urlEncode($url);

        return "&affilData=" . $affilDataValue . "&serveUrl=" . $serveUrl;
    }

117
}