2009年11月4日 星期三

發現...

無意間發現有人在轉錄我的文章?不過是比較像弄成書籤的網誌。


有人把我寫的文章的標題打在自己的 blog 上,然後再把該篇文章的上前面幾段內文貼上去。我本來是沒什麼在意,但如果考慮 Search Engines 的搜尋排名,那就會計較了,哈。


原本是去 Search 某個關鍵字看看會不會找到我的文章,然後才發現怎麼有人的文章跟我標題一模一樣?還出現部份內文?且比我排在前頭!比較好的做法是至少文章標題不要一樣嘛,多個 [轉錄] 不是很好嗎?因為我自己一開始去看那個網站時,真的會以為那些文章都是對方寫的,每一篇雖然文章底部有標明 Source 的字樣,但仍不是很明顯。


之所以會有不好的感觸是因為前陣子我在找某個主題的相關文章,恰巧也發現 A 先生的文章也被 B 先生這樣處理( A 文章內容被 B 先生複製貼上),雖然多少有標明來源,但文章標題都仍是一樣的。如果只是這樣倒沒什麼差,但我卻發現某個很大咖的研究中心,在推廣教學時,反而去引用 B 先生的網誌文章,這就讓我感覺對 A 先生的不公平,畢竟原先的文章是由 A 先生辛苦撰寫的嘛。


anyway,稍稍抱怨一下,我猜我發現的那個網站應該是類似機器人處理的,因為它除了有制式的格式外,還有附上文章來源的網頁快照(snapshot),只是,尊重作者,最好還是明確地寫上些完整的訊息會來得更好。但,寫下東西的最大用意除了跟大家分享外,就是幫自己寫點紀錄啦。


Facebook API Publisher - 使用 Attach 附件服務增加發佈塗鴉牆的效果

Facebook Attach


有時希望可以讓使用者在分享文字或連結時,能自動帶出一些效果,這時候我們需要的就是 Publisher 服務。例如當輸入 YouTuBe 影片連結時,系統會自動幫我們嵌入 YouTuBe 的顯示頁面,而不再只是簡單的 link 樣貌,此時原理即是將使用者輸入的資料,進行分析再轉換成新的呈現方式。


然而,目前 Facebook 平台開放的狀態還沒讓開發者如此便利,但有個間接使用的方式,那就是發表時以 Attach(附件) 來呈現。關於 Attach 的設定方式如下:


Setup



  • My Applications > Edit Settings > Profiles > Profile Publisher

    • Publish

      • Text

      • Callback URL



    • Self-Publish

      • Text

      • Callback URL






其中可分成兩類,一種是在別人的塗鴉牆留言時,另一種則是在自己的塗鴉牆留話。分別都需要填寫兩項資訊,其中 Text 代表在 Attach(附件) 顯示的按鈕名稱,如:加增表情符號。 Callback URL 則是當使用者按下按鈕時,系統會去這個位置要資料來呈現。實作上要留意,那個 Callback URL 回傳的資料要以 JSON 格式回來。


至於 Facebook 去 Callback URL 進行撈資料時,帶有以下參數資訊:



  • fb_sig_api_key: Your application's API key.

  • fb_sig: A signature using your application's secret key to encrypt the request.

  • fb_sig_in_canvas: This parameter is always false or 0 because the Publisher is not a canvas page.

  • fb_sig_request_method: This is always a POST method.

  • fb_sig_locale: This is the user's locale.

  • fb_sig_time: The time that the request was made.

  • fb_sig_added: Indicates whether the user has authorized (agreed to your Terms of Service) the application.

  • fb_sig_profile_user: Indicates the user whose profile is being published to.

  • fb_sig_user: The ID of the user interacting with the Publisher.

  • method: Indicates if the Publisher is getting the application's Publisher interface or getting the Feed story. 

  • 等等‥


只是,依舊沒有使用者當下輸入的資訊,這讓我有一點小失望,這也可能基於一些隱私或安全性的考量吧?剩下的我只好有空再來摸索啦!但是,目前開放的平台特色仍可以做出不錯的效果喔!


發佈塗鴉牆的效果
Facebook Attach

點選播放的畫面
Facebook Attach


簡單的 Callback 程式碼,其中 base.php 則是處理 Facebook API 的初始化,而 publisher_getFeedStory 部份則是直接用上頭 Facebook - Publisher 提到的範例。


<?php
require( 'base.php' );
$data = array();
$data['content'] = array();

$out = '';
foreach( $_REQUEST as $k => $v )
        $out .= "[$k] => [$v]<br>";
