" sync-isbn.vim " Author: eclipse-a (http://d.hatena.ne.jp/eclipse-a/) " Last Change: 28-Feb-2008. " scriptencoding utf-8 let s:save_cpoptions = &cpoptions set cpoptions&vim if v:version < 700 || \ !has('multi_byte') || \ !has('iconv') || \ executable('curl') != 1 let &cpoptions = s:save_cpoptions finish endif if !exists('g:sync_isbn_format') let g:sync_isbn_format = "%タイトル%\n%作者%\n%出版社%\n%発売日%\n" \ . "%メディア%\n%はてな%\n%Amazon%\n%Amazon画像%" endif let s:debug = 0 let s:log = [] let s:hatena_url = 'http://d.hatena.ne.jp/asin/' let s:amazon_url = 'http://www.amazon.co.jp/exec/obidos/ASIN/' let s:amazon_img_url = \ 'http://images-jp.amazon.com/images/P/.09.MZZZZZZZ.jpg' if s:debug nnoremap :call ShowLog() endif function! SyncIsbn(method, head, tail, opts_string) if s:debug | call filter(s:log, 0) | endif if a:method != 'pull' echoerr 'sync-isbn.vim: method ' . a:method . ' is not supported.' return endif call s:AddLog('a:head:' . a:head) call s:AddLog('a:tail:' . a:tail) call s:AddLog('getline(a:head):' . getline(a:head)) call s:AddLog('getline(a:tail):' . getline(a:tail)) call s:AddLog('a:opts_string:' . a:opts_string) let code = get(filter(split(a:opts_string), 'v:val =~ "code=.*"'), 0, '') let code = substitute(code, '-', '', 'g') let isbn = get(matchlist(code, '\ccode=\([0-9x]*\)'), 1, '') call s:AddLog('isbn:' . isbn) if isbn !~ '^\d\{13}$' && isbn !~ '^\c\d\{9}[0-9x]$' echoerr "sync-isbn.vim: '" . isbn . "' is not ISBN code." return endif if isbn =~ '^\d\{13}$' call s:AddLog("'" . isbn . "' is ISBN-13.") let hatena_url = substitute(s:hatena_url, '', isbn, 'g') let tmpfile = tempname() let cmd = 'curl -D ' . tmpfile . ' ' . hatena_url call s:AddLog(cmd) call system(cmd) let pat = '\clocation:\s*http://d.hatena.ne.jp/asin/\(\d\{9}[0-9x]\)' let loc = get(filter(readfile(tmpfile), "v:val =~ '" . pat . "'"), 0, '') call delete(tmpfile) call s:AddLog('location:' . loc) let isbn10 = get(matchlist(loc, pat), 1, '') if isbn10 == '' call s:AddLog('cannot convert ISBN-13 to ISBN-10') echoerr 'sync-isbn.vim: ISBN-13 is not supported.' return endif else let isbn10 = isbn endif call s:AddLog("'" . isbn10 . "' is ISBN-10.") let hatena_url = substitute(s:hatena_url, '', isbn10, 'g') let body = system('curl ' . hatena_url) let body = iconv(body, 'euc-jp', &enc) let body = get(matchlist(body, '\(\_.*\)'), 1, '') let title = get(matchlist(body, '

\(.\{-}\)

'), 1, '') let list_circle = [] let pat = '
    \(\_.\{-}\)
' let list_s = get(matchlist(body, pat), 1, '') if list_s == '' echoerr "sync-isbn.vim: '" . isbn10 . "' is not found." return endif let list = split(substitute(list_s, '\%(\r\|\n\|
  • \)', '', 'g'), '
  • ') let dict = {'作者': '', '出版社': '', '発売日': '', 'メディア': ''} for li in list for key in keys(dict) if li =~ key let s = li let s = substitute(s, '', '', 'g') let s = substitute(s, '', '', 'g') let dict[key] = s endif endfor endfor let amazon_img_url = substitute(s:amazon_img_url, '', isbn10, 'g') let dict['Amazon画像'] = amazon_img_url let amazon_url = substitute(s:amazon_url, '', isbn10, 'g') let dict['Amazon'] = amazon_url let hatena_url = substitute(s:hatena_url, '', isbn10, 'g') let dict['はてな'] = hatena_url let dict['タイトル'] = title " let s = g:sync_isbn_format for key in keys(dict) let s = substitute(s, '%' . key . '%', get(dict, key, ''), 'g') endfor call append(a:head - 1, split(s, "\n", 1)) return {'new_opts': 'code=' . isbn10} endfunction function! s:AddLog(log) if s:debug | call add(s:log, a:log) | endif endfunction function! s:ShowLog() for l in s:log echo 'sync-isbn.vim: ' . l endfor endfunction let &cpoptions = s:save_cpoptions " vim: set expandtab tabstop=2 shiftwidth=2: