需要装的软件有python(废话),libnotify,sdcv,espeak(可以改用festival),xsel(可以改用xclip)。
希望能够抛砖引玉,看看大家有没有什么更好的点子。
代码: 全选
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#Author: Roy L Zuo (roylzuo at gmail dot come)
#Last Change: Sat Sep 27 07:30:22 2008 EST
#Description: various actions to perform on selected text
import os, re, urllib, urllib2, sys
string = (sys.argv[1] if len(sys.argv)==2 else (os.popen('xsel -o').read().strip()))
notifyargs = "notify-send -t 5000 -i ~/.icons/png-1009.png"
#notifyargs = "notify-send -t 5000 -i ~/.icons/hamster_128.png"
if string.isalpha():
#单词翻译
translation = os.popen('sdcv -n "%s"' %string).read()
pos = translation.find("-->%s\n" %string)
if pos != -1:
translation = translation[pos+len(string)+3:]
os.system('%s "Meoww~~, %s的意思是:" "%s"' %(notifyargs, string,translation) )
os.system('espeak %s' %string)
else:
text = '<span size="20000" color="brown" weight="bold">%s</span>' \
%('\n\n '+ '没有查到%s这个词' %string )
os.system("%s 'Meoww~~, 很抱歉:' '%s'" %(notifyargs, text) )
elif re.match("^(\d{1,3}\.){3}\d{1,3}$", string):
#ip查询
eform = urllib.urlencode({"ip":string, "action":2})
request = urllib2.Request("http://www.ip138.com/ips.asp", eform)
page = urllib2.urlopen(request).read()
address = page.decode("gb2312").encode("utf8")
address = re.search('本站主数据:(.*?)</li', address, re.DOTALL).group(1)
text = '<span size="24000" color="red" weight="bold">%s</span>' \
%('\n'+ address.replace(" ", "\n\n\t") )
os.system("%s 'Meoww~~,%s地址在:' '%s'" %(notifyargs,string,text) )
elif re.match("^1(\d){10}$", string):
#手机号归属地与运营商查询
data = urllib.urlencode({"mobile": string, "step": 2})
page = urllib.urlopen("http://flash.chinaren.com/ip/mobile.php",
data ).read().decode('gbk').encode('utf-8')
result = re.findall("tdc2>(.*?)</TD>", page)
text = '<span size="24000" color="red" weight="bold">%s</span>' \
%('\n'+result[1].replace(' ', '')+'\n\n'+result[2])
os.system("%s 'Meoww~~,%s机主在:' '%s'" %(notifyargs,string,text))
elif re.match("(https?|ftp)://[\-\.\,/%~_:?\#a-zA-Z0-9=&]+", string):
#打开url
os.system("$HOME/bin/firefox '%s'" %string)
elif string=="":
text = '<span size="24000" color="red" weight="bold">%s</span>' \
%('\n'+ 'Thou must give me a command!')
os.system("%s 'Meoww~~,' '%s'" %(notifyargs,text) )