Allesklar.php 1.98 KB
Newer Older
1
2
3
4
5
<?php

namespace App\Models\parserSkripte;

use App\Models\Searchengine;
6
use Log;
7
8
9
10
use Symfony\Component\DomCrawler\Crawler;

class Allesklar extends Searchengine
{
11
12
13
14
15
16
17
18
19
20
21
22
    protected $tds = "";
    public function __construct(\SimpleXMLElement $engine, \App\MetaGer $metager)
    {
        parent::__construct($engine, $metager);
    }

    public function loadResults($result)
    {
        $crawler = new Crawler(utf8_decode($result));
        $crawler = $crawler
            ->filter('table[width=585]')
            ->reduce(function (Crawler $node, $i) {
23
24
                // The first 5 elements are additional information
                return $i >= 5;
25
26
            });
        $this->counter = 0;
27
28
29
30
31
32
33
34
        $crawler->each(function (Crawler $node, $i) {
            // Only the first 20 elements are actual search results
            if ($i < 20) {
                try {
                    $titleTag = $node->filter('tr > td > a.katalogtitel')->first();
                    $title    = trim($titleTag->text());
                    $link     = $titleTag->attr('href');
                    // Sometimes the description is in the 3rd element
35
                    $descr = trim($node->filter('tr > td.bodytext')->eq(2)->text());
36
37
38
39
40
41
42
43
44
45
                    if (strlen($descr) <= 2) {
                        $descr = trim($node->filter('tr > td.bodytext')->eq(3)->text());
                    }
                    $this->counter++;
                    $this->results[] = new \App\Models\Result(
                        $this->engine,
                        $title,
                        $link,
                        $link,
                        $descr,
46
                        $this->displayName,$this->homepage,
47
48
49
50
51
52
                        $this->counter
                    );
                } catch (\Exception $e) {
                    Log::error("A problem occurred parsing results from $this->name:");
                    Log::error($e->getMessage());
                    return;
53
54
55
56
57
58
                }
            }
        });
    }

}