array( 'label' => array('name'), 'separator' => '_', 'overwrite' => true) ); /** * belongsTo association * * @var array * @access public */ var $belongsTo = array( 'PrimaryMedia' => array('className' => 'Media', 'foreignKey' => 'primary_media_id', 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => ''), ); /** * hasMany association * * @var array * @access public */ var $hasMany = array( 'ArtistAlbum' => array('className' => 'ArtistAlbum', 'foreignKey' => 'artist_profile_id', 'conditions' => '', 'fields' => '', 'order' => 'release_year DESC', 'limit' => '', 'offset' => '', 'dependent' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => ''), 'ArtistMetadatum' => array('className' => 'ArtistMetadatum', 'foreignKey' => 'artist_profile_id', 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'dependent' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => '') ); /** * hasAndBelongsToMany association * * @var array * @access public */ var $hasAndBelongsToMany = array( 'Article' => array('className' => 'Article', 'joinTable' => 'articles_artist_profiles', 'foreignKey' => 'artist_profile_id', 'associationForeignKey' => 'article_id', 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'unique' => '', 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => '', 'with' => 'ArticlesArtistProfile'), 'Media' => array('className' => 'Media', 'joinTable' => 'artist_profiles_media', 'foreignKey' => 'artist_profile_id', 'associationForeignKey' => 'media_id', 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => '', 'with' => 'ArtistsMedia'), 'CalendarEvent' => array('className' => 'CalendarEvent', 'joinTable' => 'artist_profiles_calendar_events', 'foreignKey' => 'artist_profile_id', 'associationForeignKey' => 'calendar_event_id', 'conditions' => 'CalendarEvent.moderated = 1', 'fields' => '', 'order' => 'date_created DESC', 'limit' => '', 'offset' => '', 'dependent' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => '', 'with' => 'ArtistProfilesCalendarEvents') ); /** * Array containing the names of components this model uses. * * @var array * @access public * @see app/controllers/components/lastfm.php */ var $components = array('Lastfm'); /** * A very simple search function used by the artist_profile_controller's index() function * * @param string $findMe * The string to look for * @return mixed * Returns an array of matching artist profiles, or false for no results */ function simpleSearch($findMe = null) { $this->recursive = 0; if($findMe != null) { $artCond= array(); $artCond['or'] = array('ArtistProfile.name LIKE "%'.$findMe.'%"','ArtistProfile.display_name LIKE "%'.$findMe.'%"'); $this->unbindAll(array('hasMany' => array('ArtistAlbum'))); $artists = $this->findAll($artCond); $this->ArtistAlbum->unbindAll(array('belongsTo' => array('ArtistProfile'))); $albums = $this->ArtistAlbum->findAll('ArtistAlbum.name LIKE "%'.$findMe.'%"'); $results = array('artists' => $artists, 'albums' => $albums); return $results; } else { return false; } } /** * Find the last time an artist was played on-air, according to our playlists * * @param int $id * The ID of the artist profile * @return mixed * Returns the unix timestamp of the last time an artist was played, or false if never played */ function lastPlayed($id = null) { $this->bindModel(array('hasMany' => array('Playlist'))); $last_played = $this->Playlist->findAll(array('artist_profile_id' => $id), 'Playlist.id', 'Playlist.id desc', 1); if(!$last_played) { return false; } else { return $last_played[0]; } } /** * Create a new artist record and import associated data from LastFM * * @param array $attr * Data array containing information about the artist to look for * @param boolean $auto_pop * If false, no attempt to import data will be made * @param boolean $verify * If true, will abandon profile creation if there is no data to import from LastFM * * @return mixed * Returns the new profile, or false if unsuccessful */ function createArtist($attr,$auto_pop = false,$verify = false) { if (!isset($attr['name'])) return false; $data['id'] = null; $data['name'] = ''; $data['alternate_names'] = ''; $data['bio'] = ' '; $data['creator'] = 'last.fm'; $data['credit'] = ''; $data = array_merge($data,$attr); if (!isset($data['display_name'])) { $data['display_name'] = $data['name']; } if ($verify) { $lastfm_albums = $this->Lastfm->topAlbumsForArtist($data['name']); if (count($lastfm_albums) == 0) return false; } $data = $this->create($data); if (!$this->save($data)) { return false; } $data['ArtistProfile']['id'] = $this->getLastInsertId(); if ($auto_pop) { $data = $this->fetchArtistsData($data['ArtistProfile']); if (!$this->save($data)) { return false; } } return $data; } /** * Attempts to import individual album data and artwork from LastFM * * @param string $artist * The name of the artist to look for * @param string $album * The name of the album to look for * * @return array * Returns an array of album data in an insert-friendly format */ function findAlbumDetails($artist, $album) { $output = array(); $lastfm_album = $this->Lastfm->artistAlbum($artist, $album); //format the results to be saved in the database $album_art = array(); $keywords = array($artist,$album,'album_art'); $album_art['url'] = strval($lastfm_album->coverart->large); $lastFMDefault = "http://cdn.last.fm/depth/catalogue/noimage/cover_large.gif"; //don't waste space... only save the artwork if it's not LastFM's defaut image if($album_art['url'] != $lastFMDefault) { $album_art['title'] = $album_art['caption'] = $artist.' - '.$album; $album_art['keywords'] = implode(' ',$keywords); $album_art['byline_other'] = ''; if($media_data = $this->Media->ingest($album_art)) { //print_r($media_data); $album_meta['ArtistAlbum']['media_id'] = $media_data['id']; } } $release_date = explode(" ",trim(strval($lastfm_album->releasedate))); $album_meta['ArtistAlbum']['name'] = $album; $album_meta['ArtistAlbum']['release_year'] = isset($release_date[2]) ? intval($release_date[2]) : null; //get the track list $track_counter = 1; foreach($lastfm_album->tracks->track as $track) { $track_meta = array(); $track_meta['id'] = null; $track_meta['track_number'] = $track_counter++; $track_meta['title'] = strval($track['title']); $album_meta['ArtistTrack'][] = $track_meta; } return $album_meta; } /** * Fetch the names of all albums for an artist on LastFM to find new additions, * but don't import anything yet * * @param array $profile * The artist profile * * @return array * Returns an array of album data */ function findUpdates($profile) { $output = array(); $lastfm_albums = $this->Lastfm->topAlbumsForArtist($profile['name']); if (empty($lastfm_albums)) { return $output; } $count = count($lastfm_albums->album); //if there aren't any albums, just stop here if ($count <= 0) { return $output; } // Calculate the total reach of the artist $reach = 0; foreach ($lastfm_albums->album as $album) { $reach += $album->reach; } $avg = floor($reach/$count); //only process albums with above average reach foreach($lastfm_albums->album as $album) { if ($album->reach > $avg) { $album_meta = array(); $album_meta = strval($album->name); $lastfm_album = $this->Lastfm->artistAlbum($profile['name'],$album_meta); // If LastFM doesn't know anything about this album, skip it if (!$lastfm_album) continue; // If there aren't any tracks for this album, skip it if (count($lastfm_album->tracks->track) == 0) { continue; } $output[] = $album_meta; } } return $output; } /** * Attempts to import all album data for a given artist with a saved profile * * @param string $profile * The artist profile * * @return array * Returns an array of album data in a saveable format */ private function fetchArtistsData($profile) { $output = array(); $output['ArtistProfile'] = $profile; if (!isset($profile['name'])) { return $output; } $lastfm_albums = $this->Lastfm->topAlbumsForArtist($profile['name']); if (empty($lastfm_albums)) return $output; $count = count($lastfm_albums->album); // If there isn't any albums this artist is wack if ($count <= 0) return $output; /* * This section of the code does some statisitical analysis * of the info returned by lastfm. This is necessary since lastfm * allows users to submit information with whatever artist and album * name they wish. This should (hopefully) keep the albums limited * to the official discography of the artist */ // Set up some default vars for the stat analysis of the data $reach = 0; // Calculate the total reach of the artist foreach ($lastfm_albums->album as $album) { $reach += $album->reach; } $avg = floor($reach/$count); foreach($lastfm_albums->album as $album) { if ($album->reach > $avg) { $album_meta = array(); $album_meta['name'] = strval($album->name); $lastfm_album = $this->Lastfm->artistAlbum($profile['name'],$album_meta['name']); // If LastFM doesn't know anything about this album // Skip Over it if (!$lastfm_album) continue; // If there aren't any tracks for this album its bunk if (count($lastfm_album->tracks->track) == 0) { continue; } $album_art = array(); $keywords = array($profile['name'],$album_meta['name'],'album_art'); $album_art['url'] = strval($lastfm_album->coverart->large); $lastFMDefault = "http://cdn.last.fm/depth/catalogue/noimage/cover_large.gif"; //We don't want to download the LastFM default over and over again... //so only if the album isn't using the LastFM default art do we need to upload it //and store in the media manager if($album_art['url'] != $lastFMDefault) { $album_art['title'] = $album_art['caption'] = $profile['name'].' - '.$album_meta['name']; $album_art['keywords'] = implode(' ',$keywords); $album_art['byline_other'] = ''; if($media_data = $this->Media->ingest($album_art)) { //print_r($media_data); $album_meta['media_id'] = $media_data['id']; $album_meta['Media'] = $media_data; } } $release_date = explode(" ",trim(strval($lastfm_album->releasedate))); $album_meta['release_year'] = isset($release_date[2]) ? intval($release_date[2]) : null; $album_meta['artist_profile_id'] = $profile['id']; // Create this album $this->ArtistAlbum->save($album_meta); $album_meta['id'] = $this->ArtistAlbum->getLastInsertId(); $this->ArtistAlbum->id = null; //Prepare the model for the next insert $track_counter = 1; foreach($lastfm_album->tracks->track as $track) { $track_meta = array(); $track_meta['title'] = strval($track['title']); $track_meta['artist_album_id'] = $album_meta['id']; $track_meta['track_number'] = $track_counter++; $this->ArtistAlbum->ArtistTrack->save($track_meta); $track_meta['id'] = $this->ArtistAlbum->ArtistTrack->getLastInsertID(); $album_meta['ArtistTrack'][] = $track_meta; $this->ArtistAlbum->ArtistTrack->id = null; } $output['ArtistAlbum'][] = $album_meta; } } return $output; } /** * Generates a human-readable url if someone attempt to access an artist profile by ID * * @param int $id * The id of the profile * @param boolean $full * If true, the full base URL will be prepended to the result * @return string * Translated URL */ function viewLink($id, $full = false) { // If the id is numeric convert it to a slug if (is_numeric($id)) { $artist = $this->findById($id,array('slug')); $id = $artist['ArtistProfile']['slug']; } return Router::url('/artists/view/'.$id,$full); } } ?>