MusicParse.class.php
3.33 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?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 );
}
}