Permalink
Browse files

Implement tumblr admin page caching

  • Loading branch information...
Mark Deepwell
Mark Deepwell committed Nov 17, 2011
1 parent fb2b5c1 commit 295f88d984dde3a4666721373a7e431c68478474
View
@@ -0,0 +1,4 @@
+cache/*
+data/sfWebBrowserPlugin/*
+log/*
+web/uploads/*
@@ -4,7 +4,7 @@
# default rules
homepage:
url: /
- param: { module: default, action: index }
+ param: { module: home, action: index }
# generic rules
# please, remove them by adding more specific rules
@@ -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;
+ }
+}
@@ -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>
@@ -7,6 +7,7 @@ class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
- $this->enablePlugins('sfDoctrinePlugin');
+ $this->enablePlugins('sfWebBrowserPlugin');
+ date_default_timezone_set('America/Vancouver');
}
}
View
@@ -1,7 +1,7 @@
all:
- doctrine:
- class: sfDoctrineDatabase
- param:
- dsn: 'sqlite:/tmp/sf_sandbox/data/sandbox.db'
- username: root
- password: null
+# doctrine:
+# class: sfDoctrineDatabase
+# param:
+# dsn: 'sqlite:/tmp/sf_sandbox/data/sandbox.db'
+# username: root
+# password: null
View
@@ -1,4 +1,4 @@
[symfony]
- name=sf_sandbox
- author=Your name here
+ name=Cache Tumblr
+ author=Mark Deepwell
orm=Doctrine
View
No changes.
@@ -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.