软院通知RSS(附代码)

ponyfish的那个通配符很难设,于是自己用PHP写了一个
两个订阅地址,择一即可:
http://www.JerryHong.com/SSE_RSS/
这个反应比较慢(其实是定位到index.php的),但肯定是最新的
http://www.JerryHong.com/SSE_RSS/index.xml
这个反应很快(xml文件嘛),但要有人访问了上面那个地址这个xml文件才会被更新

改进空间:
1.让index.php也直接返回xml中的内容,而从sse官方获取最新通知的动作由ajax或IFrame来触发,但是rss阅读器应该不会支持这两种……
2.怎么才能统计订阅数?是不是统计每天有阅读活动的读者数量?在统计每天有阅读活动的读者数量时要不要记录IP来判断是不是今天已经阅读过了?

  1. <?php 
  2. /**
  3. *  Class name: RSS
  4. *  Author    : LeadStar Team
  5. *  website   : http://www.leadstar.com.cn/ 
  6. *  CopyRight : LeadStar Team
  7. */
  8. if (defined('_CLASS_RSS_PHP')) return;
  9. define('_CLASS_RSS_PHP',1);
  10.  
  11. define('REFRESH_TIME',3*60*60);//刷新时间,单位秒
  12. class RSS {
  13.    //public
  14.    var $rss_ver = "2.0";
  15.    var $channel_title = 'Tongji SSE RSS';
  16.    var $channel_link = 'http://www.JerryHong.com/SSE_RSS';
  17.    var $channel_description = '';
  18.    var $language = 'zh_CN';
  19.    var $copyright = '';
  20.    var $webMaster = '';
  21.    var $pubDate = '';
  22.    var $lastBuildDate = '';
  23.    var $generator = 'JerryHong RSS Generator';
  24.  
  25.    var $content = '';
  26.    var $items = array();
  27.  
  28.    /**************************************************************************/
  29.    // 函数名: RSS
  30.    // 功能: 构造函数
  31.    // 参数: $title
  32.    // $link
  33.    // $description
  34.    /**************************************************************************/
  35.    function RSS($title, $link, $description) {
  36.        $this->channel_title = $title;
  37.        $this->channel_link = $link;
  38.        $this->channel_description = $description;
  39.        $this->pubDate = Date('Y-m-d H:i:s',time());
  40.        $this->lastBuildDate = Date('Y-m-d H:i:s',time());
  41.    }
  42.    /**************************************************************************/
  43.    // 函数名: AddItem
  44.    // 功能: 添加一个节点
  45.    // 参数: $title
  46.    // $link
  47.    // $description  $pubDate
  48.    /**************************************************************************/
  49.    function AddItem($title, $link, $description ,$pubDate) {
  50.        $this->items[] = array('title' => $title ,
  51.                         'link' => $link,
  52.                         'description' => $description,
  53.                         'pubDate' => $pubDate);
  54.    }
  55.    /**************************************************************************/
  56.    // 函数名: BuildRSS
  57.    // 功能: 生成rss xml文件内容
  58.    /**************************************************************************/
  59.    function BuildRSS() {
  60.        $s = "<?xml version=\"1.0\" encoding=\"gb2312\" ?>
  61.        <rss version=\"2.0\"\n
  62.         xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"
  63.         xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\"
  64.         xmlns:dc=\"http://purl.org/dc/elements/1.1/\">";
  65.        // start channel
  66.        $s .= "<channel>\n";
  67.        $s .= "<title>$this->channel_title</title>\n";
  68.        $s .= "<link>$this->channel_link</link>\n";
  69.        $s .= "<description>$this->channel_description</description>\n";
  70.        $s .= "<language>{$this->language}</language>\n";
  71.        if (!empty($this->copyright)) {
  72.           $s .= "<copyright>$this->copyright</copyright>\n";
  73.        }
  74.        if (!empty($this->webMaster)) {
  75.           $s .= "<webMaster>$this->webMaster</webMaster>\n";
  76.        }
  77.        if (!empty($this->pubDate)) {
  78.           $s .= "<pubDate>{$this->pubDate}</pubDate>\n";
  79.        }
  80.  
  81.        if (!empty($this->lastBuildDate)) {
  82.           $s .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>\n";
  83.        }
  84.  
  85.        if (!empty($this->generator)) {
  86.           $s .= "<generator>{$this->generator}</generator>\n";
  87.        }
  88.       
  89.        // start items
  90.        for ($i=0;$i<count($this->items);$i++) {
  91.            $s .= "<item>\n";
  92.            $s .= "<title>{$this->items[$i]['title']}</title>\n";
  93.            $s .= "<link>{$this->items[$i]['link']}</link>\n";
  94.            $s .= "<description>{$this->items[$i]['description']}</description>\n";
  95.            $s .= "<pubDate>{$this->items[$i]['pubDate']}</pubDate>\n";          
  96.            $s .= "</item>\n";
  97.        }
  98.      
  99.       // close channel
  100.       $s .= "</channel>\n</rss>";
  101.       $this->content = $s;
  102.    }
  103.    /**************************************************************************/
  104.    // 函数名: Show
  105.    // 功能: 将产生的rss内容直接打印输出
  106.    /**************************************************************************/
  107.    function Show() {
  108.        if (empty($this->content)) $this->BuildRSS();
  109.        echo($this->content);
  110.    }
  111.    /**************************************************************************/
  112.    // 函数名: SaveToFile
  113.    // 功能: 将产生的rss内容保存到文件
  114.    // 参数: $fname 要保存的文件名
  115.    /**************************************************************************/
  116.    function SaveToFile($fname) {
  117.        $handle = fopen($fname, 'wb');
  118.        if ($handle === false)  return false;
  119.        fwrite($handle, $this->content);
  120.        fclose($handle);
  121.    }
  122. }
  123.  
  124. header('Content-Type: text/xml');
  125. //http://www.ponyfish.com/feeds/28267doyQWCrC
  126. //http://sse.tongji.edu.cn
  127. //(.*)<td width=\"70\">(.*)</td>
  128. function getContent($url) {
  129.     $file = fopen($url, 'r');
  130.     if(!$file)
  131.     {
  132.         return 0;
  133.     }
  134.     $content = "";
  135.     while (!feof ($file)) {
  136.         $content.= fgets ($file, 1024);
  137.     }
  138.     return $content;
  139. }
  140.  
  141.  
  142. //流程开始
  143. $needRefresh = false;
  144. $xmlContent = getContent('index.xml');
  145. if(!$xmlContent)
  146. {
  147.     $needRefresh = true;
  148. }
  149. //取得访问次数
  150. else
  151. {
  152.     preg_match('/已被访问(\d*)次/s', $xmlContent, $countt);
  153.     $count = $countt[1];
  154.     if(!$count)
  155.         $count=1;
  156.     $count++;
  157.     $xmlContent = preg_replace('/已被访问(\d*)次/i', '已被访问'.$count.'', $xmlContent);
  158.     //取得上次刷新时间
  159.     preg_match('/<lastBuildDate>(.*?)<\/lastBuildDate>/s', $xmlContent, $lastRefreshh);
  160.     $lastRefresh = strtotime($lastRefreshh[1]);
  161.     $now = time();
  162.     if($now-$lastRefresh>=REFRESH_TIME)
  163.         $needRefresh = true;
  164.     else
  165.         $needRefresh = false;
  166. }
  167.  
  168. //如果离上次刷新时间不超过REFRESH_TIME,就把xml文件的内容呈现出去,并更新xml文件(目的是更新访问次数……)
  169. if(!$needRefresh)
  170. {
  171.     echo $xmlContent;
  172.     $file = fopen('index.xml', 'w');
  173.     fwrite($file, $xmlContent);
  174.     fclose($file);
  175.     exit();
  176. }
  177. else
  178. {
  179.     //否则从远端获取资料并刷新xml文件
  180.     $lastRefresh = Date('Y-m-d H:i:s',time());
  181.     $rss = new RSS('sseRSS', 'http://www.JerryHong.com/SSE_RSS', '软院新闻订阅源 by www.JerryHong.com 已被访问'.$count.'次 上次更新时间'.$lastRefresh);
  182.     $line = getContent('http://sse.tongji.edu.cn/');
  183.     if (preg_match ('/<!--学院通知-->(.*?)<!--学术报告-->/s', $line, $newsPart)) {
  184.         $newses = $newsPart[1];
  185.     }
  186.     //get each news from $newses
  187.     preg_match_all ('/<td width=\"270\">.*?<\/td>.*?<td width=\"70\">.*?<\/td>/s', $newses, $everynews);
  188.     foreach($everynews[0] as $news)
  189.     {
  190.         preg_match('/(\/web.*?)\"/s', $news, $linkk);
  191.         $link = $linkk[1];
  192.         $link = "http://sse.tongji.edu.cn".$link;
  193.         $link = htmlspecialchars($link);
  194.        
  195.         preg_match('/none\">(.*)<\/a>/s', $news, $titlee);
  196.         $title = $titlee[1];
  197.         $title = trim($title);
  198.         $title = htmlspecialchars($title);
  199.         $title = substr($title, 0, strrpos($title, '['));
  200.         /*
  201.         $description = getContent($link);
  202.         preg_match('/TEXT-ALIGN: left\">(.*)<\/Textarea>/', $description, $descriptionn);
  203.         $description = $descriptionn[1];
  204.         */
  205.  
  206.         preg_match('/\d\d-\d-\d\d|
  207.                     \d\d-\d\d-\d\d|
  208.                     \d\d-\d-\d|
  209.                     \d\d-\d-\d\d/', $news, $datee);
  210.         $date = $datee[0];
  211.         $rss->AddItem($title, $link, $description, $date);
  212.     }
  213.  
  214.     $rss->BuildRSS();
  215.     $rss->Show();
  216.     $rss->SaveToFile('index.xml');
  217. }
  218. ?>

Related posts

2 Responses to “软院通知RSS(附代码)”


  1. 1 (沙发) 怪怪

    瑞仔你好帅~~

  2. 2 (板凳) Jerry

    @怪怪
    这个世界就你有爱了……

Leave a Reply