Skip to content

Instantly share code, notes, and snippets.

@dreamersdw
Created October 29, 2011 16:05
Show Gist options
  • Save dreamersdw/1324703 to your computer and use it in GitHub Desktop.
Save dreamersdw/1324703 to your computer and use it in GitHub Desktop.
Linux下的划词翻译脚本(带发音)
#!/bin/bash
while true
do
cur_word=$(xclip -o | awk '{print $1}' | grep '^[a-zA-Z]\+$')
if [[ $last_word == $cur_word || -z $cur_word ]]; then
sleep 1
continue
fi
word=$cur_word
result=$(parse.py $word)
title=$(echo "$result" | head -n1)
content=$(echo "$result" | sed '1d')
notify-send "$title" "$content"
audio_url="http://dict.youdao.com/dictvoice?audio=$word"
# Play the pronunciation
mplayer "$audio_url" >> /dev/null 2>&1
last_word=$cur_word
done
#!/usr/bin/python3
import sys
from lxml.html import parse
if __name__ == '__main__':
word = sys.argv[1]
url = "http://dict.youdao.com/search?q={0}".format(word)
dom = parse(url).getroot()
try:
proun = dom.cssselect(".phonetic")[0].text
print(word + " " + proun)
meanings = dom.cssselect("#etcTrans li")
for m in meanings:
print(m.text)
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment