Skip to content

Commit

Permalink
Fix specs and bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshartig committed Jul 28, 2011
1 parent a04275a commit 3fde7c0
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 44 deletions.
93 changes: 68 additions & 25 deletions gsAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ public function authenticateUser(gsUser $user){
}
}

if (!$user->getUsername() || !$user->getToken()) {
if ((!$user->getUsername() && !$user->getEmail()) || !$user->getToken()) {
return false;
}

$return = self::apiCall('authenticate',array('username'=>$user->getUsername(), 'password'=>$user->getToken(), 'sessionID'=>$this->session), true);
$return = self::apiCall('authenticate',array('login'=>($user->getUsername() ? $user->getUsername() : $user->getEmail()),
'password'=>$user->getToken(), 'sessionID'=>$this->session), true);
if (isset($return['decoded']['result']['UserID']) && $return['decoded']['result']['UserID'] > 0) {
$user->importUserData($return['decoded']['result']);
$this->sessionUserid = $user->getUserID();
Expand Down Expand Up @@ -248,8 +249,8 @@ public function getUserPlaylists($limit=null){
}

$return = self::apiCall('getUserPlaylists',array('sessionID'=>$this->session, 'limit'=>$limit));
if (isset($return['decoded']['result'])) {
return $return['decoded']['result'];
if (isset($return['decoded']['result']['playlists'])) {
return $return['decoded']['result']['playlists'];
} else {
return false;
}
Expand All @@ -276,6 +277,48 @@ public static function getUserPlaylistsByUserID($userid, $limit=null){
return false;
}
}

/*
* getUserLibrary
Requirements: session
@param integer limit, optional
*/
public function getUserLibrarySongs($limit=null){
if (empty($this->session)){
trigger_error(__FUNCTION__." requires a valid session. No session was found.",E_USER_ERROR);
return false;
}

$return = self::apiCall('getUserLibrarySongs',array('sessionID'=>$this->session, 'limit'=>$limit));
if (isset($return['decoded']['result']['songs'])) {
return $return['decoded']['result']['songs'];
} else {
return false;
}
}

/*
* Returns a list of favorites from the user.
TODO: Sort by newest at the top.
Requirements: session
*/
public function getUserFavoriteSongs($limit=null){
if (empty($this->session)){
trigger_error(__FUNCTION__." requires a valid session. No session was found.",E_USER_ERROR);
return false;
}

$return = self::apiCall('getUserFavoriteSongs',array('sessionID'=>$this->session, 'limit'=>$limit));
if (isset($return['decoded']['result']['songs'])) {
return $return['decoded']['result']['songs'];
} else {
return false;
}
}

/*
* Adds a song to the logged-in user's favorites
Expand Down Expand Up @@ -416,6 +459,27 @@ public static function getAlbumInfo($album){
return false;
}
}

/*
* Returns songs in a playlist
Requirements: none
Static Session
@param integer playlistID
*/
public static function getPlaylistSongs($playlistID, $limit=null){
if (!is_numeric($playlistID)){
return false;
}

$return = self::apiCall('getPlaylistSongs', array('playlistID'=>$playlistID, 'limit'=>$limit));
if (isset($return['decoded']['result']['songs'])) {
return $return['decoded']['result']['songs'];
} else {
return false;
}
}

/*
* Creates a playlist under the logged in user
Expand Down Expand Up @@ -654,27 +718,6 @@ public static function getPopularSongsMonth($limit=null){
}
}

/*
* Returns a list of favorites from the user.
TODO: Sort by newest at the top.
Requirements: session
*/
public function getUserFavoriteSongs($limit=null){
if (empty($this->session)){
trigger_error(__FUNCTION__." requires a valid session. No session was found.",E_USER_ERROR);
return false;
}

$return = self::apiCall('getUserFavoriteSongs',array('sessionID'=>$this->session, 'limit'=>$limit));
if (isset($return['decoded']['result'])) {
return $return['decoded']['result'];
} else {
return false;
}
}

/*
* Returns the Country from the IP Address it was requested from
Expand Down
22 changes: 4 additions & 18 deletions gsPlaylist.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private function setURL($url) {
}

public function getURL() {
return self:getPlaylistURLService($this->playlistid);
return self::getPlaylistURLService($this->playlistid);
}

public static function getPlaylistURLService($playlistID){
Expand All @@ -141,24 +141,10 @@ public function getSongs($fetch=true) {
if ($this->songs || !$fetch) {
return $this->songs;
}

return $this->getPlaylistSongs();
$this->songs = parent::getPlaylistSongs($this->getPlaylistID());
return $this->songs;
}

private function getPlaylistSongs($limit=null){
if (!is_numeric($this->getPlaylistID())){
return false;
}

$return = parent::apiCall('getPlaylistSongs', array('playlistID'=>$this->getPlaylistID(), 'limit'=>$limit));
if (isset($return['decoded']['result'])) {
$this->setSongs($return['decoded']['result']);
return $this->songs;
} else {
return false;
}
}


public function importPlaylistData($data) {
if (is_array($data)) {
if (isset($data['PlaylistID'])) {
Expand Down
38 changes: 37 additions & 1 deletion gsUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ function toArray() {
if ($this->getLevel(false) === 0) { $array['IsPlus'] = false; $array['IsAnywhere'] = false; }
if ($this->getProfile(false)) $array['Profile'] = $this->getProfile(false);
if ($this->getFavorites(false) !== null) $array['Favorites'] = $this->getFavorites(false);
//if ($this->getLibrary()) $array['Library'] = $this->getLibrary();
if ($this->getLibrary()) $array['Library'] = $this->getLibrary(false);
if ($this->getPlaylists(false) !== null) $array['Playlists'] = $this->getPlaylists(false);
return $array;
}

public function setUsername($string) {
if (preg_match("/^([a-zA-Z0-9]+[\.\-_]?)+[a-zA-Z0-9]+$/",$string) === false){

if (strpos($string, "@") !== false) {
$this->setEmail($string);
return $string;
}

return false;
} else {
$this->username = $string;
Expand Down Expand Up @@ -276,6 +282,26 @@ public function getPlaylistsFromID($fetch=true) {
return null;
}

public function setLibrary($array) {
if (is_array($array)) {
$this->library = $array;
return true;
}
return false;
}

public function getLibrary($fetch=true) {
if (is_array($this->library) || !$fetch) {
return $this->library;
}
if (is_object($this->parent) && $this->getUserID(false) == $this->parent->sessionUserid && $this->parent->getSession()) {
$library = $this->parent->getUserLibrarySongs();
$this->library = $library;
return $this->library;
}
return null;
}

public function importUserData($data) {
if (is_array($data)) {
if (isset($data['UserID'])) {
Expand Down Expand Up @@ -320,6 +346,12 @@ public function importUserData($data) {
}
if (isset($data['Favorites'])) {
$this->setFavorites($data['Favorites']);
}
if (isset($data['Playlists'])) {
$this->setPlaylists($data['Playlists']);
}
if (isset($data['Library'])) {
$this->setLibrary($data['Library']);
}
return true;
} else {
Expand Down Expand Up @@ -348,6 +380,10 @@ public function authenticate() {
return true;
}
}

public function getSession() {
return $this->parent->session;
}

}

Expand Down

0 comments on commit 3fde7c0

Please sign in to comment.