分页: 1 / 2

抛砖引玉--万用查询脚本

发表于 : 2008-10-01 14:23
roylez
这个脚本,是我用来查IP,手机归属地,单词,以及打开选中的url的。使用的时候用鼠标选中要查的内容,按一个快捷键调用就行了(我把它绑给了thinkpad的ACCESS IBM键)。效果见附件。
需要装的软件有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('&nbsp;', '')+'\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) )

Re: 抛砖引玉--万用查询脚本

发表于 : 2008-10-01 14:36
eexpress
搞集中专制啊。我的都是分开的。翻译/查ip

Re: 抛砖引玉--万用查询脚本

发表于 : 2008-10-01 14:45
roylez
eexpress 写了:搞集中专制啊。我的都是分开的。翻译/查ip
年纪大了,脑子不好使了,键多了记不住。 :em20

Re: 抛砖引玉--万用查询脚本

发表于 : 2008-10-01 14:51
sevk
我也写了个类似的代码,不过是在IRC里用的.
我是初学RUBY,代码比较粗糙.
目前已经实现:
1,google\百度查询
2,中英文单词\整句互译
3,拼音整句查询
4,股票查询
5,IP查询
6,天气查询
7,URL标题查询
8,简单的ruby脚本计算.


代码下载地址: http://code.google.com/p/kk-irc-bot/source/list

Re: 抛砖引玉--万用查询脚本

发表于 : 2010-11-29 21:20
thomasxie
:em11

Re: 抛砖引玉--万用查询脚本

发表于 : 2010-11-29 21:36
ChenFengyuan
除了5楼,我都在irc里见过 :em06
坐等lisp,haskell实现。
perl的应该在git ubuntu cn里有吧。ee那里

Re: 抛砖引玉--万用查询脚本

发表于 : 2010-11-29 21:42
tenzu
我是来拜主席的

Re: 抛砖引玉--万用查询脚本

发表于 : 2010-11-29 22:45
nmsfan
威武
回寝室了试试去

Re: 抛砖引玉--万用查询脚本

发表于 : 2010-11-29 23:13
photor
:em09

Re: 抛砖引玉--万用查询脚本

发表于 : 2010-12-01 10:04
skydark
非常给力啊!
一个小问题, 单词翻译的翻译结果里可能有'`',需要把它replace掉。
还有查询结果过长的情况时怎样处理比较好呢?

Re: 抛砖引玉--万用查询脚本

发表于 : 2010-12-01 10:14
hasee.wu
主席的作品,前排支持.

Re: 抛砖引玉--万用查询脚本

发表于 : 2010-12-01 10:21
xiooli
查中文呢?

Re: 抛砖引玉--万用查询脚本

发表于 : 2010-12-01 10:38
eexpress
额。又翻出这了。
我的也有 就monitor-clip.pl
支持更多,比如各种关键词的url自动下载。

回想下,这样的用法,还是蛮好的。

Re: 抛砖引玉--万用查询脚本

发表于 : 2010-12-01 10:41
leeaman
好吧,支持主席和ee大pk :em05

Re: 抛砖引玉--万用查询脚本

发表于 : 2010-12-01 10:45
lubcat
:em11