aboutsummaryrefslogtreecommitdiff
path: root/bridges/MixCloudBridge.php
blob: d2b35989855c2115c86d17deb783b56154033106 (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
<?php

class MixCloudBridge extends BridgeAbstract
{
    const MAINTAINER = 'Alexis CHEMEL';
    const NAME = 'MixCloud';
    const URI = 'https://www.mixcloud.com';
    const API_URI = 'https://api.mixcloud.com/';
    const CACHE_TIMEOUT = 3600; // 1h
    const DESCRIPTION = 'Returns latest musics on user stream';

    const PARAMETERS = [[
        'u' => [
            'name' => 'username',
            'required' => true,
            'exampleValue' => 'DJJazzyJeff',
        ]
    ]];

    public function getName()
    {
        if (!is_null($this->getInput('u'))) {
            return 'MixCloud - ' . $this->getInput('u');
        }

        return parent::getName();
    }

    private static function compareDate($stream1, $stream2)
    {
        return (strtotime($stream1['timestamp']) < strtotime($stream2['timestamp']) ? 1 : -1);
    }

    public function collectData()
    {
        $user = urlencode($this->getInput('u'));
        // Get Cloudcasts
        $mixcloudUri = self::API_URI . $user . '/cloudcasts/';
        $content = getContents($mixcloudUri);
        $casts = json_decode($content)->data;

        // Get Listens
        $mixcloudUri = self::API_URI . $user . '/listens/';
        $content = getContents($mixcloudUri);
        $listens = json_decode($content)->data;

        $streams = array_merge($casts, $listens);

        foreach ($streams as $stream) {
            $item = [];

            $item['uri'] = $stream->url;
            $item['title'] = $stream->name;
            $item['content'] = '<img src="' . $stream->pictures->thumbnail . '" />';
            $item['author'] = $stream->user->name;
            $item['timestamp'] = $stream->created_time;

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

        // Sort items by date
        usort($this->items, ['MixCloudBridge', 'compareDate']);
    }
}