MusicParse.class.php 3.33 KB
<?php
/**
 * 解析音频播放地址
 * @name MusicParse
 * @version 1.0(2011-06-21)
 * @package QLib.Utils
 * @author kim@yohoinc.com
 * @since 1.0
 */

class Lib_Utils_MusicParse 
{
	private static $_xiamiFormat = array ('100' => 1, '185' => 2, '55' => 3 );
	
	//http://www.xiami.com/song/1770272568
	//http://www.xiami.com/song/play?ids=/song/playlist/id/1770272566/object_name/default/object_id/0
	/**
	 * 判断是否是音乐接口
	 * 
	 * @param string $url
	 */
	public static function isMusic($url) 
	{
		//判断是否是虾米的链接
		if (strpos ( $url, 'www.xiami.com/song/' ) !== false) 
		{
			return 'xiami';
		}
		return false;
	}
	
	/**
	 * 解析虾米的地址
	 * 
	 * @param string $url
	 * @return array|false array('title' => ,'album' => , 'author' => , 'url' => , 'img' => )
	 */
	public static function parseXiamiUrl($url) 
	{
		//如果已经存在信息,则直接返回
		$shortcode = Lib_Utils_StringHelper::getShortLink ( $url );
		$t = Facade_Blog_Shortlink::getShortlink ( $shortcode );
		$aVinfo = Facade_Blog_Shortlink::getVoice ( $shortcode );
		if (! empty ( $t ) && ! empty ( $aVinfo )) 
		{
			return array ('title' => $aVinfo ['title'], 'url' => $aVinfo ['voice_url'], 'img' => $aVinfo ['img_url'], 'author' => $aVinfo ['author'] );
		}
		//如果不存在信息,直接分析网页
		$id = self::parseXiamiID ( $url );
		if ($id) 
		{
			$sUrl = 'http://www.xiami.com/song/' . $id;
			$ret = self::_parseXiamiContent ( $sUrl );
			if ($ret !== null) 
			{
				$ret ['url'] = $sUrl;
				return $ret;
			} 
			else 
			{
				return false;
			}
		}
		return false;
	}
	/**
	 * 解析虾米的网页结构,得到:歌曲名,封面,专辑,艺术家
	 * 
	 * @param string $url
	 * @return array|null
	 */
	private static function _parseXiamiContent($url) 
	{
		$str = @file_get_contents ( $url );
		$re = '/<div\s*id=[\"|\']{1}title[\"|\']{1}\s*>\s*<h1>(.+)<\/h1>\s*<\/div>';
		$re .= '.+<div\s+class=[\"|\']{1}cover[\"|\']{1}\s*>.+<img.+src=[\"|\']{1}(.+)[\"|\']{1}\s*\/>.+<\/div>';
		$re .= '.+<a\s+href=[\"|\']{1}\/album\/\d+[\"|\']{1}.+>(.+)<\/a>';
		$re .= '.+<a\s+href=[\"|\']{1}\/artist\/\d+[\"|\']{1}.+>(.+)<\/a>/sU';
		
		if ($str && preg_match ( $re, $str, $arr )) 
		{
			return array ('title' => $arr [1], 'img' => self::getXiamiSourceLogo ( $arr [2] ), 'album' => $arr [3], 'author' => $arr [4] );
		}
		return null;
	}
	/**
	 * 解析虾米的链接得到虾米的播放ID
	 * 
	 * @param string $url
	 * @return string|false
	 */
	public static function parseXiamiID($url)
	{
		$reSingle = '/xiami\.com\/song\/(\d+)/';
		$reList = '/xiami\.com\/song\/play\?ids=\/song\/playlist\/id\/(\d+)/';
		if (preg_match ( $reSingle, $url, $arr )) 
		{
			return $arr [1];
		}
		if (preg_match ( $reList, $url, $arr )) 
		{
			return $arr [1];
		}
		return false;
	}
	/**
	 * 根据封面地址得到,封面源地址
	 * @param string $lUrl
	 * @return string
	 */
	public static function getXiamiSourceLogo($lUrl) 
	{
		return preg_replace ( '/_\d{1}\./', '.', $lUrl );
	}
	
	/**
	 * 根据封面源地址,得到不同规格的图片
	 * 
	 * @param string $url
	 * @param string $format 55, 100, 185
	 * @return string
	 */
	public static function getXiamiImage($url, $format = '100x100')
	{
		$name = basename ( $url );
		$nname = str_replace ( '.', '_' . self::$_xiamiFormat [$format] . '.', $name );
		return str_replace ( $name, $nname, $url );
	}
}