sasezakiの日記

 | 

2007-08-30

<?php
/**
 *
 * @category   Diggin
 * @package    Diggin_Service
 * @subpackage Tumblr
 * @license    MITあたり
 * @version    $Id:$
 */

/**
 * = how to use=
 * require_once 'Diggin/Service/Tumblr.php';
 * $tumblr = new Diggin_Service_Tumblr();
 * $tumblr->setAuth('sample@sample.com', 'password');
 * $tumblr->setConfig(array('timeout'=> 500));//option
 * $tumblr->writeRegular('title', 'body messages');
 */


/**
 * @see Zend_Http_Client
 */
require_once 'Zend/Http/Client.php';

/**
 * @see Zend_Http_Client_Exception
 */
require_once 'Zend/Http/Client/Exception.php';

/**
 * @category   Diggin
 * @package    Diggin_Service
 * @subpackage Tumblr
 * @author     kazusuke <sasezaki@gmail.com>
 */

class Diggin_Service_Tumblr
{
    const DIGGIN_SERVICE_TUMBLR = 'DIGGIN_SERVICE_TUMBLR';
    
    /**
     * Zend_Http_Client Object
     *
     * @var Zend_Http_Client
     */
    protected $_client;
    
    /**
     * Your Tumblr Email
     *
     * @var string
     */
    protected $_tumblrEmail;
    
    /**
     * Your Tumblr Password
     *
     * @var string
     */
    protected $_tumblrPassword;
    
    /**
     * Checking Type
     * 
     * @var string
     */    
    protected $_checkType;
    
    /*
     * 
     * 
     */
    protected $posts = array(); 
   
    /**
     * Sets up character encoding, instantiates the HTTP client, and assigns the web service version
     * and testing parameters (if provided).
     *
     * @param  string $tumblrEmail
     * @param  string $$tumblrPassword
     * @param  array  (Zend_Http_Client)$config
     * @return void
     */
    public function __construct($tumblrEmail = null, $tumblrPassword = null, $config = array())
    {           
        $this->_client = new Zend_Http_Client();
        $this->_client->setConfig($config);
        $this->setAuth($tumblrEmail, $tumblrPassword);
    }

    /**
     * Set E-mail address & Password For Tumblr
     * 
     * @param string $tumblrEmail
     * @param string $tumblrPassword
     * @return   
     */
    public function setAuth($tumblrEmail, $tumblrPassword)
    {    
        $this->_tumblrEmail     = (string) $tumblrEmail;
        $this->_tumblrPassword  = (string) $tumblrPassword;
        
        return $this;
    }
    
    /**
     * config 
     * 
     * @param array  (Zend_Http_Client)$config
     * @return   
     */
    public function setConfig($config = array())
    {    
        $this->_client->setConfig($config);
        
        return $this;
    }
    
    /**
     * Checking Option before Posting Tumblr
     * 
     * @param $checkType (Full, STRICT, NONE)
     * @return Diggin_Service_Tumblr Provides a fluent interface 
     */
    public function setChecked($checkType = null)
    {
        $this->_checkType = $checkType;
        
        return $this;
    }
    
    
    /**
     * Private method that queries 
     *
     * @param  array  $posts  
     * @throws Zend_Http_Client_Exception
     * @throws Diggin_Service_Exception
     * @return Diggin_Service_Tumblr Provides a fluent interface
     */
    private function write($posts, $generator = null)
    {
        $posts['email'] = $this->_tumblrEmail;   
        $posts['password'] = $this->_tumblrPassword;
        
        foreach ($posts as $p => $var) {
            if(trim($var)) {
                $this->posts[strtolower($p)] = $var;
            }
        }
        
        if(!isset($generator)) {
            $generator = DIGGIN_SERVICE_TUMBLR;
        } elseif (isset($generator) && strlen($generator) <= 64) {
            $posts['generator'] = $generator;
        } else {
            /**
             * @see Diggin_Service_Exception
             */
            require_once 'Diggin/Service/Exception.php';
            $exception = new Diggin_Service_Exception('Generator Name is longed');
            throw $exception; 
        }
        
        $this->_client->setUri('http://www.tumblr.com/api/write');
        $this->_client->setParameterPost($posts);
        
        $request  = $this->_client->request('POST');
        $response = $request->getStatus();

        if ($response == 403) {
            throw new Zend_Http_Client_Exception('Your email address or password were incorrect');
        } else if ($response == 400) {
            throw new Zend_Http_Client_Exception('There was at least one error while trying to save your post');
        } else if ($request->isError()) {
            throw new Zend_Http_Client_Exception('The tumblr.com returned unknown status code');
        }

        return $this;
    }

