From a5eb02d3c37df76bde655885609be2382f971fee Mon Sep 17 00:00:00 2001 From: quickwick <2566133+quickwick@users.noreply.github.com> Date: Sun, 3 Apr 2022 00:51:41 -0700 Subject: [PATCH] [MixcloudBridge] switch to using API (#2591) * switch to using public API * switch to different API endpoints * fix: urlencode username Co-authored-by: Dag --- bridges/MixCloudBridge.php | 40 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/bridges/MixCloudBridge.php b/bridges/MixCloudBridge.php index c1534ec9..2296af2f 100644 --- a/bridges/MixCloudBridge.php +++ b/bridges/MixCloudBridge.php @@ -5,6 +5,7 @@ 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'; @@ -24,28 +25,37 @@ class MixCloudBridge extends BridgeAbstract { return parent::getName(); } + private static function compareDate($stream1, $stream2) { + return (strtotime($stream1['timestamp']) < strtotime($stream2['timestamp']) ? 1 : -1); + } + public function collectData(){ - $html = getSimpleHTMLDOM(self::URI . '/' . $this->getInput('u')); + $user = urlencode($this->getInput('u')); + // Get Cloudcasts + $mixcloudUri = self::API_URI . $user . '/cloudcasts/'; + $content = getContents($mixcloudUri); + $casts = json_decode($content)->data; - foreach($html->find('section.card') as $element) { + // 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 = array(); - $item['uri'] = self::URI . $element->find('hgroup.card-title h1 a', 0)->getAttribute('href'); - $item['title'] = html_entity_decode( - $element->find('hgroup.card-title h1 a span', 0)->getAttribute('title'), - ENT_QUOTES - ); - - $image = $element->find('a.album-art img', 0); - - if($image) { - $item['content'] = ''; - } - - $item['author'] = trim($element->find('hgroup.card-title h2 a', 0)->innertext); + $item['uri'] = $stream->url; + $item['title'] = $stream->name; + $item['content'] = ''; + $item['author'] = $stream->user->name; + $item['timestamp'] = $stream->created_time; $this->items[] = $item; } + + // Sort items by date + usort($this->items, array('MixCloudBridge', 'compareDate')); } }