if( !isset( $_REQUEST['method'] ) || $_REQUEST['method'] == 'publisher_getInterface'  )
{
        $data['content']['fbml'] = json_encode( $out );
        $data['content']['oublishEnabled'] = true;
        $data['method'] = 'publisher_getInterface';
}
else
{
        $data['content']['attachment'] = array();
        $data['content']['attachment']['name'] = 'ninja cat';
        $data['content']['attachment']['href'] = 'http://www.youtube.com/watch?v=muLIPWjks_M';
        $data['content']['attachment']['caption'] = '{*actor*} uploaded a video to www.youtube.com';
        $data['content']['attachment']['description'] = 'a sneaky cat';
        $data['content']['attachment']['properties'] = array();
        $data['content']['attachment']['properties']['category'] = array();
        $data['content']['attachment']['properties']['category']['text'] = 'pets';
        $data['content']['attachment']['properties']['category']['href'] = 'http://www.youtube.com/browse?s=mp&t=t&c=15';
        $data['content']['attachment']['properties']['ratings'] = '5 stars';
        $data['content']['attachment']['media'] = array();
        $data['content']['attachment']['media'][0]['type'] = 'video';
        $data['content']['attachment']['media'][0]['video_src'] = 'http://www.youtube.com/v/fzzjgBAaWZw&hl=en&fs=1';
        $data['content']['attachment']['media'][0]['preview_img'] = 'http://i2.ytimg.com/i/uZp-cf0yOTqQEOuKGGnocg/1.jpg';
        $data['content']['attachment']['media'][0]['video_link'] = 'http://www.youtube.com/watch?v=muLIPWjks_M';
        $data['content']['attachment']['media'][0]['video_title'] = 'ninja cat';
        $data['method'] = 'publisher_getFeedStory';
}
echo json_encode( $data );
?>


其中,上述發佈用的 template 允許四種 type ,分別為 image , flash, mp3, vedio ,細節請參考 Facebook - Attachment (Streams)


參考資料



2009年11月3日 星期二

[Javascript] 以 Array 方式產生亂數序列

有時需要一串亂數序列使用時,例如亂數播放影片等等的,就可以用下面這招!把數列都打亂囉。


使用到 Math.round (四捨五入) 與 Math.random (亂數),流程是先呼叫 Math.random 產生一個介於 0 ~ 1 的數字,再利用 Math.round 讓這個數值為 0 或 1 ,最後再把數值扣 0.5 ,讓它明確地變成負數或正數,以此達成 sorting 用的 compare function 啦。因此呼叫 sort 完後,就變成亂數的序列囉。


<script type="text/javascript">
    test = new Array( 1 , 2 , 3 , 4 , 5 );
    test.sort( function(){ return Math.round( Math.random() ) - 0.5 ; } );
    document.write( test );
</script>

參考資料



2009年11月2日 星期一

Django Testing

最近要開始嘗試 Test-driven development (TDD) 了,簡單地說就是以測試起家,什麼想做的事,第一件事就是幫它規劃輸入的參數與預期的輸出結果,然後就開始撰寫測試的程式碼,最後才開始填寫核心程式碼。


這樣的好處除了可以替案子留下測試的簡單驗證程式碼外,以避免未來案子過大造成的相互影響,也能更明確寫程式的目標,而唯一的缺點就是耗時間。以下紀錄 Django 在 apps 裡新增 testing 的環境。



  1. 在 project 中的 apps 裡新增 tests.py 檔案和 fixtures 目錄

  2. 在 fixtures 目錄裡準備一筆測資

    • ex: record_1000.json

    • 可從現有 database 匯出

      • # python manager dumpdata your_app > record_1000.json





  3. 撰寫 tests.py 檔案

    • import django
      import views as testingAPI

      class FuncTestCase(django.test.TestCase):
          fixtures=['record_1000']
          def test_MyFunc(self):
              tags='Testing for My Func'
              arg = 0
              result=testingAPI._MyFunc( arg )
              self.assertEqual(result["errorcode"],"0")

              return



  4. 測試

    • # python manage.py test your_app'




參考資料



2009年10月29日 星期四

Facebook API Template Bundle - 發佈到個人塗鴉牆的樣版設定教學

今早打算編寫新的 Template 時,在 Tools 頁面發現以下訊息:


Facebook will deprecate template bundles and their related API calls December 20, 2009.
Please start using stream attachments with stream.publish, FB.Connect.streamPublish, and Facebook.streamPublish instead.


這個衝擊說大不大說小不小!影響不大是因為我還沒很善用它,但我已經有點彈性疲乏啦,因為準備要塞進 Template 的資料,弄得很疲憊。不過,該做的正事還是要完成。


目的:


在 FBML 模式下,以 FBJS 達成過去 Facebook.showFeedDialog 的效果。


解法:


使用 Facebook.streamPublish 來代替,而原先的 template 部份將變成在 Code 裡。好處是可以少一次跟 Facebook Server 要資料吧。或許這也是 template 佔用資源而被拿掉的可能性??


除此之外,還需要用到 attachment 概念,就是以前寫在 template 上的資訊


程式碼:


<script type="text/javascript">
function do_post()
{
        var attachment = {
                'name':'Quiz',
                'href':'http://apps.facebook.com/your_apps/',
                'description':'good!',
                'caption':'{*actor*} is very smart!',
                'media':[
                                {
                                        'type':'image',
                                        'src':'http://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Facebook.svg/200px-Facebook.svg.png',
                                        'href':'http://zh.wikipedia.org/wiki/Facebook'
                                }
                ]
        };
        Facebook.streamPublish( ''  , attachment , null , null , 'Message:' , null , false  );
}
</script>


接著在適當地地方呼叫 do_post(); 吧!



  1. 以 click 呼叫

    • <a href="#" onclick="do_post();return false;">Click Here!</a>



  2. 自動執行

    • <script type="text/javascript">
          do_post();
      </script>

    • 記得這段 code 要擺在 function() do_post{ ... } 後頭才用喔




收工。


參考資料:



相關文章