Skip to content

Instantly share code, notes, and snippets.

@mmisono
Created June 9, 2013 04:57
Show Gist options
  • Save mmisono/5737714 to your computer and use it in GitHub Desktop.
Save mmisono/5737714 to your computer and use it in GitHub Desktop.
"fetch lyrics from lyrics.wikia.com
function! s:fetch_lyrics_from_lyricswiki(artist,title)
let artist = webapi#http#encodeURI(a:artist)
let title = webapi#http#encodeURI(a:title)
let url = "http://lyrics.wikia.com/api.php?action=lyrics&fmt=xml&".
\"func=getSong&artist=".artist."&song=".title
let r = webapi#http#get(url)['content']
if r =~ "<lyrics>Not found</lyrics>"
return []
endif
let start = stridx(r,"<url>")
let end = stridx(r,"</url>")
let url = r[start+5 : end-1]
let r = webapi#http#get(url)['content']
let start = stridx(r,"'17'/></a></div>")
let end = stridx(r,"<!--",start)
let lyrics = split(webapi#html#decodeEntityReference(
\r[start+16 : end-1]),'<br />')
let end = lyrics[-1][:-2]
let lyrics_ = lyrics[:-2]
call add(lyrics_,end)
return lyrics_
endfunction
" fetch lyrics from utamap
function! s:fetch_lyrics_from_utamap(artist,title)
let id = s:get_lyrics_id(a:artist,a:title)
if id == -1
return []
endif
let url = "http://www.utamap.com/phpflash/flashfalsephp.php?unum=".id
let lyrics = split(webapi#http#get(url)['content'],"\n")
let lyrics[0] = substitute(lyrics[0],"test1=\\d\\+&test2=","","g")
return lyrics
endfunction
function! s:get_lyrics_id(artist,title)
let cnt=0
let artist = webapi#http#encodeURI(a:artist)
let title = iconv(a:title,&encoding,"utf-8")
while 1
let url = "http://www.utamap.com/searchkasi.php?searchname=artist"
\."&act=search&sortname=1&pattern=3"
\."&word=".artist
\."&page=".cnt
let html = split(webapi#http#get(url)['content'],"\n")
let match = 0
for line in html
let id = matchstr(line,"showkasi.php?surl=\\zs.\\+\\ze\">")
let title_ = matchstr(line,"showkasi.php?surl=.\\+\">\\zs.\\+\\ze</A>")
let title_ = iconv(title_,"euc_jp","utf-8")
if id != ''
let match = 1
if title == title_
return id
endif
endif
endfor
if !match | break| endif
let cnt += 1
endwhile
return -1
endfunction
let g:use_lyrics_cache = 1
function! Fetch_lyrics(artist,title)
let name = "==Lyrics: ".a:artist." - ".a:title."=="
let bufnr = bufnr(name)
if bufnr != -1
let winnr = bufwinnr(bufnr)
if winnr != -1
exe "normal \<c-w>".winnr."w"
exe "normal \<c-w>p"
return
else
let buflist = tabpagebuflist()
for i in buflist
if bufname(i) =~ "^==Lyrics:"
let lyrics_winnr = bufwinnr(i)
exe "normal \<c-w>".lyrics_winnr."w"
exe "buffer ".bufnr
exe "normal \<c-w>p"
return
endif
endfor
silent botright vertical split
exe "buffer ".bufnr
exe "normal \<c-w>p"
return
endif
endif
let lyrics_func = [
\"fetch_lyrics_from_lyricswiki",
\"fetch_lyrics_from_utamap",
\]
for func in lyrics_func
let lyrics = s:{func}(a:artist,a:title)
if lyrics != [] | break | endif
endfor
if lyrics == []
echo "not found"
return
endif
let buflist = tabpagebuflist()
let lyrics_buf_found = 0
for i in buflist
if bufname(i) =~ "^==Lyrics:"
let lyrics_winnr = bufwinnr(i)
exe "normal \<c-w>".lyrics_winnr."w"
exe "e `=name`"
let lyrics_buf_found = 1
break
endif
endfor
if lyrics_buf_found == 0
exe "silent botright vertical split `=name`"
endif
call append(0,lyrics)
call append(0,[a:artist." - ".a:title,""])
setl nomodified
setl noswapfile
setl nonumber
setl buftype=nowrite
if g:use_lyrics_cache == 0
setl bufhidden=delete
endif
normal gg
exe "normal \<C-w>p"
endfunction
" get lyrics of the song currently played on iTunes {{{2
function! s:get_iTunes_lyrics()
let artist = system("osascript -e 'tell app \"iTunes\"'
\ -e 'artist of current track' -e 'end tell'")[:-2]
let title = system("osascript -e 'tell app \"iTunes\"'
\ -e 'name of current track' -e 'end tell'")[:-2]
echo artist . " : " . title
call Fetch_lyrics(artist,title)
endfunction
command! GetITunesLyrics call s:get_iTunes_lyrics()
nnoremap <leader>l :GetITunesLyrics<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment