sasezakiの日記

 | 

2007-10-01

それplaggerで、やることじゃないんじゃいかな。 02:34

http://plagger.g.hatena.ne.jp/SweetPotato/20070919/tumblr

API用意されてるのになんでRSSたたくんだろう。。

<?php

/**
 * Tumblr Read APIをたたくらいぶらり
 * Zend_Service_Deliciousからぱくりとちゅう
 */

/**
 * @see Zend_Service_Abstract
 */
require_once 'Zend/Service/Abstract.php';

/**
 * @see Zend_Service_Exception
 */
require_once 'Zend/Service/Exception.php';

/**
 * @category   Diggin
 * @package    Diggin_Service_Tumblr
 * @subpackage Tumblr
 * @author     
 */

class Diggin_Service_Tumblr_Read extends Zend_Service_Abstract
{
    
    const API_LOCATION = '.tumblr.com/api/read';
    
    /**
     * Zend_Http_Client Object
     *
     * @var Zend_Http_Client
     */
    protected $_client;
    
    /**
     * target
     *
     * @var string
     */
    protected $_target;

    /**
     * Microtime of last request
     *
     * @var float
     */
    protected static $_lastRequestTime = 0;
    
    
    /**
     * Set target
     *
     * @param  string $target subdomain,domain,URL
     * @return Zend_Service_Tumblr_Read
     */
    public function setTarget($target)
    {
        $this->_target = (string) $target;
        
        return $this;
    }
    
    /**
     * Get target
     *
     * @return string $target
     */
    public function getTarget()
    {
        return $this->_target;    
    }

    public function __construct()
    {
    	
    }
    
    public function getApiUrl()
    {
    	$apiUrl = 'http://'.$this->getTarget().self::API_LOCATION;
    	
    	return $apiUrl;
    }

    public function getTotal()
    {
        $response = $this->makeRequest();
        
        $rootNode = $response->documentElement;

        if ($rootNode->nodeName == 'tumblr') {
            $childNodes = $rootNode->childNodes;

            for ($i = 0; $i < $childNodes->length; $i++) {
                $currentNode = $childNodes->item($i);
                if ($currentNode->nodeName == 'posts') {
                    (integer)$total = $currentNode->getAttribute('total');
                }
            }
        } else {
            /**
             * @see Zend_Service_Delicious_Exception
             */
            require_once 'Zend/Service/Delicious/Exception.php';
            throw new Zend_Service_Delicious_Exception('del.icio.us web service has returned something odd!');
        }

        return $total;
    }
    
    
    //This is test method
    public function getAllPhotoUrl($type = null)
    {
        $num = 50;
        
        $parms['start'] = 0;
        $parms['num'] = 50;
        $parms['type'] = $type;
        
        (integer) $total = $this->getTotal();
        (integer) $loop = $total / $num;
        $loop = floor($loop) + 1;
$loop = 2;////←ここをコメントアウトすると多分しぬ
        $arrPhotoUrl = array();
        
		for ($i = 0; $i < $loop; $i++) {
		    if($i!=0){
		        $parms['start'] = $i * $num + 1;
		    }
		    
		    $response = $this->makeRequest($parms);
		    //DOM

		    $xml = simplexml_import_dom($response);
		    
		    foreach($xml->posts->post as $post){
		        if($post['type'] == 'photo'){
		            foreach($post as $po){
		                if($po['max-width'] == '500'){
		                   $arrPhotoUrl[] = $po;
		                }
		            }
		        }
		    }
		}
        
         
        return $arrPhotoUrl;       
    }
    
        
    /**
     * Handles all GET requests to a web service
     *
     * @param  string $path  Path
     * @param  array  $parms Array of GET parameters
     * @return mixed  decoded response from web service
     */
    public function makeRequest(array $parms = array())
    {
        // if previous request was made less then 1 sec ago
        // wait until we can make a new request
        $timeDiff = microtime(true) - self::$_lastRequestTime;
        if ($timeDiff < 1) {
            usleep((1 - $timeDiff) * 1000000);
        }

        $this->_client = self::getHttpClient();
        $this->_client->setUri(self::getApiUrl());
        if(isset($parms)){
        	$this->_client->setParameterGet($parms);
        }
        
        self::$_lastRequestTime = microtime(true);
        $response = $this->_client->request('GET');
        
        if (!$response->isSuccessful()) {
             /**
              * @see Zend_Service_Delicious_Exception
              */
             require_once 'Zend/Service/Exception.php';
             throw new Zend_Service_Delicious_Exception("Http client reported an error: '{$response->getMessage()}'");
        }

        $responseBody = $response->getBody();
        
           $dom = new DOMDocument() ;
    
           if (!@$dom->loadXML($responseBody)) {
               /**
                * @see Zend_Service_Delicious_Exception
                */
               require_once 'Zend/Service/Delicious/Exception.php';
               throw new Zend_Service_Delicious_Exception('XML Error');
           }
    
        return $dom;

    }
}

/////
$tumblr = new Diggin_Service_Tumblr_Read();
$tumblr->setTarget('fuba');
//    $tumblr->setHttpClient($client);
//print_r($tumblr->getAllPhotoUrl());
$photoUrls = $tumblr->getAllPhotoUrl();
foreach($photoUrls  as $photoUrl){
    echo $photoUrl."\n";
}

ruby-tumblrを使うほうが賢明

http://d.hatena.ne.jp/keita_yamaguchi/20070820/1187556541

SewanaSewana2011/08/19 12:53It's like you're on a moissin to save me time and money!

ggkohwiazggkohwiaz2011/08/19 21:28BZElw8 <a href="http://cxdmqtorldyf.com/">cxdmqtorldyf</a>

ijtdionftczijtdionftcz2011/08/21 00:28IbtcI9 , [url=http://qntarzelxeyn.com/]qntarzelxeyn[/url], [link=http://nvwpxrnlfbiz.com/]nvwpxrnlfbiz[/link], http://nphficzfhsxh.com/

uxhzztoduxhzztod2011/08/25 19:23hhSNha <a href="http://ihdzbljbifoz.com/">ihdzbljbifoz</a>

tmmfvtnupmitmmfvtnupmi2011/08/31 02:356Drk8l , [url=http://egjenfsvvukj.com/]egjenfsvvukj[/url], [link=http://tooplyskzdgb.com/]tooplyskzdgb[/link], http://cubrbzqrdejp.com/

IqbalIqbal2013/04/03 15:50Redanig posts like this make surfing such a pleasure

mgggikazykpmgggikazykp2013/04/04 06:24Ia7Rdh <a href="http://swqmxvrzfrgx.com/">swqmxvrzfrgx</a>

criljddcriljdd2013/04/08 08:208ziM4z , [url=http://ntkzyomejgdd.com/]ntkzyomejgdd[/url], [link=http://oagkoueglteb.com/]oagkoueglteb[/link], http://dgyrhyihtpjo.com/

 |