aboutsummaryrefslogtreecommitdiff
path: root/bridges/AlfaBankByBridge.php
blob: 7c13c14dbfafa448c1b45442d2634687b0bc0ded (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php

class AlfaBankByBridge extends BridgeAbstract
{
    const MAINTAINER = 'lassana';
    const NAME = 'AlfaBank.by Новости';
    const URI = 'https://www.alfabank.by';
    const DESCRIPTION = 'Уведомления Alfa-Now — новости от Альфа-Банка';
    const CACHE_TIMEOUT = 3600; // 1 hour
    const PARAMETERS = [
        'News' => [
            'business' => [
                'name' => 'Альфа Бизнес',
                'type' => 'list',
                'title' => 'В зависимости от выбора, возращает уведомления для" .
					" клиентов физ. лиц либо для клиентов-юридических лиц и ИП',
                'values' => [
                    'Новости' => 'news',
                    'Новости бизнеса' => 'newsBusiness'
                ],
                'defaultValue' => 'news'
            ],
            'fullContent' => [
                'name' => 'Включать содержимое',
                'type' => 'checkbox',
                'title' => 'Если выбрано, содержимое уведомлений вставляется в поток (работает медленно)'
            ]
        ]
    ];

    public function collectData()
    {
        $business = $this->getInput('business') == 'newsBusiness';
        $fullContent = $this->getInput('fullContent') == 'on';

        $mainPageUrl = self::URI . '/about/articles/uvedomleniya/';
        if ($business) {
            $mainPageUrl .= '?business=true';
        }
        $html = getSimpleHTMLDOM($mainPageUrl);
        $limit = 0;

        foreach ($html->find('a.notifications__item') as $element) {
            if ($limit < 10) {
                $item = [];
                $item['uid'] = 'urn:sha1:' . hash('sha1', $element->getAttribute('data-notification-id'));
                $item['title'] = $element->find('div.item-title', 0)->innertext;
                $item['timestamp'] = DateTime::createFromFormat(
                    'd M Y',
                    $this->ruMonthsToEn($element->find('div.item-date', 0)->innertext)
                )->getTimestamp();

                $itemUrl = self::URI . $element->href;
                if ($business) {
                    $itemUrl = str_replace('?business=true', '', $itemUrl);
                }
                $item['uri'] = $itemUrl;

                if ($fullContent) {
                    $itemHtml = getSimpleHTMLDOM($itemUrl);
                    if ($itemHtml) {
                        $item['content'] = $itemHtml->find('div.now-p__content-text', 0)->innertext;
                    }
                }

                $this->items[] = $item;
                $limit++;
            }
        }
    }

    public function getIcon()
    {
        return static::URI . '/local/images/favicon.ico';
    }

    private function ruMonthsToEn($date)
    {
        $ruMonths = [
            'Января', 'Февраля', 'Марта', 'Апреля', 'Мая', 'Июня',
            'Июля', 'Августа', 'Сентября', 'Октября', 'Ноября', 'Декабря' ];
        $enMonths = [
            'January', 'February', 'March', 'April', 'May', 'June',
            'July', 'August', 'September', 'October', 'November', 'December' ];
        return str_replace($ruMonths, $enMonths, $date);
    }
}