自制 Tumblr RSS:可输出大图(转制版)

/ 2评 / 2

著名的“老司机”平台——汤不热(Tumblr),一直支持 RSS 输出。不过其中输出的图片尺寸是缩小版的。所以自制了这个简单的脚本,可将RSS中的小图网址替换为大图的网址,再输出。喜欢的朋友可以将这个 php 脚本放到自己的 php 虚拟主机中,以 site_name.com/tumblr.php?id=xxx 的方式来订阅。

例如:原来的 RSS 网址 http://wanimal1983.tumblr.com/rss, 转制后通过 http://site_name.com/tumblr.php?id=wanimal1983 来订阅。

脚本内容如下:

<?php

/**
 * Aurhor: wizos
 */

// 制定配置
$user_id = $_GET["id"];

$config['start_url'] = 'http://'.$user_id.'.tumblr.com/rss';
$config['file_title'] = 'tumblr-'.$user_id;
$config['cache_time'] = 6*3600;
$config['output_encoding'] = 'utf-8';


check_cache ( $config );
$res = get_net( $config['start_url'] );
$res = preg_replace('/src="(.+?)_500\.jpg"/s', 'src="$1_1280.jpg"', $res );
$res = preg_replace('/<source\ssrc="http:\/\/.+?\/tumblr_(.+?)"/s', '<source src="http://vtt.tumblr.com/tumblr_$1.mp4"', $res );

//<source src="http://lingzai01.tumblr.com/video_file/t:8rtoLPwrJKhRw7BczIH2hQ/143310201992/tumblr_nvn07mfX021ufqkfe" type="video/mp4">

file_put_contents( $config['cache_file'] , $res );
header('Content-Type: text/xml; charset=utf-8') ;
@readfile( $config['cache_file'] );

function check_cache ( $config ){
  $cache_folder = dirname( __FILE__ ).'/cache';
  $cache_file = $cache_folder.'/'.$config['file_title'].'.xml' ; // 缓存文件藏在此处
  $GLOBALS['config']['cache_file'] = $cache_file; // 方便下文使用
  if ( is_file( $cache_file ) ){
        $interval_time = time() - filemtime( $cache_file ) ; // 以秒为单位
        if ( $interval_time < $config['cache_time'] ){
      header('Content-Type: text/xml; charset='.$config['output_encoding']) ;
      @readfile( $cache_file );
       		exit();
        }
    }elseif( !is_dir( $cache_folder ) ){
        mkdir( $cache_folder );
    }
}


function get_net( $url, $options = array() ){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url ); 
    curl_setopt($curl, CURLOPT_HEADER, 0);//非零值时,将 header 包含在输出中
    curl_setopt($curl, CURLOPT_NOSIGNAL, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置不输出在浏览器上
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    return common_net( $url, $curl, $options );
}

function common_net( $url, $curl, $options = array() ){
    if (preg_match('/^https/',$url)){
        //$options[CURLOPT_SSL_VERIFYHOST] = 1;
        $options[CURLOPT_SSL_VERIFYPEER] = 0;
    }
    curl_setopt_array($curl, $options);
    $data = curl_exec($curl);
    $curl_errno = curl_errno($curl); 
    curl_close($curl);//关闭cURL资源,并且释放系统资源
    if($curl_errno>0){
        return 'error';
    }else{
        return $data;
    }
}



$feed_items = '';
$feed_header = '';
$feed_footer = '';
$encoding = '';

function make_feed ( $config ){
    $GLOBALS['feed_header']= '<?xml version="1.0" encoding="'.$config['output_encoding'].'"?><rss version="2.0"><link rel="stylesheet" href="lib/rss.css"/><channel>
<title>'.$config['feed_title'].'</title>
<link>'.$config['start_url'].'</link>
<description>'.$config['feed_description'].'</description>'; 
    $GLOBALS['feed_footer'] = '</channel></rss>';
    $encoding = $config['output_encoding'];
}

function add_item ( $title, $link, $description, $author, $pubDate ){
  if( ($title ==null || $title=='') && ($description ==null || $description=='') ){
    return;
  }
  $GLOBALS['feed_items'] .='<item><title><![CDATA['.$title.']]></title><link>'.$link.'</link><description><![CDATA['.$description.']]></description><author>'.$author.'</author><pubDate>'.$pubDate.'</pubDate></item>';
}

function add_item_no_cdata ( $title, $link, $description, $author, $pubDate ){
  if( ($title ==null || $title=='') && ($description ==null || $description=='') ){
    return;
  }
  $GLOBALS['feed_items'] .='<item><title>'.$title.'</title><link>'.$link.'</link><description><![CDATA['.$description.']]></description><author>'.$author.'</author><pubDate>'.$pubDate.'</pubDate></item>';
}

function output_feed ( ){
  if( $GLOBALS['feed_items'] ==null || $GLOBALS['feed_items']==''){
    return;
  }
    return $GLOBALS['feed_header'].$GLOBALS['feed_items'].$GLOBALS['feed_footer'];
}

?>

 

 

2条回应:“自制 Tumblr RSS:可输出大图(转制版)”

  1. youyu说道:

    是否需要墙外的服务器?

发表评论

电子邮件地址不会被公开。 必填项已用*标注