Icecast 歌曲历史

如何解决Icecast 歌曲历史

我正在 wordpress 上开发一个广播流媒体网站。我发现下面的脚本应该能够完成这项工作。但是我无法弄清楚如何在我的网站中实现这个脚本,以便它显示一个简短的播放列表历史。有人可以启发我对脚本进行哪些更改吗?

我的 icecast 流网址是这样的: http://server-26.stream-server.nl:8812/stream

提前致谢。

米兰

<?PHP

    /**
     * Class IceCastPlaylist
     * See https://stackoverflow.com/questions/60502076/how-to-get-icecast-server-songs-history/60798774#60798774 for context
     * 
     * This class can be used as follows:
     * global $wpdb
     * $playlistClass = new IceCastPlaylist($wpdb);
     * $playlistClass->fetchSongInfo();
     * $playlist = $songInfoClass->getPlaylist();
     * header('Content-Type: application/json;charset=utf-8');
     * echo $playlist;
     * die();
     */
    class IceCastPlaylist {
    
        /**
         * @var wpdb
         */
        protected $db;
    
        /**
         * @var string
         */
        protected $table = '[replace with playlist table]';
    
    
        /**
         * Replace this with your server URL
         * @var string
         */
        protected $songDataUrl = 'http://server-26.stream-server.nl:8812/stream';
    
        /**
         * @var string
         */
        protected $albumCoverUrl = 'https://itunes.apple.com/search?entity=musicTrack&term=';
    
        /**
         * Replace this with your storage path
         * @var string
         */
        protected $albumCoverPath = '[absolute_path_to_cover_images_folder e.g /media/covers/]';
    
        /**
         * Replace with your site URL
       * Most of the time you will want to set the URL to the site dynamically,* so we do this in the constructor
         */
        protected $siteUrl = 'https://stamcafe.compra.nl';
    
        /**
         * Replace this with the desired length of your playlist
         * @var int
         */
        protected $playlistLength = 10;
    
        /**
         * IceCastPlaylist constructor.
         * In this case,I used wordpress wpdb,but it can be replaced with another DB instance
       * When using something else than wpdb,the query functions will need to be adjusted
         *
         * @param $db
         */
        public function __construct(wpdb $db) {
            $this->db = $db;
        // If you need to set the site URL via a function,you can set it here in the constructor
            $this->siteUrl = get_site_url() . '/wp-content/media/covers/';
        }
    
        public function getPlaylist() {
            $result = $this->db->get_results("SELECT * FROM {$this->table} as songs ORDER BY `songs`.`id` DESC LIMIT {$this->playlistLength}");
            return json_encode($result);
        }
    
        /**
         * [
         *  'playedat' => 'H:i',*  'title' => 'title',*  'artist' => 'bla',*  'coverImage' => 'linkToCoverArt',*  'created_at' => 'Y-m-d',* ]
         * @param array $entry
         */
        public function storeEntry(array $entry) {
            $this->db->insert($this->table,$entry);
        }
    
        /**
         * @return mixed
         * @throws Exception
         */
        public function fetchSongInfo() {
            $rawData = file_get_contents($this->songDataUrl);
            $Now = new DateTime('Now');
            if(!$rawData) {
                throw new Exception('Error retrieving song data');
            }
    
            $xml = new SimpleXMLElement($rawData);
            $titleArtist = preg_split("/\s-\s/",(string) $xml->trackList->track->title);
    
            if($this->isNewSong($titleArtist[1])) {
    
                $stream['playedAt'] = $Now->format('H:i');
                $stream['title'] = $titleArtist[1];
                $stream['artist'] = $titleArtist[0];
                $stream['coverImage'] = $this->getCoverArt($xml->trackList->track->title);
                $stream['created_at'] = $Now->format('Y-m-d H:i:s');
    
                $this->storeEntry($stream);
            }
        }
    
        /**
         * Use in your daily cleanup cron
         */
        public function dailyCleanup() {
            $this->db->query("TruncATE TABLE {$this->table}");
        }
    
        /**
         * @param string $title
         *
         * @return bool
         */
        private function isNewSong(string $title) {
            $result = $this->db->get_results("SELECT * FROM {$this->table} as songs ORDER BY `songs`.`id` DESC LIMIT 1");
            if(empty($result)) {
                return true;
            }
    
            return $result[0]->title !== $title;
        }
    
        /**
         * Take the raw 'songtitle - artist' and try to find cover art
         * @param string $title
         *
         * @return string|null
         */
        private function getCoverArt(string $title) {
            $itunesData = wp_remote_get($this->albumCoverUrl . urlencode($title),array(
                'timeout' => 45,'redirection' => 5,'httpversion' => '1.0','blocking' => true,'headers' => array(
                    'accept-encoding' => 'gzip,deflate,br','pragma' => 'no-cache','accept-language' => 'en-US,en;q=0.8','user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/60.0.3112.101 Safari/537.36','accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
                ),'cookies' => array(),));
    
            $songInfo = json_decode(trim(wp_remote_retrieve_body($itunesData)));
            $imageHash = '';
            if(isset($songInfo->results[0])) {
                $coverImage = str_replace('100x100bb.jpg','',$songInfo->results[0]->artworkUrl100);
                $imageHash = md5($title) . '.jpg';
                if(!file_exists($this->albumCoverPath . $imageHash) && $coverImage !== '') {
                    $img = file_get_contents($coverImage . '200x200bb.jpg');
                    file_put_contents($this->albumCoverPath . $imageHash,$img);
                }
            }
    
            return (empty($imageHash)) ? null :  $this->siteUrl . $imageHash;
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?