4
0
mirror of https://github.com/AzuraCast/AzuraCast.git synced 2024-06-14 21:26:37 +00:00

Local caching for all three music providers.

This commit is contained in:
Buster Neece 2014-05-31 10:23:02 -05:00
parent d25a6f6821
commit 8dd7a87750
11 changed files with 213 additions and 32 deletions

View File

@ -87,6 +87,7 @@ class NowPlaying
// Post statistics to official record.
Statistic::post($nowplaying);
/*
// Pull external data for newly updated songs.
if (count(self::$song_changes) > 0)
{
@ -96,6 +97,7 @@ class NowPlaying
$song_obj->syncExternal();
}
}
*/
return $pvl_file_path;
}
@ -221,6 +223,7 @@ class NowPlaying
$np['song_id'] = $current_np_data['song_id'];
$np['song_sh_id'] = $current_np_data['song_sh_id'];
$np['song_score'] = $current_np_data['song_score'];
$np['song_external'] = $current_np_data['song_external'];
}
else if (empty($np['text']))
{
@ -230,6 +233,7 @@ class NowPlaying
$np['song_id'] = NULL;
$np['song_sh_id'] = NULL;
$np['song_score'] = 0;
$np['song_external'] = array();
}
else
{
@ -247,6 +251,9 @@ class NowPlaying
$song_obj = Song::getOrCreate($np);
$sh_obj = SongHistory::register($song_obj, $station, $np);
$song_obj->syncExternal();
$np['song_external'] = $song_obj->getExternal();
$np['song_id'] = $song_obj->id;
$np['song_sh_id'] = $sh_obj->id;
$np['song_score'] = SongVote::getScoreForStation($song_obj, $station);

View File

@ -31,13 +31,9 @@ class BronyTunes
if (isset($existing_ids[$id]))
{
if ($existing_ids[$id] != $processed['hash'])
{
$record = SongExternalBronyTunes::find($id);
}
else
{
$record = NULL;
}
}
else
{

View File

@ -2,9 +2,88 @@
namespace PVL\Service;
use \Entity\Song;
use \Entity\SongExternalEqBeats;
use \Entity\SongExternalEqBeats as External;
class EqBeats
{
public static function load()
{
set_time_limit(300);
// Get existing IDs to avoid unnecessary work.
$existing_ids = External::getIds();
$em = External::getEntityManager();
$tracks = array();
for($i = 1; $i <= 200; $i++)
{
$page_tracks = self::loadPage($i);
if (count($page_tracks) == 0)
break;
$tracks[$i] = $page_tracks;
}
// Loop through tracks.
foreach($tracks as $page_num => $result)
{
foreach((array)$result as $row)
{
$id = $row['id'];
$processed = External::processRemote($row);
if (isset($existing_ids[$id]))
{
if ($existing_ids[$id] != $processed['hash'])
$record = External::find($id);
else
$record = NULL;
}
else
{
$record = new External;
}
if ($record instanceof External)
{
$record->fromArray($processed);
$em->persist($record);
}
}
$em->flush();
$em->clear();
}
return true;
}
public static function loadPage($page = 1)
{
$remote_url = 'https://eqbeats.org/tracks/all/json?'.http_build_query(array(
'page' => $page,
'per_page' => 100,
'client' => 'ponyvillelive',
));
$result_raw = @file_get_contents($remote_url);
if ($result_raw)
{
$result = json_decode($result_raw, TRUE);
return $result;
}
return NULL;
}
/**
* Single Record Search
*/
public static function fetch(Song $song)
{
$result = self::_exactSearch($song);

View File

@ -2,9 +2,91 @@
namespace PVL\Service;
use \Entity\Song;
use \Entity\SongExternalPonyFm;
use \Entity\SongExternalPonyFm as External;
class PonyFm
{
public static function load()
{
set_time_limit(180);
// Get existing IDs to avoid unnecessary work.
$existing_ids = External::getIds();
$em = External::getEntityManager();
$tracks = array();
$first_page = self::loadPage(1);
if (!$first_page)
return FALSE;
$total_pages = (int)$first_page['total_pages'];
$tracks[1] = $first_page['tracks'];
for($i = 2; $i <= $total_pages; $i++)
{
$next_page = self::loadPage($i);
if ($next_page)
$tracks[$i] = $next_page['tracks'];
}
// Loop through tracks.
foreach($tracks as $page_num => $result)
{
foreach((array)$result as $row)
{
$id = $row['id'];
$processed = External::processRemote($row);
if (isset($existing_ids[$id]))
{
if ($existing_ids[$id] != $processed['hash'])
$record = External::find($id);
else
$record = NULL;
}
else
{
$record = new External;
}
if ($record instanceof External)
{
$record->fromArray($processed);
$em->persist($record);
}
}
$em->flush();
$em->clear();
}
return true;
}
public static function loadPage($page = 1)
{
$remote_url = 'https://pony.fm/api/web/tracks?'.http_build_query(array(
'page' => $page,
'client' => 'ponyvillelive',
));
$result_raw = @file_get_contents($remote_url);
if ($result_raw)
{
$result = json_decode($result_raw, TRUE);
return $result;
}
return NULL;
}
/**
* Individual Record Fetching (Retired)
*/
public static function fetch(Song $song)
{
$base_url = 'https://pony.fm/api/v1/tracks/radio-details/';

View File

View File

@ -58,7 +58,7 @@ class SongExternalBronyTunes extends \DF\Doctrine\Entity
public static function match(Song $song, $force_lookup = false)
{
$record = self::getRepository()->findOneBy(array('hash' => $song->id));
if ($record instanceof self && $record->timestamp >= $threshold)
if ($record instanceof self) // && $record->timestamp >= $threshold)
return $record;
else
return NULL;

View File

@ -44,16 +44,11 @@ class SongExternalEqBeats extends \DF\Doctrine\Entity
public static function match(Song $song, $force_lookup = false)
{
$threshold = time()-self::SYNC_THRESHOLD;
$record = self::getRepository()->findOneBy(array('hash' => $song->id));
if ($record instanceof self) // && $record->timestamp >= $threshold)
return $record;
if (!$force_lookup)
{
$record = self::getRepository()->findOneBy(array('hash' => $song->id));
if ($record instanceof self && $record->timestamp >= $threshold)
return $record;
}
return self::lookUp($song);
return null;
}
public static function lookUp(Song $song)
@ -95,4 +90,15 @@ class SongExternalEqBeats extends \DF\Doctrine\Entity
);
}
public static function getIds()
{
$em = self::getEntityManager();
$ids_raw = $em->createQuery('SELECT se.id, se.hash FROM '.__CLASS__.' se')->getArrayResult();
$ids = array();
foreach($ids_raw as $row)
$ids[$row['id']] = $row['hash'];
return $ids;
}
}

View File

@ -56,16 +56,11 @@ class SongExternalPonyFm extends \DF\Doctrine\Entity
public static function match(Song $song, $force_lookup = false)
{
$threshold = time()-self::SYNC_THRESHOLD;
$record = self::getRepository()->findOneBy(array('hash' => $song->id));
if ($record instanceof self) // && $record->timestamp >= $threshold)
return $record;
if (!$force_lookup)
{
$record = self::getRepository()->findOneBy(array('hash' => $song->id));
if ($record instanceof self && $record->timestamp >= $threshold)
return $record;
}
return self::lookUp($song);
return NULL;
}
public static function lookUp(Song $song)
@ -111,4 +106,16 @@ class SongExternalPonyFm extends \DF\Doctrine\Entity
);
}
public static function getIds()
{
$em = self::getEntityManager();
$ids_raw = $em->createQuery('SELECT se.id, se.hash FROM '.__CLASS__.' se')->getArrayResult();
$ids = array();
foreach($ids_raw as $row)
$ids[$row['id']] = $row['hash'];
return $ids;
}
}

View File

@ -84,6 +84,8 @@ class Api_NowplayingController extends \PVL\Controller\Action\Api
'score' => $np_raw['song_score'],
'sh_id' => $np_raw['song_sh_id'],
'vote_urls' => $vote_urls,
'external' => $np_raw['song_external'],
);
$np['current_song'] = $current_song;

View File

@ -16,12 +16,8 @@ class UtilController extends \DF\Controller\Action
\PVL\Debug::showErrors();
\PVL\Debug::setEchoMode(TRUE);
$song = \Entity\Song::find('3618c6feced139030b0306bf15c3fa9c');
$song->syncExternal(true);
$result = $song->getExternal();
\PVL\Debug::print_r($result);
\PVL\Service\EqBeats::load();
echo 'Done Loading EqBeats';
exit;
$np_data = \PVL\NowPlaying::get(2);

View File

@ -11,10 +11,16 @@ error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', 1);
ini_set('memory_limit', '256M');
// Sync the BronyTunes library.
\PVL\Service\BronyTunes::load();
// Sync analytical and statistical data (long running).
\PVL\AnalyticsManager::run();
// Sync the BronyTunes library.
\PVL\Service\BronyTunes::load();
// Sync the Pony.fm library.
\PVL\Service\PonyFm::load();
// Sync the EqBeats library.
\PVL\Service\EqBeats::load();
\Entity\Settings::setSetting('sync_slow_last_run', time());