summaryrefslogtreecommitdiff
path: root/src/App.php
blob: 317eb2b137b9ba59693bf4a29166df1eb3f8c7dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
<?php

namespace FBeans\Blamarine;

use FBeans\BlaIRC\Command;
use FBeans\BlaIRC\Application As BlaApplication;
use PHPHtmlParser\Dom;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


class App extends BlaApplication
{
    public function commands(): Array
    {
        $command = new Command;
        $command->setName('marine');

        // argument 0 is the command name
        $command->addArgument(1);

        $command->setExecuteCallback(
            function (InputInterface $input, OutputInterface $output) {
                $ship_id = $input->getArgument(1);

                $url = $this->getVesselUrl($ship_id);

                $dom = new Dom;
                $dom->loadFromUrl($url);

                $html = $dom->find(
                    ".column.vfix-top.npr table tbody"
                );

                $out = [];

                foreach ($html as $inner_elem) {
                    $elem_dom = new Dom();
                    $item = $elem_dom->loadStr($inner_elem);


                    $out = '';
                    $index = 0;
                    while ($data = $item->find('tr td', $index)) {
                        $value = $item->find('tr td', $index+1)->innerHtml();
                        $out .= strip_tags($data->innerHtml()) . ': ' . strip_tags($value) . ' ';

                        $index += 2;
                    }

                    $output->writeLn(rtrim($out));
                }

                return Command::SUCCESS;
            }
        );

        return [$command];
    }

    private function getVesselUrl(string $ship_id): string
    {
        return "https://www.vesselfinder.com/vessels/" . $ship_id;
    }
}