Skip to content

Commit

Permalink
Implement tumblr admin page caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Deepwell committed Nov 17, 2011
1 parent fb2b5c1 commit 295f88d
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
cache/*
data/sfWebBrowserPlugin/*
log/*
web/uploads/*
2 changes: 1 addition & 1 deletion apps/frontend/config/routing.yml
Expand Up @@ -4,7 +4,7 @@
# default rules # default rules
homepage: homepage:
url: / url: /
param: { module: default, action: index } param: { module: home, action: index }


# generic rules # generic rules
# please, remove them by adding more specific rules # please, remove them by adding more specific rules
Expand Down
40 changes: 40 additions & 0 deletions apps/frontend/modules/home/actions/actions.class.php
@@ -0,0 +1,40 @@
<?php

/**
* home actions.
*
* @package sf_sandbox
* @subpackage home
* @author Your name here
* @version SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
*/
class homeActions extends sfActions
{
/**
* Executes index action
*
* @param sfRequest $request A request object
*/
public function executeIndex(sfWebRequest $request)
{
// get list of files
$files = array();
$upload_directory = sfConfig::get('sf_upload_dir');
if ($handle = opendir($upload_directory))
{
while (false !== ($file = readdir($handle)))
{
if (substr($file, -4) == 'html')
{
$files[] = $file;
}
}
closedir($handle);
}

// sort by newest on top
rsort($files);

$this->files = $files;
}
}
7 changes: 7 additions & 0 deletions apps/frontend/modules/home/templates/indexSuccess.php
@@ -0,0 +1,7 @@
<h2>Cached files</h2>

<ul>
<?php foreach ($files as $file): ?>
<li><a href="/uploads/<?php echo $file ?>"><?php echo $file ?></a></li>
<?php endforeach ?>
</ul>
3 changes: 2 additions & 1 deletion config/ProjectConfiguration.class.php
Expand Up @@ -7,6 +7,7 @@ class ProjectConfiguration extends sfProjectConfiguration
{ {
public function setup() public function setup()
{ {
$this->enablePlugins('sfDoctrinePlugin'); $this->enablePlugins('sfWebBrowserPlugin');
date_default_timezone_set('America/Vancouver');
} }
} }
12 changes: 6 additions & 6 deletions config/databases.yml
@@ -1,7 +1,7 @@
all: all:
doctrine: # doctrine:
class: sfDoctrineDatabase # class: sfDoctrineDatabase
param: # param:
dsn: 'sqlite:/tmp/sf_sandbox/data/sandbox.db' # dsn: 'sqlite:/tmp/sf_sandbox/data/sandbox.db'
username: root # username: root
password: null # password: null
4 changes: 2 additions & 2 deletions config/properties.ini
@@ -1,4 +1,4 @@
[symfony] [symfony]
name=sf_sandbox name=Cache Tumblr
author=Your name here author=Mark Deepwell
orm=Doctrine orm=Doctrine
Empty file removed data/sandbox.db
Empty file.
58 changes: 58 additions & 0 deletions lib/task/tumblrCacheTask.class.php
@@ -0,0 +1,58 @@
<?php
/**
* Cache tumbler admin homepage.
*/
class tumblrCacheTask extends sfBaseTask
{
public function configure()
{
// use frontend app so we get app.yml config params
$this->addOptions(array(
new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'prod'),
));

$this->namespace = 'tumblr';
$this->name = 'cache';
}

public function execute($arguments = array(), $options = array())
{
// load config
$blog_name = sfConfig::get('app_tumblr_blog_name');
$email = sfConfig::get('app_tumblr_email');
$password = sfConfig::get('app_tumblr_password');
$cache_directory = sfConfig::get('sf_upload_dir');

// sanity check
if (empty($blog_name) || empty($email) || empty($password))
{
echo "Blog name, email, and password must be configured in app.yml\n";
echo "Stopped on errors\n";
return;
}

// Run
$b = new sfWebBrowser(array(), 'sfCurlAdapter', array('cookies' => true));

// Login
echo "Attempting to login...\n";
$res = $b->post('https://www.tumblr.com/login', array('email'=>$email, 'password'=>$password));
if ($res->responseIsError())
{
echo "Login Failed\n";
}

// get the blog
$blog = $b->get('http://www.tumblr.com/blog/'.$blog_name);

// cache to a file
$filename = $cache_directory.DIRECTORY_SEPARATOR.$blog_name.'-'.date('Y-m-d_H:i:s').'.html';
if (!file_put_contents($filename, $blog->getResponseText()))
{
echo "Failed to save cache file\n";
}

echo "Done\n";
}
}

0 comments on commit 295f88d

Please sign in to comment.