Quicktips.php 5.48 KB
Newer Older
1
2
3
4
<?php

namespace App\Models\Quicktips;

Dominik Hebeler's avatar
Dominik Hebeler committed
5
use Cache;
6
7
8
9
10
11
12
13
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Redis;
use Log;

class Quicktips
{
    use DispatchesJobs;

14
    private $quicktipUrl = "/1.1/quicktips.xml";
15
    const QUICKTIP_NAME = "quicktips";
16
17
18
19
20
21
    const CACHE_DURATION = 60;

    private $hash;

    public function __construct($search, $locale, $max_time)
    {
22
        if (env("APP_ENV") === "production") {
23
            $this->quicktipUrl = "https://quicktips.metager.de" . $this->quicktipUrl;
24
        } else {
25
            $this->quicktipUrl = "https://dev.quicktips.metager.de" . $this->quicktipUrl;
26
        }
27
28
29
30
31
        $this->startSearch($search, $locale, $max_time);
    }

    public function startSearch($search, $locale, $max_time)
    {
32
        $url = $this->quicktipUrl . "?search=" . $this->normalize_search($search) . "&locale=" . $locale;
33
        $this->hash = md5($url);
34

Dominik Hebeler's avatar
Dominik Hebeler committed
35
        if (!Cache::has($this->hash)) {
36
37

            // Queue this search
Dominik Hebeler's avatar
Dominik Hebeler committed
38
39
40
41
42
43
44
45
46
47
48
49
            $mission = [
                "resulthash" => $this->hash,
                "url" => $url,
                "username" => null,
                "password" => null,
                "headers" => [],
                "cacheDuration" => self::CACHE_DURATION,
            ];

            $mission = json_encode($mission);

            Redis::rpush(\App\MetaGer::FETCHQUEUE_KEY, $mission);
50
51
52
        }
    }

53
54
55
56
57
58
    /**
     * Load the current Quicktip results
     * 1. Retrieve the raw results
     * 2. Parse the results
     * Returns an empty array if no results are found
     */
59
60
61
    public function loadResults()
    {
        $resultsRaw = $this->retrieveResults($this->hash);
62
63
64
65
66
67
        if ($resultsRaw) {
            $results = $this->parseResults($resultsRaw);
            return $results;
        } else {
            return [];
        }
68
69
70
71
    }

    public function retrieveResults($hash)
    {
Dominik Hebeler's avatar
Dominik Hebeler committed
72
73
74
75
76
        $body = null;

        if (Cache::has($this->hash)) {
            $body = Cache::get($this->hash);
        }
77

Dominik Hebeler's avatar
Dominik Hebeler committed
78
        if ($body !== null) {
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
            return $body;
        } else {
            return false;
        }
    }

    public function parseResults($quicktips_raw)
    {
        $quicktips_raw = preg_replace("/\r\n/si", "", $quicktips_raw);
        try {
            $content = simplexml_load_string($quicktips_raw);
            if (!$content) {
                return;
            }

            $content->registerXPathNamespace('main', 'http://www.w3.org/2005/Atom');

            $quicktips = [];

            $quicktips_xpath = $content->xpath('main:entry');
            foreach ($quicktips_xpath as $quicktip_xml) {
100
101
102
103
104
105
106
107
108
                // Type
                $quicktip_xml->registerXPathNamespace('mg', 'http://metager.de/opensearch/quicktips/');
                $types = $quicktip_xml->xpath('mg:type');
                if (sizeof($types) > 0) {
                    $type = $types[0]->__toString();
                } else {
                    $type = "";
                }

109
110
111
112
113
114
                // Title
                $title = $quicktip_xml->title->__toString();

                // Link
                $link = $quicktip_xml->link['href']->__toString();

115
                // gefVon
116
                $quicktip_xml->registerXPathNamespace('mg', 'http://metager.de/opensearch/quicktips/');
117
118
119
                $gefVonTitles = $quicktip_xml->xpath('mg:gefVonTitle');
                if (sizeof($gefVonTitles) > 0) {
                    $gefVonTitle = $gefVonTitles[0]->__toString();
120
                } else {
121
122
123
124
125
126
127
                    $gefVonTitle = "";
                }
                $gefVonLinks = $quicktip_xml->xpath('mg:gefVonLink');
                if (sizeof($gefVonLinks) > 0) {
                    $gefVonLink = $gefVonLinks[0]->__toString();
                } else {
                    $gefVonLink = "";
128
129
                }

130
131
132
133
134
135
136
137
                $quicktip_xml->registerXPathNamespace('mg', 'http://metager.de/opensearch/quicktips/');
                $author = $quicktip_xml->xpath('mg:author');
                if (sizeof($author) > 0) {
                    $author = $author[0]->__toString();
                } else {
                    $author = "";
                }

138
139
                // Description
                $descr = $quicktip_xml->content->__toString();
140

141
                // Details
142
                $details = [];
143
144
                $details_xpath = $quicktip_xml->xpath('mg:details');
                if (sizeof($details_xpath) > 0) {
145
146
                    foreach ($details_xpath[0] as $detail_xml) {
                        $details_title = $detail_xml->title->__toString();
147
                        $details_link = $detail_xml->url->__toString();
148
                        $details_descr = $detail_xml->text->__toString();
149
                        $details[] = new \App\Models\Quicktips\Quicktip_detail(
150
151
152
153
154
155
156
157
158
159
                            $details_title,
                            $details_link,
                            $details_descr
                        );
                    }
                }
                $quicktips[] = new \App\Models\Quicktips\Quicktip(
                    $type,
                    $title,
                    $link,
160
161
                    $gefVonTitle,
                    $gefVonLink,
162
                    $author,
163
164
165
166
167
168
169
170
171
172
                    $descr,
                    $details
                );
            }
            return $quicktips;
        } catch (\Exception $e) {
            Log::error("A problem occurred parsing quicktips");
            return [];
        }
    }
173
174
175

    public function normalize_search($search)
    {
176
        return urlencode($search);
177
    }
178
}