2010/11/10

favstarを見にいくのにいちいちブラウザあげるのが面倒くさいので、なんとかならないかなと思ってた。
普段TwitVimを使ってるんだけど、同じようにvimから見れないかなーと思って勢いで書いた。
function! s:ShowFavStar(...)
  let user = a:0 > 0 ? a:1 : exists('g:favstar_user') ? g:favstar_user : ''
  if len(user) == 0
    echohl WarningMsg
    echo "Usage:"
    echo "  :FavStar [user]"
    echo "  you can set g:favstar_user to specify default user"
    echohl None
    return
  endif
  let yql = "select * from html where url = 'http://favstar.fm/users/".user."/recent' and xpath = '//div[@class=\"tweetContainer\"]'"
  let res = http#get("http://query.yahooapis.com/v1/public/yql", {'q': yql})
  let dom = xml#parse(res.content)
  for item in dom.childNode('results').childNodes('div')
    let tweet = item.find('div', {"class": "theTweet"})
    let text = substitute(tweet.childNode('p').value(), "\n", " ", "g")
    let text = substitute(text, "^ *", "", "")
    echohl Function
    echo text
    echohl None
    let actions = item.findAll('div', {"class": "avatarList"})
    for action in actions
      let line = ''
      if action.attr['id'] =~ "^faved_by_"
        let line .= "FAV:"
      elseif action.attr['id'] =~ "^rt_by_"
        let line .= "RT:"
      endif
      for a in action.findAll('img')
        let line .= " " . a.attr['alt']
      endfor
      echohl Statement
      echo line
      echohl None
    endfor
    echo "\n"
  endfor
endfunction

command! -nargs=? FavStar call <SID>ShowFavStar(<f-args>)
ソースはたったこれだけ。いちおうリポジトリのリンク
mattn's favstar-vim at master - GitHub

vimscript for favstar

http://github.com/mattn/favstar-vim
実行には webapi-vim が必要です。webapi-vim は更新が多いと思うので pathogen とかで使った方がよいです。
mattn's webapi-vim at master - GitHub

vim interface to Web API

http://github.com/mattn/webapi-vim
html(実際にはxml)をDOMでスクレイピングしてます。webapi-vimのxml.vimさまさまです。誰が書いたの?スゲーな。えっ俺ですか。俺スゲー?
で、YQLを使ってスクレイピングしているのはHTMLは正規化されていないのでYQLに正規化してもらっています。
実行は :FavStar otsune
とするかvimrcに let g:favstar_user = "otsune"
と書いておいて :FavStar
とする等して下さい。動かなかったとしても大丈夫だ。問題ない。

実行するとこんな画面になります。
favstar-vim
ご活用下さい。もうvimscriptなんでもアリな気がしてきた。
Posted at 02:36 | WriteBacks () | Edit
Edit this entry...

wikieditish message: Ready to edit this entry.






















A quick preview will be rendered here when you click "Preview" button.