上個例子,我們用PHP + Ruby 搭配ActiveRecord的方式來寫資料新增的程式
今天我們就來延伸應用一下,要做什麼呢? RSS聯撥器!
有鑒於GoogleReader的RSS聯撥器產生出來的東西太醜(只能修改一兩個小地方.. 我總覺得那好胖=  =),乾脆自己寫個來用
Demo網址改天再PO上來,我們先來寫程式比較重要:P
主機請記得先裝好Ruby、PHP、Apache跟MySQL;OS要啥都沒差,我比較建議LAMP的配置XD
我們來建立一個叫做feeds的目錄包含一個子目錄,叫做lib:

  mkdir -p feeds/lib
 
先跳到feeds/lib新增幾個會被require的檔案:

  cd feeds/lib
  touch connect.rb model.rb require.rb

以下是各個檔案的用處:
- connect.rb
  資料庫連線初始化
- model.rb
  資料表模型宣告
- rqeuire.rb
  會用到的額外library引入

原始碼:

  - connect.rb
    #!/usr/bin/env ruby;require 'lib/require';ActiveRecord::Base.establish_connection({:adapter => "mysql",:host => "localhost",:username => "username",:password => "password",:database => "others"})
  - model.rb
    #!/usr/bin/env ruby;require 'lib/connect';class Feed < ActiveRecord::Base;end
  - require.rb
    #!/usr/bin/env ruby;%w|rubygems active_record hpricot open-uri|.each{|lib| require lib}

一切搞定後,我們可以開始來建立資料庫了!

  mysql> create database others;
  mysql> use others;
  mysql> create table feeds(id int, uri varchar(255));
  mysql> describe feeds;
 
看看資料表結構是否正確!
接著回到上一層目錄,新增底下的幾個檔案:

  touch index.php list.rb new.htm new_record.rb save.php

- index.php
  網站首頁,會列出目前的RSS feed
- save.php
  儲存RSS feed網址
- list.rb
  處理RSS feed
- new_record.rb
  將RSS feed網址存入資料庫(也可以直接用php寫.. 我是沒有意見)
- new.htm
  新增RSS feed網址的表單

原始碼我就直接貼了

  - index.php
    <html>
      <head>
        <title></title>
      </head>
      <body>
        <a href="new.htm">Create</a>
        <ul>
    <?php
      exec("ruby list.rb", $args);
      for($i=0;$i<count($args);$i+=3)
        echo "<li><a href=\"" . $args[$i+1] . "\" title=\"作者:" . $args[$i+2] . "\">" . $args[$i] . "</a> -- " . $args[$i+2] . "</li>";
    ?>
        </ul>
      </body>
    </html>

  - save.php
    <?php
      exec("ruby new_record.rb " . $_POST["feed_uri"], $arg);
      if ($arg) echo "<script>location.href=\"index.php\";</script>";
    ?>
 
  - list.rb
    #!/usr/bin/env ruby
    =begin
        Filename: list.rb
    =end
    require 'lib/model'

    Feed.find(:all).each{|feed|
        doc = Hpricot(open(feed.uri))
        rss = doc.search("entry")
        max = rss.size > 3 ? 3 : rss.size
        max.times {|i|
          break if rss.nil?
          puts rss[i].search("title").text.gsub(/\n/, " ") # Return the title of the article to the PHP file.
          puts rss[i].search("link[@rel='alternate']")[0]["href"].gsub(/\n/, " ") # Return the link of the article to the PHP file.
          puts rss[i].search("author/name").text.gsub(/\n/, " ") # Return the author of the article to the PHP file.
        }
    }

  - new_record.rb
    #!/usr/bin/env ruby
    =begin
      Filename: new_record.rb
    =end
    require 'lib/model';puts Feed.new({:uri => ARGV[0]}).save
 
  - new.htm
    <html>
      <head>
        <title></title>
      </head>
      <body>
        <form action="save.php" method="post">
          <p>Please input the feed url:<input type="text" name="feed_uri" /></p>
          <p><input type="submit" value="Save!" /></p>
        </form>
     </body>
    </html>
 
OK,這樣就可以啦XD
arrow
arrow
    全站熱搜

    hechian 發表在 痞客邦 留言(0) 人氣()