    /**
     * write by 'Regular' 
     * 
     * @param string $postTitle (optional)
     * @param string $postBody
     * @return Diggin_Service_Tumblr Provides a fluent interface  
     */
    public function writeRegular($postTitle = null, $postBody = null)
    {
        $posts['type'] = 'regular';
        
        if(trim($postTitle) == null && trim($postBody) == null) {
            /**
             * @see Diggin_Service_Exception
             */
            require_once 'Diggin/Service/Exception.php';
            $exception = new Diggin_Service_Exception('Requires at least one');
            throw $exception; 
        }
        
        $posts['title'] = $postTitle;
        $posts['body']  = $postBody;
        
        return $this->write($posts);
    }

    /**
     * write by 'Photo'
     * 
     * @param string type ('source' or 'data') 
     * @param string $sourceOrData
     * 			source - The URL of the photo to copy.
	 * 			data - The binary data of the requested image. 
     * @param string $caption (optional)
     * @return Diggin_Service_Tumblr Provides a fluent interface  
     */
    public function writePhoto($type, $sourceOrData, $caption = null)
    {
       
        $posts['type'] = 'photo';
 
        if (strtolower($type) == 'source'){
            $posts['source'] = $sourceOrData; 
        } elseif (strtolower($type) == 'data'){
            $posts['data'] = $sourceOrData;
        } else {
            require_once 'Diggin/Service/Exception.php';
            $exception = new Diggin_Service_Exception('Requires either source or data');
            throw $exception; 
        }
        
        $posts['caption']  = $caption;
        
        return $this->write($posts);
    }

    /**
     * write by 'quote'
     * 
     * @param string $quote
     * @param string $source (optional) : <a href="source"></a>
     * @return Diggin_Service_Tumblr Provides a fluent interface  
     */
    public function writeQuote($quote, $source = null)
    {
        $posts['type'] = 'quote';
        $posts['quote']  = $quote;
        $posts['source']  = $source;
                
        return $this->write($posts);
    }

    /**
     * write by 'Link'
     * 
     * @param string $name (optional)
     * @param string $url 
     * @param string $descrption (optional)
     * @return Diggin_Service_Tumblr Provides a fluent interface  
     */
    public function writeLink($name = null, $url, $descrption = null)
    {   
        $posts['type'] = 'link';
        $posts['name']  = $name;
        $posts['url'] = $url;
        $posts['descrption']  = $descrption;
                
        return $this->write($posts);
    }
    
    /**
     * write by 'conversation '
     * 
     * @param string $title (optional)
     * @param string $conversation 
     * @return Diggin_Service_Tumblr Provides a fluent interface  
     */
    public function writeConversation($title = null, $conversation)
    {
        $posts['type'] = 'conversation';
        $posts['title']  = $title;
        $posts['conversation'] = $conversation;
                
        return $this->write($posts);
    }
    
    /**
     * write by 'Video'
     * 
     * @param string $embed 
     * @param string $caption (optional)
     * @return Diggin_Service_Tumblr Provides a fluent interface  
     */
    public function writeVideo($embed, $caption = null)
    {
        $posts['type'] = 'video';
        $posts['embed']  = $embed;
        $posts['caption'] = $caption;
                
        return $this->write($posts);
    }
    
}

AndreAndre 2012/07/27 04:21 MsWonderwoman99 : Hey, nice I'm an artist too, well a young one I'm 13, and might be posintg tutorials myself , have any suggestions for me? And also I tend to have trouble with things like creases in clothes and stuff, can you help me? Maybe you can make a video with basics and types of shading. I don't need help drawing objects , I can probably draw MJ but... I need basics like perspective and all. Please help.

atvbrkusatvbrkus 2012/07/27 14:21 SNC1jE <a href="http://pbqbhvbpfpzc.com/">pbqbhvbpfpzc</a>

rvimkwrvimkw 2012/07/28 18:17 q012ie , [url=http://mmfhzxlfmlde.com/]mmfhzxlfmlde[/url], [link=http://dokczwgpsuwi.com/]dokczwgpsuwi[/link], http://lcsrekldkgpy.com/

mptwleguqmptwleguq 2012/07/29 03:57 hbS62I <a href="http://ckaowrdtpcuu.com/">ckaowrdtpcuu</a>

tetzsotetzso 2012/07/29 18:15 G2j9Rn , [url=http://mggfmcfvcleh.com/]mggfmcfvcleh[/url], [link=http://jsxqbftcpneh.com/]jsxqbftcpneh[/link], http://umdlmwpfmmwk.com/

ゲスト



トラックバック - http://tumblr.g.hatena.ne.jp/sasezaki/20070830
 |