Kelkoo.php 5.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php

namespace app\Models\parserSkripte;

use App\Models\Searchengine;
use Log;

class Kelkoo extends Searchengine
{
    public $results = [];
Aria Givi's avatar
Aria Givi committed
11
    public $unsignedGetString = "";
12
13
14
15

    public function __construct($name, \StdClass $engine, \App\MetaGer $metager)
    {
        parent::__construct($name, $engine, $metager);
Aria Givi's avatar
Aria Givi committed
16
17
        $this->unsignedGetString = $this->getString;
        $this->getString = $this->UrlSigner($this->unsignedGetString);
18
19
20
21
22
23
        $this->hash = md5($this->engine->host . $this->getString . $this->engine->port . $this->name);
    }

    public function loadResults($result)
    {
        $result = preg_replace("/\r\n/si", "", $result);
24
25
        # delete namespace, allowing easier xpath access
        $result = str_replace('xmlns="urn:yahoo:prods"', '', $result);
26
        try {
Dominik Hebeler's avatar
Dominik Hebeler committed
27
            $content = \simplexml_load_string($result);
28
29
30
            if (!$content) {
                return;
            }
31
32
33
34
35
36
37
38
39
            # Kekloo gives us the total Result Count
            $resultCount = $content->xpath('/ProductSearch/Products/@totalResultsAvailable');
            if (sizeof($resultCount) > 0) {
                $resultCount = intval($resultCount[0]->__toString());
            } else {
                $resultCount = 0;
            }
            $this->totalResults = $resultCount;
            $results = $content->xpath('/ProductSearch/Products/Product/Offer');
40
41

            foreach ($results as $result) {
Dominik Hebeler's avatar
Dominik Hebeler committed
42
43
44
                $result = \simplexml_load_string($result->saveXML());

                $title = $result->Title[0]->__toString();
45
46
47
48
49
50

                $price = floatval($result->Price[0]->Price[0]);
                $deliveryPrice = floatval($result->Price[0]->DeliveryCost[0]);
                $totalPrice = $price + $deliveryPrice;

                $descr = "";
Dominik Hebeler's avatar
Dominik Hebeler committed
51
52

                if (isset($result->Description[0])) {
Aria Givi's avatar
Aria Givi committed
53
                    $descr = $result->Description[0]->__toString();
54
                }
Dominik Hebeler's avatar
Dominik Hebeler committed
55
56
57
                $descr .= "<p>Preis: " . $price . " €</p>";
                $image = $result->Images[0]->Image[0]->Url[0]->__toString();
                $link = $result->Url[0]->__toString();
58
                $anzeigeLink = $result->Merchant[0]->Name[0]->__toString();
59
60
61
62
63
64
65
                $this->counter++;
                $this->results[] = new \App\Models\Result(
                    $this->engine,
                    $title,
                    $link,
                    $anzeigeLink,
                    $descr,
Dominik Hebeler's avatar
Dominik Hebeler committed
66
                    $this->engine->{"display-name"}, $this->engine->homepage,
67
                    $this->counter,
68
                    ['image' => $image,
Dominik Hebeler's avatar
Dominik Hebeler committed
69
                        'price' => $totalPrice * 100]
70
71
72
73
74
75
76
77
78
                );
            }
        } catch (\Exception $e) {
            Log::error("A problem occurred parsing results from $this->name:");
            Log::error($e->getMessage());
            return;
        }
    }

79
80
    public function getNext(\App\MetaGer $metager, $result)
    {
Aria Givi's avatar
Aria Givi committed
81
82
83
84
        $result = preg_replace("/\r\n/si", "", $result);
        # delete namespace, allowing easier xpath access
        $result = str_replace('xmlns="urn:yahoo:prods"', '', $result);
        try {
Dominik Hebeler's avatar
Dominik Hebeler committed
85
            $content = \simplexml_load_string($result);
Aria Givi's avatar
Aria Givi committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
            if (!$content) {
                return;
            }
        } catch (\Exception $e) {
            Log::error("A problem occurred parsing results from $this->name:");
            Log::error($e->getMessage());
            return;
        }
        # Kekloo gives us the total Result Count
        $resultCount = $content->xpath('/ProductSearch/Products/@totalResultsAvailable');
        if (sizeof($resultCount) > 0) {
            $resultCount = intval($resultCount[0]->__toString());
        } else {
            $resultCount = 0;
        }
        $this->totalResults = $resultCount;

        // Get the current Result Position
        $current = 0;
        if (strpos($this->getString, "&start=") !== false) {
            $tmp = substr($this->getString, strpos($this->getString, "&start=") + 7);
            if (strpos($tmp, "&") !== false) {
                $tmp = substr($tmp, 0, strpos($tmp, "&"));
            }
            $current = intval($tmp);
        }

        if ($current >= ($this->totalResults - 20)) {
            return;
        }

        # Erstellen des neuen Suchmaschinenobjekts und anpassen des GetStrings:
        $next = new Kelkoo($this->name, $this->engine, $metager);
        $next->unsignedGetString .= "&start=" . ($current + 20);
        $next->getString = $next->UrlSigner($next->unsignedGetString);
Dominik Hebeler's avatar
Dominik Hebeler committed
121

Aria Givi's avatar
Aria Givi committed
122
123
        $next->hash = md5($next->engine->host . $next->getString . $next->engine->port . $next->name);
        $this->next = $next;
124
125
    }

126
    # kelkoogroup.com/kelkoo-customer-service/kelkoo-developer-network/shopping-services/samples/signing-url-php/
Dominik Hebeler's avatar
Dominik Hebeler committed
127
128
129
    public function UrlSigner($path)
    {

Aria Givi's avatar
Aria Givi committed
130
        $urlPath = $path;
131
132
133
134
135
        $partner = $this->engine->{"http-auth-credentials"}->ID;
        $key = $this->engine->{"http-auth-credentials"}->Key;

        $URL_sig = "hash";
        $URL_ts = "timestamp";
Dominik Hebeler's avatar
Dominik Hebeler committed
136
        $URL_partner = "aid";
137
138
139
140
141
142
143
144
        $URLreturn = "";
        $URLtmp = "";
        $s = "";
        // get the timestamp
        $time = time();
        // replace " " by "+"
        $urlPath = str_replace(" ", "+", $urlPath);
        // format URL
Aria Givi's avatar
Aria Givi committed
145
        $URLtmp = $urlPath . "&" . $URL_partner . "=" . $partner . "&" . $URL_ts . "=" . $time;
Dominik Hebeler's avatar
Dominik Hebeler committed
146

147
148
149
150
151
152
153
154
155
156
        // URL needed to create the token
        $s = $urlPath . "&" . $URL_partner . "=" . $partner . "&" . $URL_ts . "=" . $time . $key;
        $token = "";
        $token = base64_encode(pack('H*', md5($s)));
        $token = str_replace(array("+", "/", "="), array(".", "_", "-"), $token);
        $URLreturn = $URLtmp . "&" . $URL_sig . "=" . $token;
        return $URLreturn;

    }
}