/** * @param SimpleXMLElement $xml * @return Track */ public static function fromXML(SimpleXMLElement $xml) { $track = new Track(); if (!empty($xml->name)) { $track->setName((string) $xml->name[0]); } if (!empty($xml->cmt)) { $track->setComment((string) $xml->cmt[0]); } if (!empty($xml->desc)) { $track->setDescription((string) $xml->desc[0]); } if (!empty($xml->src)) { $track->setSource((string) $xml->src[0]); } if (!empty($xml->link)) { $links = []; foreach ($xml->link as $link) { array_push($links, Link::fromXML($link)); } $track->setLinks($links); } if (!empty($xml->number)) { $track->setNumber((int) $xml->number[0]); } if (!empty($xml->type)) { $track->setType((string) $xml->type[0]); } if (!empty($xml->extensions)) { $track->setExtensions(Extensions::fromXML($xml->extensions[0])); } if (!empty($xml->trkseg)) { $trackSegments = []; foreach ($xml->trkseg as $trackSegment) { array_push($trackSegments, TrackSegment::fromXML($trackSegment)); } $track->setTrackSegments($trackSegments); } return $track; }
public static function findArtistTracks($entityManager) { #get the configuration file for the app $file = file_get_contents(__DIR__ . "/../../etc/config.json"); $config = json_decode($file, true); $artistRepository = $entityManager->getRepository('Artist'); $artists = $artistRepository->findBy(array('hasTracks' => NULL), array('name' => 'ASC'), 30, 0); if (count($artists) == 0) { error_log("no trackless artists. Exit"); return; } else { error_log(count($artists) . "artist(s) to process."); } foreach ($artists as $artist) { $name = $artist->getName(); if (!$name) { continue; } #ignore artists that already have tracks if ($artist->getTracks()->count()) { $entityManager->persist($artist); continue; } error_log("processing {$name}"); $spotifyId = $artist->getSpotifyId(); $parameters = array('bucket' => array('id:spotify-WW', 'tracks'), 'limit' => "true", "results" => 10); if ($spotifyId) { $parameters['artist_id'] = $spotifyId; } else { $parameters['artist'] = $name; } $query = http_build_query($parameters); $query = preg_replace("/\\%5B\\d+\\%5D/im", "", $query); \Echonest\Service\Echonest::configure($config['echonest']['key']); $response = \Echonest\Service\Echonest::query('song', 'search', $query); $response = $response->response; if ($response && $response->status->code == 0) { $songs = $response->songs; $entityManager->persist($artist); #no songs, move on to next artist if (!count($songs)) { $artist->setHasTracks(false); continue; } else { $artist->setHasTracks(true); } foreach ($songs as $song) { $track = new \Track(); $title = $song->title; $track->setName($title); $foreign = array_shift($song->tracks); if ($foreign) { $spotifyId = $foreign->foreign_id; $track->setSpotifyId($spotifyId); } $artist->addTrack($track); $track->assignToArtist($artist); $entityManager->persist($track); } } else { error_log("error: " . $response->status->message); break; } } $entityManager->flush(); }