Update of /cvsroot/php-blog/additional_plugins/alpha/serendipity_plugin_heavyrotation/lib
In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv17677/alpha/serendipity_plugin_heavyrotation/lib
Added Files:
album.php albumhandler.php coverfetcher.php
Log Message:
gitclone.sh autocommit
--- NEW FILE: coverfetcher.php ---
<?php
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'helper' . DIRECTORY_SEPARATOR . 'aws_signed_request.php';
/**
* Proxy class to combine the access to serendipity_plugin_heavyrotation_helper_audioscrobbler
* and serendipity_plugin_heavyrotation_helper_amazon.
*
* @author Lars Strojny <la...@st...>
*/
class serendipity_plugin_heavyrotation_coverfetcher
{
/**
* Audioscrobbler username
*
* @var string
*/
protected $_audioscrobbler_username;
/**
* Amazon API id
*
* @var string
*/
protected $_amazon_id;
/**
* Amazon access key
*
* @var string
*/
protected $_amazon_access_key;
/**
* Amazon country code
*
* @var string
*/
protected $_amazon_country_code;
/**
* Singleton'ed instance of serendipity_plugin_heavyrotation_helper_audioscrobbler
*
* @var serendipity_plugin_heavyrotation_helper_audioscrobbler
*/
protected $_audioscrobbler_instance;
/**
* Set AudioScrobbler username
*
* @param string $username
*/
public function setAudioscrobblerUsername($username)
{
$this->_audioscrobbler_username = $username;
return $this;
}
/**
* Get set AudioScrobbler username
*
* @return string
*/
public function getAudioscrobblerUsername()
{
if ($this->_audioscrobbler_username === null)
throw new Exception('No audioscrobbler username set');
return $this->_audioscrobbler_username;
}
/**
* Set Amazon API key ID
*
* @param string $amazon_id
*/
public function setAmazonId($amazon_id)
{
$this->_amazon_id = $amazon_id;
return $this;
}
/**
* Return set amazon API id
*
* @return string
*/
public function getAmazonId()
{
if ($this->_amazon_id === null)
throw new Exception('No Amazon API id set');
return $this->_amazon_id;
}
public function setAmazonAccessKey($access_key)
{
$this->_amazon_access_key = $access_key;
return $this;
}
public function getAmazonAccessKey()
{
if ($this->_amazon_access_key === null)
throw new Exception('No Amazon access key set');
return $this->_amazon_access_key;
}
/**
* Set Amazon country code
*
* @var string
*/
public function setAmazonCountryCode($code)
{
$this->_amazon_country_code = $code;
return true;
}
/**
* Get set Amazon country code
*
* @return string
*/
public function getAmazonCountryCode()
{
if ($this->_amazon_country_code === null)
throw new Exception('No Amazon country code set');
return $this->_amazon_country_code;
}
/**
* Refresh singletons
*
* @return boolean
*/
public function refresh()
{
$this->_audioscrobbler_instance = null;
return true;
}
/**
* Fetch album information (first audioscrobbler, than amazon)
*
* @return serendipity_plugin_heavyrotation_album|false
*/
public function fetchAlbum()
{
/**
* The strategy is, trying to fetch the best first. If we do not find a
* cover on Amazon, we're iterating through all the next positions and
* using the first complete combination.
*/
$album = $this->_audioscrobbler->getTopAlbumPerWeek();
try {
$image = $this->_fetchAlbumImage($album->artist, $album->name);
return $this->_createAlbum($album->artist, $album->name, $image, $album->url);
} catch (Exception $exception) {
foreach ($this->_audioscrobbler->getTopAlbumsPerWeek() as $album) {
try {
$image = $this->_fetchAlbumImage($album->artist, $album->name);
return $this->_createAlbum($album->artist, $album->name, $image, $album->url);
// Go to the next result
} catch (Exception $exception) {
error_log($exception->getMessage());
}
}
return false;
}
}
/**
* Helper method to create a album object
*
* @return serendipity_plugin_heavyrotation_album
*/
protected function _createAlbum($artist, $name, $image, $url)
{
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'album.php';
return new serendipity_plugin_heavyrotation_album((string)$artist, (string)$name, (string)$image, (string)$url);
}
protected function _fetchAlbumImage($artist, $album)
{
error_log('ARTIST:'.$artist);
$xml = aws_signed_request(
$this->getAmazonCountryCode(),
array(
'Operation' => 'ItemSearch',
'Artist' => $artist,
'Title' => $album,
'SearchIndex' => 'Music',
'ResponseGroup' => 'Images',
),
$this->getAmazonId(),
$this->getAmazonAccessKey()
);
if (!$xml) {
throw new Exception('Error while performing API request');
}
$xml->registerXpathNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2009-03-31');
$imageUrl = (string)array_shift($xml->xpath('//az:LargeImage/az:URL'));
if (!$imageUrl) {
error_log('ERROR');
throw new Exception('Could not find cover');
}
if (extension_loaded('curl')) {
$curl = curl_init($imageUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$image = curl_exec($curl);
} else {
$image = file_get_contents($imageUrl);
}
return $image;
}
/**
* Implementation of property based lazy loading and singleton
* functionality.
*
* @throws Exception
* @return serendipity_plugin_heavyrotation_helper_amazon|serendipity_plugin_heavyrotation_helper_audioscrobbler|null
*/
public function __get($var)
{
switch ($var) {
case "_audioscrobbler":
if ($this->_audioscrobbler_instance === null) {
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'helper' . DIRECTORY_SEPARATOR . 'audioscrobbler.php';
$this->_audioscrobbler_instance = new serendipity_plugin_heavyrotation_helper_audioscrobbler;
$this->_audioscrobbler_instance->setDefaultUsername($this->getAudioscrobblerUsername());
}
return $this->_audioscrobbler_instance;
default:
throw new Exception("Invalid var: \"{$var}\"");
}
}
}
--- NEW FILE: album.php ---
<?php
/**
* Simple album object
*
* @author Lars Strojny <la...@st...>
*/
class serendipity_plugin_heavyrotation_album
{
/**
* Artist name
*
* @var string
*/
public $artist;
/**
* Album name
*
* @var string
*/
public $name;
/**
* Album image (binary data)
*
* @var string
*/
public $image;
/**
* URL in last.fm (breaks solid OOP, I know)
*
* @var string
*/
public $url;
/**
* Setup object. Provides convenience to pass all params in one line
*
* @return serendipity_plugin_heavyrotation_album
*/
public function __construct($artist = null, $name = null, $image = null, $url = null)
{
$this->artist = $artist;
$this->name = $name;
$this->image = $image;
$this->url = $url;
}
}
--- NEW FILE: albumhandler.php ---
<?php
/** Include exception classes */
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'albumhandler' . DIRECTORY_SEPARATOR . 'chickeneggproblem.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'albumhandler' . DIRECTORY_SEPARATOR . 'readerror.php';
/**
* Album handler object
*
* Utilizes the singleton pattern and the factory pattern to provide an easy
* to use interface for the album object. Without forcing the developer to
* stuck into the defaults of conditional loading the data from remote, reading
* them from a file or refreshing the data because they are incomplete.
*
* @author Lars Strojny <la...@st...>
*/
class serendipity_plugin_heavyrotation_albumhandler
{
/**
* Instance of serendipity_plugin_heavyrotation_album
*
* @var serendipity_plugin_heavyrotation_album
*/
protected $_instance = null;
/**
* Metadata directory
*
* @var string
*/
protected $_directory;
/**
* Metadata filename
*
* @var string
*/
protected $_filename;
/**
* Assembled path information for metadata
*
* @var string
*/
protected $_path;
/**
* Path of the image directory
*
* @var string
*/
protected $_image_directory;
/**
* Construct the object. Provides certain convenience parameters to set the
* important parameters at once.
*
* @param serendipity_plugin_heavyrotation_album $instance
* @param string $directory
* @param string $filename
* @param string image_directory
* @return serendipity_plugin_heavyrotation_albumhandler
*/
public function __construct(serendipity_plugin_heavyrotation_album $instance = null,
$directory = null, $filename = null, $image_directory = null)
{
if ($instance !== null) {
$this->setInstance($instance);
}
if ($directory !== null) {
$this->setDirectory($directory);
}
if ($filename !== null) {
$this->setFilename($filename);
}
}
/**
* Set directory where the metadata should be stored in
*
* @param string $directory
* @return serendipity_plugin_heavyrotation_albumhandler
*/
public function setDirectory($directory)
{
if (!file_exists($directory) or !is_dir($directory) or !is_writable($directory))
throw new serendipity_plugin_heavyrotation_albumhandler_readerror("Directory \"{$directory}\" is not writable");
$this->_directory = $directory;
/** Reset path since it is generated on self::getPath() */
$this->_path = null;
return $this;
}
/**
* Get directory
*
* @return string
*/
public function getDirectory()
{
return $this->_directory;
}
/**
* Get completed path
*
* @return string
*/
public function getPath()
{
if ($this->_path == null) {
$path = $this->getDirectory() . DIRECTORY_SEPARATOR . $this->getFilename();
$this->_path = $path;
}
return $this->_path;
}
/**
* Set path including the filename (convenience function)
*
* @param string $path
* @return serendipity_plugin_heavyrotation_albumhandler
*/
public function setPath($path)
{
$this->setDirectory(dirname($path));
$this->setFile(basename($path));
return $this;
}
/**
* Set image filename
*
* @return serendipity_plugin_heavyrotation_albumhandler
*/
public function setFilename($filename)
{
$this->_filename = $filename;
/** Reset path since it is generated on self::getPath() */
$this->_path = null;
return $this;
}
/**
* Get filename
*
* @return string
*/
public function getFilename()
{
return $this->_filename;
}
/**
* Read data from the metadata
*
* @throws serendipity_plugin_heavyrotation_albumhandler_readerror
* @return serendipity_plugin_heavyrotation_album
*/
public function read()
{
$data = array();
$path = $this->getPath();
if (!file_exists($path) or !is_readable($path) or filesize($path) === 0)
throw new serendipity_plugin_heavyrotation_albumhandler_readerror("File {$path} does not exists");
foreach ((array)file($this->getPath()) as $value)
$data[] = trim($value);
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'album.php';
$instance = new serendipity_plugin_heavyrotation_album;
list($instance->artist, $instance->name, $instance->url) = $data;
return $instance;
}
/**
* Write metadata and propably the image
*
* @return serendipity_plugin_heavyrotation_albumhandler
*/
public function write()
{
$instance = $this->getInstance();
file_put_contents($this->getPath(), join(array($instance->artist, $instance->name, $instance->url), "\n"));
if ($instance->image !== null) {
$umask = umask(0133);
file_put_contents($this->getImagePath(), $instance->image);
umask($umask);
}
return $this;
}
/**
* Get an serendipity_plugin_heavyrotation_album-instance
*
* @throws serendipity_plugin_heavyrotation_albumhandler_chickeneggproblem
* @return serendipity_plugin_heavyrotation_album
*/
public function getInstance()
{
if (!$this->hasInstance()) {
try {
$this->setInstance($this->read());
if (!file_exists($this->getImagePath()) or filesize($this->getImagePath()) === 0)
throw new serendipity_plugin_heavyrotation_albumhandler_readerror(
'Metadata exists, but image is not fetched. So reread the whole stuff.');
} catch (serendipity_plugin_heavyrotation_albumhandler_readerror $exception) {
throw new serendipity_plugin_heavyrotation_albumhandler_chickeneggproblem(
"Could not read image from a file. Error was: {$exception->getMessage()}");
}
}
return $this->_instance;
}
/**
* The handler contains an instance or not?
*
* @return boolean
*/
public function hasInstance()
{
return $this->_instance !== null;
}
/**
* Set serendipity_plugin_heavyrotation_album instance
*
* @param serendipity_plugin_heavyrotation_album $instance
* @return serendipity_plugin_heavyrotation_albumhandler
*/
public function setInstance(serendipity_plugin_heavyrotation_album $instance)
{
$this->_instance = $instance;
return $this;
}
/**
* Get image filename
*
* @return string
*/
public function getImageFilename()
{
$instance = $this->getInstance();
return md5($instance->artist . $instance->name) . ".jpg";
}
/**
* Set the image directory
*
* @param string $image_directory
* @return serendipity_plugin_heavyrotation_albumhandler
*/
public function setImageDirectory($image_directory)
{
if (!file_exists($image_directory) or !is_dir($image_directory) or !is_writable($image_directory))
throw new serendipity_plugin_heavyrotation_albumhandler_readerror("\"{$image_directory}\" is not writable");
$this->_image_directory = $image_directory;
return $this;
}
/**
* Get the image directory
*
* @return string
*/
public function getImageDirectory()
{
return $this->_image_directory;
}
/**
* Get path where the image file is located
*
* @return string
*/
public function getImagePath()
{
return $this->getImageDirectory() . DIRECTORY_SEPARATOR . $this->getImageFilename();
}
}
|