Created
April 2, 2019 14:26
-
-
Save tamamu/dc37de0db1064bed2a272c967cfd417e to your computer and use it in GitHub Desktop.
Look the inputed word up in Eijiro on the WEB from Vim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" | |
" Required: | |
" $ pip3 install bs4 | |
" | |
function! s:alc() | |
python3 << EOF | |
import os | |
import tempfile | |
import urllib.request | |
import urllib.parse | |
import bs4 | |
import vim | |
def parseli(li): | |
children = list(filter(lambda e: not isinstance(e, str), li.children)) | |
midashi = li.select('.midashi') | |
if not midashi: | |
midashi = li.select('.midashi_je') | |
if not midashi: | |
return None | |
midashi = midashi[0].text | |
yaku = children[1].text | |
return midashi+':\n'+(''.join(map(lambda s: '\t\t'+s, yaku.split('\n')))) | |
def main(): | |
url = "https://eow.alc.co.jp/search?q=" | |
q = str(vim.eval('input("E<=>J: ")')) | |
req = urllib.request.Request(url+'+'.join(map(urllib.parse.quote, q.split()))) | |
with urllib.request.urlopen(req) as res: | |
body = res.read() | |
soup = bs4.BeautifulSoup(body, 'html.parser') | |
li = soup.select('#resultsList ul li') | |
if not li: | |
print('Not found') | |
return | |
if len(li) > 2: | |
li.pop(1) | |
text = '\n\n'.join(filter(None, map(parseli, li))) | |
fd, path = tempfile.mkstemp(prefix='alc.vim.', text=True) | |
with open(fd, 'w') as f: | |
f.write(text) | |
vim.command('split '+path) | |
return | |
main() | |
EOF | |
endfunction | |
command! Alc :call s:alc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment