getURL($size);
}
function getDeviations($args){
$deviations = array();
$items = dda_fetch_rss(DeviantArt::createRSS($args));
//var_dump($items);
foreach( (array)$items->items as $item){
$over = array('title' => $item['title'], 'href' => $item['link'], 'category' => $item['category']);
$d = new Deviation($item['guid'], $over);
if( ! empty($d->urls) )
$deviations[] = $d;
}
return $deviations;
}
function createRSS($args){
$fragments = array();
//Main search item:
if( true == $args['user'] )
$fragments[] = 'by:' . $args['term'];
elseif( true == $args['fav'] )
$fragments[] = 'favby:' . $args['term'];
elseif( true == $args['search'] )
$fragments[] = $args['term'];
//Deal with the Order:
switch( $args['order'] ){
case 'popular24h':
$fragments[] = "boost:popular age_sigma:24h";
break;
case 'popular3d':
$fragments[] = "boost:popular max_age:72h";
break;
case 'popular1w':
$fragments[] = "boost:popular max_age:168h";
break;
case 'popular1m':
$fragments[] = "boost:popular max_age:744h";
break;
case 'popular':
$fragments[] = "boost:popular";
break;
default:
case 'time':
$fragments[] = "sort:time";
break;
}
if( isset($args['scraps']) && $args['scraps'] == false )
$fragments[] = "-in:scraps";
if( !isset($args['mature']) || $args['mature'] == false )
$fragments[] = "!attribute:DEV_META_MATURE";
//Add all the fragments together as a Search term: (and urlencode it)
$fragments = array( 'q=' . urlencode(implode(' ',$fragments)) );
//Create Feed:
return 'http://backend.deviantart.com/rss.xml?type=deviation&'.implode('&',$fragments);
}
function loadFromXML($id){
$remote = dda_http('http://backend.deviantart.com/embed/info.php?id=' . $id);
if( ! $remote )
return new WP_Error('Cannot load Deviation');
if( preg_match('|(.*?)|i', $remote->content, $mat) )
return new WP_Error($mat[1]);
if( ! preg_match('|(.*?)|imxs',$remote->content,$mat) ||
! preg_match_all('|<(.*?)>(.*?)\\1>|imxs',$mat[1],$mat) )
return new WP_Error('Invalid XML Response');
$return = array();
for($i = 0; $i< count($mat[1]); $i++){
if( strpos($mat[2][$i], ' -1 )
$mat[2][$i] = str_replace( array(''), '', $mat[2][$i]);
$return[ $mat[1][$i] ] = $mat[2][$i];
}
return $return;
}
function loadFromRedirect($id,$item=150){
if( in_array($item,array(100,150,300,'300W')))
$size = $item;
else
$size = 150;
$url = "http://www.deviantart.com/global/getthumb.php?size=$size&id=$id";
$remote = dda_http($url);
if( ! preg_match('|/([a-zA-Z_]+?)_by_(.*?).jpg|i',$remote->headers['Location'],$mat) )
return new WP_Error('Cannot load Deviation');
$details = array( 'title' => str_replace('_',' ',$mat[1]),
'username' => str_replace('_',' ',$mat[2]),
$size => $headers['Location'] );
return $details;
}
}
class Deviation{
var $id = 0;
var $title = '';
var $user = '';
var $userSymbol = '~';
var $submitted = 0;
var $resolution = array('W'=>0,'H'=>0);
var $category = '';
var $urls = array();
function Deviation($id, $over_details = array() ){
$this->id = $id;
$details = wp_cache_get($this->id,'dda2');
if( ! $details )
$details = DDA_Cache::get('Deviation' . $id);
if( ! $details ) {
$details = DeviantArt::loadFromXML($this->id);
if( ! $details || is_wp_error($details) )
$details = DeviantArt::loadFromRedirect($this->id);
}
wp_cache_set($this->id, $details, 'dda2', 60*30);
DDA_Cache::set('Deviation' . $id, $details, 60*60*24*7);//1 week
$details = (array)$over_details + (array)$details;
foreach((array)$details as $name => $value){
if( $value == null )
continue;
switch($name){
case 'title': $this->title = $value; break;
case 'ts': $this->submitted = $value; break;
case 'height': $this->resolution['H'] = (int)$value; break;
case 'width': $this->resolution['W'] = (int)$value; break;
case 'symbol': $this->userSymbol = $value; break;
case 'username': $this->user = $value; break;
case 'deviationid': $this->id = (int)$value; break;
case 'category':
case 'cattitle': $this->category = $value; break;
case 100:
case 'onehundred': $this->urls[100] = $value; break;
case 150:
case 'onefifty': $this->urls[150] = $value; break;
case 300:
case '300W':
case 'threehundred':$this->urls[300] = $value; break;
case 'fileurl': $this->urls['full'] = $value; break;
}
}
}
function getURL($size=150, $rep = false){
if( $size == '300W' )
$size = 300;
$url = isset($this->urls[$size]) ? $this->urls[$size] : false;
if( $url ){
return $url;
} else {
if( $rep )
return false;
$details = DeviantArt::loadFromRedirect($this->id, $size);
if( $details && ! is_wp_error($details) )
$this->Deviation($this->id, $details );
return $this->getURL($size,true);
}
}
}
?>