2011-02-13

Tcl 存取 Firefox 書籤

Firefox 關於網址的記錄都存放在 places.sqlite 中,此檔案為 sqlite3 資料庫,檔案位置

Linux
/home/<user>/.mozilla/firefox/<profile>/
Win XP
C:\Documents and Settings\<user>\Application Data\Mozilla\Firefox\Profiles\<profile>\
Vista / Win7
C:\Users\<user>\AppData\Roaming\Mozilla\Firefox\Profiles\<profile>\

其中的 <profile> 格式為 "亂數.default",例如 dhyt3gd2.default,Schema 參考 The Places database

以下範例是讀出書籤中 wretch 與 xuite 網址,將使用者 id 與網站類型語名稱存入 array 。(註: 當 Firefox 正在執行時會 lock db,須先將其複製到 temp 後再存取)
package require sqlite3

set dirList [glob -directory [file join $::env(home) AppData/Roaming/Mozilla/Firefox/Profiles] -type d *]
foreach dir $dirList {
  set fBookmark [file join $dir places.sqlite]
  if { [file exists $fBookmark]  == 0 } { unset fBookmark }
}

if [info exists fBookmark] {
  set fDb [file join $::env(home) .wget bookmarkFF.db]
  file copy -force $fBookmark $fDb
  array set bookmarkList ""

  db eval {SELECT url, moz_bookmarks.title FROM moz_places, moz_bookmarks \
                WHERE moz_places.id = moz_bookmarks.fk and \
                (url like '%www.wretch.cc%' or url like '%xuite.net%')} result {
    regexp {http://([\w.]+)} $result(url) -> host
    foreach t {wretch xuite} {
      if {[string first $t $host] != -1} {
        set type $t
        break
      }
    }
    set uid ""
    switch $type {
      wretch {
        regexp {http://[\w.]+/[\w]+/([\w]+)} $result(url) -> uid
      }
      xuite {
        regexp {http://[\w.]+/([\w.]+)} $result(url) -> uid
      }
      set bookmarkList($type,[string tolower $uid]) $result(title)
    } 
  }
}
db close

沒有留言:

張貼留言