Skip to content

Instantly share code, notes, and snippets.

@MSeven
Created April 29, 2010 09:47
Show Gist options
  • Save MSeven/383378 to your computer and use it in GitHub Desktop.
Save MSeven/383378 to your computer and use it in GitHub Desktop.
CakePhp Queue Flickr Upload tasks
<?php
/**
* @author MGriesbach@gmail.com
* @package QueuePlugin
* @subpackage QueuePlugin.Tasks
*/
abstract class FlickrShell extends Shell {
protected $perms = 'write';
protected $token;
protected function flickrauth() {
Configure::load('flickr_auth');
$tokendata = Configure::read('flickrAuth');
if (!is_array($tokendata) || $tokendata['perms'] != $this->perms) {
$f = $this->getflickr();
$frob = $f->auth_getFrob();
$api_sig = md5($f->secret . "api_key" . $f->api_key . "frob" . $frob . "perms" . $this->perms);
$this->out('http://flickr.com/services/auth/?api_key=' . $f->api_key . '&perms=' . $this->perms . '&frob=' . $frob . '&api_sig=' . $api_sig);
$this->in('done authing?');
$tokendata = $f->auth_getToken($frob);
Configure::store('flickrAuth', 'flickr_auth', $tokendata);
}
$this->token = $tokendata;
return $tokendata['token'];
}
protected function getflickr() {
App::import('Vendor', 'phpFlickr', array(
'file' => 'phpflickr' . DS . 'phpFlickr.php'
));
return (new phpFlickr('xxxxxxxx', 'yyyyyyyyyyyy', true));
}
}
?>
<?php
/**
* @author MGriesbach@gmail.com
* @package QueuePlugin
* @subpackage QueuePlugin.Tasks
*/
App::import('Vendor', 'flickrshell');
class queueFlickrfolderTask extends FlickrShell {
/**
* Adding the QueueTask Model
*
* @var array
*/
public $uses = array(
'Queue.QueuedTask'
);
/**
* ZendStudio Codecomplete Hint
*
* @var QueuedTask
*/
public $QueuedTask;
/**
* Timeout für run, after which the Task is reassigned to a new worker.
*
* @var integer
*/
public $timeout = 120;
/**
* Number of times a failed instance of this task should be restarted before giving up.
*
* @var integer
*/
public $retries = 0;
/**
* Example add functionality.
* Will create one example job in the queue, which later will be executed using run();
*/
public function add() {
$this->out('Flickr Folder Parser.');
$this->hr();
if (count($this->args) < 2) {
$this->out('Please call like this:');
$this->out(' cake queue add flickrfolder <folder>');
} else {
$this->flickrauth();
foreach ($this->args as $folder) {
if ($folder != 'flickrfolder' && file_exists($folder)) {
/**
* Adding a task of type 'example' with no additionally passed data
*/
if ($this->QueuedTask->createJob('flickrfolder', array(
'folder' => $folder
))) {
$this->out('Folder Queued For Evaluation.');
} else {
$this->err('Could not create Folder evaluation Job.');
}
} else {
$this->err('Could not find Folder: ' . $folder);
}
}
}
}
/**
* Example run function.
* This function is executed, when a worker is executing a task.
* The return parameter will determine, if the task will be marked completed, or be requeued.
*
* @param array $data the array passed to QueuedTask->createJob()
* @return bool Success
*/
public function run($data) {
$f = $this->getflickr();
$f->token = $this->flickrauth();
$folder = new Folder($data['folder']);
$tags = basename($data['folder']);
$photosLocal = $folder->find('.*\.jpg');
// Live tag check
$photosLive = $f->photos_search(array(
'user_id' => $this->token['user']['nsid'],
'tags' => $tags
));
if ($photosLive['photo'] > 0) {
$this->out('Tag "' . $tags . '" already exists, checking for dupes.');
foreach ($photosLive['photo'] as $photo) {
$search = array_search($photo['title'], $photosLocal);
if ($search !== NULL) {
unset($photosLocal[$search]);
$this->out($photo['title'] . ' already uploaded, removing.');
}
}
}
if (count($photosLocal) > 0) {
foreach ($photosLocal as $photo) {
$this->QueuedTask->createJob('flickrupload', array(
'file' => $data['folder'] . DS . $photo,
'tags' => array(
$tags
)
));
$this->out('will Upload ' . $photo);
}
} else {
$this->out('no new images to upload in this folder.');
}
return true;
}
}
?>
<?php
/**
* @author MGriesbach@gmail.com
* @package QueuePlugin
* @subpackage QueuePlugin.Tasks
*/
App::import('Vendor', 'flickrshell');
class queueFlickruploadTask extends FlickrShell {
/**
* Adding the QueueTask Model
*
* @var array
*/
public $uses = array(
'Queue.QueuedTask'
);
/**
* ZendStudio Codecomplete Hint
*
* @var QueuedTask
*/
public $QueuedTask;
/**
* Timeout für run, after which the Task is reassigned to a new worker.
*
* @var integer
*/
public $timeout = 600;
/**
* Number of times a failed instance of this task should be restarted before giving up.
*
* @var integer
*/
public $retries = 2;
/**
* Example add functionality.
* Will create one example job in the queue, which later will be executed using run();
*/
public function add() {
$this->out('Flickr Upload.');
$this->hr();
// brauchen wir hier nicht, aber erzeugt den auth token...
$this->flickrauth();
$filepath = $this->in('Path to the file to upload:');
if (file_exists($filepath)) {
/**
* Adding a task of type 'example' with no additionally passed data
*/
if ($this->QueuedTask->createJob('flickrupload', array(
'file' => $filepath
))) {
$this->out('File Queued For upload.');
} else {
$this->err('Could not create Upload Job.');
}
} else {
$this->err('Could not find file: ' . $filepath);
}
}
/**
* Example run function.
* This function is executed, when a worker is executing a task.
* The return parameter will determine, if the task will be marked completed, or be requeued.
*
* @param array $data the array passed to QueuedTask->createJob()
* @return bool Success
*/
public function run($data) {
$f = $this->getflickr();
$f->token = $this->flickrauth();
if (file_exists($data['file'])) {
$tags = array();
if (array_key_exists('tags', $data) && is_array($data['tags'])) {
$tags = $data['tags'];
}
$photosLive = $f->photos_search(array(
'user_id' => $this->token['user']['nsid'],
'tags' => $tags,
'text' => basename($data['file'])
));
if ($photosLive['total'] == 0) {
$this->out('Attempting Upload of ' . basename($data['file']) . ' with tags "' . implode(' ', $tags) . '"');
$photoid = $f->sync_upload($data['file'], basename($data['file']), null, implode(' ', $tags), 0, 0, 0);
$this->out('Uploaded ' . basename($data['file']) . ' as ' . $photoid);
} else {
$this->err('File ' . $data['file'] . ' already Uploaded, Skipping.');
}
return true;
} else {
$this->err('File ' . $data['file'] . ' not found.');
}
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment