Skip to content

Instantly share code, notes, and snippets.

@mgng
Created December 15, 2008 05:10
Show Gist options
  • Save mgng/35892 to your computer and use it in GitHub Desktop.
Save mgng/35892 to your computer and use it in GitHub Desktop.
myspace mp3 / flv loader
<?php
/**
* Misc_Lib_Myspace class
*
* myspace url/id ⇒ get mp3/flv video
*
* [usage_1 set id]
* $ms = new Misc_Lib_Myspace();
* $ms->setId('britneyspears');
* $ms->load();
* $mp3 = $ms->getMp3();
* $flv = $ms->getVideo();
*
* [usage_2 set url]
* $ms = new Misc_Lib_Myspace();
* $ms->setUrl('http://www.myspace.com/britneyspears');
* $ms->load();
* $mp3 = $ms->getMp3();
* $flv = $ms->getVideo();
*
* [usage_3 other]
* $ms = new Misc_Lib_Myspace();
* $mp3 = $ms->setId('britneyspears')->load()->getMp3();
*/
class Misc_Lib_Myspace
{
const VERSION = '0.1';
const MYSPACE_BASE_URL = 'http://www.myspace.com/';
const MYSPACE_BASE_MUSIC_PATH = 'http://mediaservices.myspace.com/services/media/musicplayerxml.ashx?b=';
const MYSPACE_BASE_FLV_PATH = 'http://mediaservices.myspace.com/services/rss.ashx?type=video&friendID=';
const MYSPACE_CHK_REG = '/^http\:\/\/[a-z]+\.myspace\.com\/[a-z0-9]+$/i';
private $_id = null;
private $_url = null;
private $_friendId = null;
private $_userImgPath = null;
private $_src = null;
protected function _getFriendId() {
$p = '/\"DisplayFriendId\"\:([0-9]+)/i';
preg_match($p, $this->_src, $m);
$this->_friendId = isset($m[1])? $m[1] : false;
return $this;
}
protected function _getUserImage() {
$p = '/DefaultImage.+\<img src=\"(http\:\/\/.+\.(jpg|gif|png))\"/i';
preg_match($p, $this->_src, $m);
$this->_userImgPath = isset($m[1])? $m[1] : false;
return $this;
}
public function setUrl($url) {
$this->_url = $url;
return $this;
}
public function setId($id) {
$this->_id = $id;
return $this;
}
public function load() {
if ($this->_url === null && $this->_id === null) {
throw new Exception('initialized error...');
}
$this->_url = ($this->_id !== null) ? self::MYSPACE_BASE_URL . $this->_id : $this->_url;
if (preg_match(self::MYSPACE_CHK_REG, $this->_url) === 0 ||
($this->_src = @file_get_contents($this->_url)) === false) {
throw new Exception($this->_url . ' load error ...');
}
$this->_getFriendId();
$this->_getUserImage();
if ($this->_friendId === false) {
throw new Exception('friend id error ...');
}
return $this;
}
public function getSrc() {
return $this->_src;
}
public function getFriendId() {
return $this->_friendId;
}
public function getUserImagePath() {
return $this->_userImgPath;
}
public function getMp3() {
$url = self::MYSPACE_BASE_MUSIC_PATH . $this->_friendId;
$src = str_replace('encoding="iso-8859-1"', 'encoding="UTF-8"', @file_get_contents($url)); // おまじない
$obj = @simplexml_load_string($src);
if ($obj === false || !isset($obj->playlist)) {
return false;
}
$data = array(
'name' => isset($obj->name)? (string)$obj->name : '',
'songs' => array()
);
foreach($obj->playlist->children() as $song) {
$title = (string)$song['title'];
$img = (string)$song['imagename'];
$alt = (string)$song['imagedesc'];
$durl = (string)$song['durl'];
$durlFull = str_replace('/std_', '/full_', $durl);
$data['songs'][] = array(
'title' => $title,
'img' => $img,
'alt' => $alt,
'durlStd' => $durl,
'durlFul' => $durlFull
);
}
return (count($data['songs']) > 0) ? $data : false;
}
public function getVideo() {
$obj = @simplexml_load_file(self::MYSPACE_BASE_FLV_PATH . $this->_friendId);
if ($obj === false && !isset($obj->channel->item)) {
return false;
}
$data = array();
foreach ($obj->channel->item as $item) {
$title = @$item->xpath('title');
$thum = @$item->xpath('media:thumbnail/@url');
$flv = @$item->xpath('media:content/@url');
if ($title !== false && $flv !== false) {
$data[] = array(
'title' => (string)$title[0],
'thum' => ($thum !== false)? (string)$thum[0] : '',
'flv' => (string)$flv[0]['url']
);
}
}
return (count($data)>0) ? $data : false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment