private function saveData($data) { $artist = $data['artist']; $tracks = $data['tracks']['data']; $genres = $data['genres']['data']; $data['release_date'] = date("d/m/Y", strtotime($data['release_date'])); $album_manager = ManagerFactory::getManager('album'); $artist_manager = ManagerFactory::getManager('artist'); $track_manager = ManagerFactory::getManager('track'); $genre_manager = ManagerFactory::getManager('genre'); $artist_key = $artist_manager->add($artist); $album_key = $album_manager->add($data, $artist_key); $track_num = 1; foreach ($tracks as $track) { $secondes = $track['duration']; $min = (int) ($secondes / 60); $sec = $secondes % 60; $duration = array('min' => $min, 'sec' => $sec); $track['duration'] = $duration; $track['num'] = $track_num; $track_manager->add($track, $album_key); $track_num++; } foreach ($genres as $genre) { $genre_manager->add($genre); $genre_manager->add($genre, $album_key); } }
public function get($key) { $splited = split('_', $key); $nodes = array('artists', $splited[0], 'albums', $splited[0] . '_' . $splited[1], 'tracks', $key); $track = $this->db->getBranch($nodes); if (is_null($track)) { return null; } $album_manager = ManagerFactory::getManager('album'); $album = $album_manager->get($splited[0] . '_' . $splited[1]); $track['album'] = $album; return $track; }
} else { return $app->sendFile($file, 200, ['Content-Type' => 'audio/mp3']); } }); $app->get('/services/{type}/{key}', function ($type, $key) use($app) { $manager = ManagerFactory::getManager($type); if (is_null($manager)) { return null; } if ($key == 'all') { return json_encode($manager->all()); } return json_encode($manager->get($key)); }); $app->get('/services/{type}/{key}/xml', function ($type, $key) use($app) { $manager = ManagerFactory::getManager($type); if (is_null($manager)) { return null; } if ($type == 'genre') { $data = $manager->getAlbumsByGenre($key); if (!is_null($data)) { $xml = XMLParser::encode($data, 'albums'); Utils::Download(sys_get_temp_dir() . '-' . $key . '.xml', $xml->asXML()); return $app->abort(200); } } return $app->abort(404); }); $app->get('/load/{start_id}:{numbers}', function ($start_id, $numbers) use($app) { $deezer = new DeezerLoader();
public function getAlbumsByGenre($key) { $album_keys = array(); $artists = $this->db->getBranch(array('artists')); foreach ($artists as $artist) { foreach ($artist['albums'] as $album_key => $album) { foreach ($album['genres'] as $genre) { if ($genre == $key) { array_push($album_keys, $album_key); } } } } $albums = array(); $manager = ManagerFactory::getManager('album'); foreach ($album_keys as $album_key) { $albums[$album_key] = $manager->get($album_key); } if (sizeof($albums) == 0) { return null; } return $albums; }
private function updTrack($key, $action, $params) { switch ($action) { case 'del': $splited = split('_', $key); $manager = ManagerFactory::getManager('track'); $new_key = $manager->del($key); return array('url' => "album/{$splited['0']}_{$splited['1']}"); case 'set': $splited = split('_', $key); $manager = ManagerFactory::getManager('track'); if (!is_null($params['duration_min']) && !is_null($params['duration_sec'])) { $duration = array('min' => $params['duration_min'], 'sec' => $params['duration_sec']); $params['duration'] = $duration; } if (!is_null($this->_file)) { $url = $manager->saveResources($this->_file, 'track', $key); $params['preview'] = $url; } $new_key = $manager->set($params, $key); return array('url' => "album/{$splited['0']}_{$splited['1']}"); } }
public function search($criterias) { $answer = array(); $track_founds = array(); $artist_founds = array(); $album_founds = array(); $artists = $this->db->getBranch(array('artists')); $types = array(); if ($criterias['checkArtist']) { array_push($types, 'artist'); } if ($criterias['checkAlbum']) { array_push($types, 'album'); } foreach ($artists as $artist_key => $artist) { foreach ($artist['albums'] as $album_key => $album) { if ($criterias['checkMusic']) { foreach ($album['tracks'] as $track_key => $track) { if (trim($criterias['keyword']) != '') { if (stristr($track['infos']['title'], $criterias['keyword'])) { if ($criterias['checkGenre']) { foreach ($criterias['genre'] as $genre_param) { if (in_array($genre_param, $album['genres'])) { if ($criterias['checkYear']) { if ($criterias['choice_year'] == 'value') { if (substr($album['infos']['release_date'], 6, 10) == $criterias['year']) { //KW+GENRE+Y array_push($track_founds, $track_key); } } if ($criterias['choice_year'] == 'interval') { //KW+GR+IN if (substr($album['infos']['release_date'], 6, 10) >= substr($criterias['interval_year'], 0, 4) && substr($album['infos']['release_date'], 6, 10) <= substr($criterias['interval_year'], 5, 8)) { array_push($track_founds, $track_key); } } } else { array_push($track_founds, $track_key); //KEYWORD + GENRE } } } } else { if ($criterias['checkYear']) { if ($criterias['choice_year'] == 'value') { if (substr($album['infos']['release_date'], 6, 10) == $criterias['year']) { //KW+Y array_push($track_founds, $track_key); } } if ($criterias['choice_year'] == 'interval') { //KW+IN if (substr($album['infos']['release_date'], 6, 10) >= substr($criterias['interval_year'], 0, 4) && substr($album['infos']['release_date'], 6, 10) <= substr($criterias['interval_year'], 5, 8)) { array_push($track_founds, $track_key); } } } else { array_push($track_founds, $track_key); //KEYWORD } } } } else { if ($criterias['checkGenre']) { foreach ($criterias['genre'] as $genre_param) { if (in_array($genre_param, $album['genres'])) { if ($criterias['checkYear']) { if ($criterias['choice_year'] == 'value') { if (substr($album['infos']['release_date'], 6, 10) == $criterias['year']) { //GENRE+Y array_push($track_founds, $track_key); } } if ($criterias['choice_year'] == 'interval') { //GR+IN if (substr($album['infos']['release_date'], 6, 10) >= substr($criterias['interval_year'], 0, 4) && substr($album['infos']['release_date'], 6, 10) <= substr($criterias['interval_year'], 5, 8)) { array_push($track_founds, $track_key); } } } else { array_push($track_founds, $track_key); //GENRE } } } } else { if ($criterias['checkYear']) { if ($criterias['choice_year'] == 'value') { if (substr($album['infos']['release_date'], 6, 10) == $criterias['year']) { //Y array_push($track_founds, $track_key); } } if ($criterias['choice_year'] == 'interval') { //IN if (substr($album['infos']['release_date'], 6, 10) >= substr($criterias['interval_year'], 0, 4) && substr($album['infos']['release_date'], 6, 10) <= substr($criterias['interval_year'], 5, 8)) { array_push($track_founds, $track_key); } } } } } } } foreach ($types as $type) { if ($type == 'artist') { $kw_place = $artist['infos']['name']; } if ($type == 'album') { $kw_place = $album['infos']['title']; } if (trim($criterias['keyword']) != '') { if (stristr($kw_place, $criterias['keyword'])) { if ($criterias['checkGenre']) { foreach ($criterias['genre'] as $genre_param) { if (in_array($genre_param, $album['genres'])) { if ($criterias['checkYear']) { if ($criterias['choice_year'] == 'value') { if (substr($album['infos']['release_date'], 6, 10) == $criterias['year']) { //KW+GENRE+Y if ($type == 'artist') { array_push($artist_founds, $artist_key); } if ($type == 'album') { array_push($album_founds, $album_key); } } } if ($criterias['choice_year'] == 'interval') { //KW+GR+IN if (substr($album['infos']['release_date'], 6, 10) >= substr($criterias['interval_year'], 0, 4) && substr($album['infos']['release_date'], 6, 10) <= substr($criterias['interval_year'], 5, 8)) { if ($type == 'artist') { array_push($artist_founds, $artist_key); } if ($type == 'album') { array_push($album_founds, $album_key); } } } } else { if ($type == 'artist') { array_push($artist_founds, $artist_key); } if ($type == 'album') { array_push($album_founds, $album_key); } } } } } else { if ($criterias['checkYear']) { if ($criterias['choice_year'] == 'value') { if (substr($album['infos']['release_date'], 6, 10) == $criterias['year']) { //KW+Y if ($type == 'artist') { array_push($artist_founds, $artist_key); } if ($type == 'album') { array_push($album_founds, $album_key); } } } if ($criterias['choice_year'] == 'interval') { //KW+IN if (substr($album['infos']['release_date'], 6, 10) >= substr($criterias['interval_year'], 0, 4) && substr($album['infos']['release_date'], 6, 10) <= substr($criterias['interval_year'], 5, 8)) { if ($type == 'artist') { array_push($artist_founds, $artist_key); } if ($type == 'album') { array_push($album_founds, $album_key); } } } } else { if ($type == 'artist') { array_push($artist_founds, $artist_key); } if ($type == 'album') { array_push($album_founds, $album_key); } } } } } else { if ($criterias['checkGenre']) { foreach ($criterias['genre'] as $genre_param) { if (in_array($genre_param, $album['genres'])) { if ($criterias['checkYear']) { if ($criterias['choice_year'] == 'value') { if (substr($album['infos']['release_date'], 6, 10) == $criterias['year']) { //GENRE+Y if ($type == 'artist') { array_push($artist_founds, $artist_key); } if ($type == 'album') { array_push($album_founds, $album_key); } } } if ($criterias['choice_year'] == 'interval') { //GR+IN if (substr($album['infos']['release_date'], 6, 10) >= substr($criterias['interval_year'], 0, 4) && substr($album['infos']['release_date'], 6, 10) <= substr($criterias['interval_year'], 5, 8)) { if ($type == 'artist') { array_push($artist_founds, $artist_key); } if ($type == 'album') { array_push($album_founds, $album_key); } } } } else { if ($type == 'artist') { array_push($artist_founds, $artist_key); } if ($type == 'album') { array_push($album_founds, $album_key); } } } } } else { if ($criterias['checkYear']) { if ($criterias['choice_year'] == 'value') { if (substr($album['infos']['release_date'], 6, 10) == $criterias['year']) { //Y if ($type == 'artist') { array_push($artist_founds, $artist_key); } if ($type == 'album') { array_push($album_founds, $album_key); } } } if ($criterias['choice_year'] == 'interval') { //IN if (substr($album['infos']['release_date'], 6, 10) >= substr($criterias['interval_year'], 0, 4) && substr($album['infos']['release_date'], 6, 10) <= substr($criterias['interval_year'], 5, 8)) { if ($type == 'artist') { array_push($artist_founds, $artist_key); } if ($type == 'album') { array_push($album_founds, $album_key); } } } } } } } } } $tracks = array(); $albums = array(); $artists = array(); $manager = ManagerFactory::getManager('track'); foreach ($track_founds as $key) { $tracks[$key] = $manager->get($key); } $answer['tracks'] = $tracks; $manager = ManagerFactory::getManager('artist'); foreach ($artist_founds as $key) { $artists[$key] = $manager->get($key); } $answer['artists'] = $artists; $manager = ManagerFactory::getManager('album'); foreach ($album_founds as $key) { $albums[$key] = $manager->get($key); } $answer['albums'] = $albums; return $answer; }
public function lastTen() { $temps = []; $artists = $this->db->getBranch(array('artists')); foreach ($artists as $artist_key => $artist_value) { foreach ($artist_value['albums'] as $album_key => $album_value) { $temp = array($album_key, $album_value['infos']['release_date']); array_push($temps, $temp); } } for ($i = 0; $i < count($temps); $i++) { for ($j = $i + 1; $j < count($temps); $j++) { if ($temps[$j][1] > $temps[$i][1]) { $temp = $temps[$i]; $temps[$i] = $temps[$j]; $temps[$j] = $temp; } } } $end = count($temps) > 10 ? 10 : count($temps); $albums = []; $album_manager = ManagerFactory::getManager('album'); for ($i = 0; $i < $end; $i++) { array_push($albums, $album_manager->get($temps[$i][0])); } return $albums; }