summaryrefslogtreecommitdiff
path: root/providers/Untappd.php
blob: 95c7ab7f662fa1fbf8f584c5321eb1a82dd46074 (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
66
<?php

namespace Providers;

require_once('HtmlFeed.php');
require_once('Item.php');

class Untappd extends \Providers\HtmlFeed {

    protected $_cacheTimeout = '5 minutes';

    protected function _getCachePath() {
        return '../cache/untappd.%s';
    }

    protected function _getFeedUrl($feed) {
        return sprintf('https://untappd.com/%s/beer?sort=created_at_desc', $feed);
    }

    protected function _parseFeedContent($tree) {
        $items = [];
        $parents = [];
        foreach ($tree->find('div.beer-item') as $div) {
            $details = $div->find('div.beer-details, div.details');
            if ($details) {
                $texts = [
                    $details->find('p.name')->text(),
                    $details->find('p.style')->text(),
                    $details->find('p.desc')->eq(1)->text(),
                    $details->find('p.abv')->text(),
                    $details->find('p.ibu')->text()
                ];
                $items[] = [
                    'id' => $div->attr()['data-bid'],
                    'link' => $div->find('a.label')->attr()['href'],
                    'name' => $texts[0],
                    'texts' => $texts,
                    'time' => trim($details->find('p.date')->text())
                ];
            }
        }
        return array_values($items);
    }

    protected function _mapItems($content) {
        return array_map(
            function ($obj) {
                $item = new Item();
                $item->ID = $obj['id'];
                $item->Link = sprintf('https://untappd.com/%s', $obj['link']);
                $item->Title = sprintf('New beer by %s: %s', $this->_feed, $obj['name']);
                $item->Text = implode('<br />', $obj['texts']);
                $item->Time = strtotime(str_replace('Added ', '', $obj['time']) . ' 00:00:00');
                return $item;
            },
            $content
        );
    }

    public function title() {
        return sprintf("%s's untappd beer list", $this->_feed);
    }

}

?>