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

软件和网站开发以及相关技术探讨
头像
roylez
帖子: 1928
注册时间: 2005-10-04 10:59
来自: 上海

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

#1

帖子 roylez » 2008-10-01 14:23

这个脚本,是我用来查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) )
附件
shot200810011651.png
shot200810011650.png
shot200810011601.png
上次由 roylez 在 2008-10-01 14:53,总共编辑 1 次。
弄个dropbox空间来备份文件或者做私人代码服务器
配置:[url]git://github.com/roylez/dotfiles.git[/url]
主页:http://roylez.heroku.com
各种稀奇玩意儿:http://dooloo.info
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

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

#2

帖子 eexpress » 2008-10-01 14:36

搞集中专制啊。我的都是分开的。翻译/查ip
● 鸣学
头像
roylez
帖子: 1928
注册时间: 2005-10-04 10:59
来自: 上海

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

#3

帖子 roylez » 2008-10-01 14:45

eexpress 写了:搞集中专制啊。我的都是分开的。翻译/查ip
年纪大了,脑子不好使了,键多了记不住。 :em20
头像
sevk
帖子: 2060
注册时间: 2007-05-08 16:26
系统: arch
来自: 火星内核某分子内某原子核内
联系:

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

#4

帖子 sevk » 2008-10-01 14:51

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


代码下载地址: http://code.google.com/p/kk-irc-bot/source/list
笔记本 :
F208S : gentoo
A460P i3G D6 : UBUNTU + WIN7
UN43D1 : UBUNTU + WIN7
1000人超级QQ群 LINUX + WIN : 31465544 或 18210387
头像
thomasxie
帖子: 317
注册时间: 2009-11-22 15:29
联系:

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

#5

帖子 thomasxie » 2010-11-29 21:20

:em11
时间就像一张网,你撒在那里,你的收获就在那里。
头像
ChenFengyuan
帖子: 770
注册时间: 2008-03-23 0:39

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

#6

帖子 ChenFengyuan » 2010-11-29 21:36

除了5楼,我都在irc里见过 :em06
坐等lisp,haskell实现。
perl的应该在git ubuntu cn里有吧。ee那里
头像
tenzu
论坛版主
帖子: 36924
注册时间: 2008-11-21 20:26

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

#7

帖子 tenzu » 2010-11-29 21:42

我是来拜主席的
头像
nmsfan
帖子: 18958
注册时间: 2009-10-16 22:46
来自: finland

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

#8

帖子 nmsfan » 2010-11-29 22:45

威武
回寝室了试试去
>>>>推Ubuntu 桌面培训~~<<<<
>>>>想加入/了解gimp汉化吗,点我吧~<<<<
——————————————————————
不推荐wubi,也不推荐你给别人推荐wubi…………
随心而为的感觉真好……
强推mayhem!!
强推ensiferum
头像
skydark
帖子: 24
注册时间: 2009-03-31 8:12

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

#10

帖子 skydark » 2010-12-01 10:04

非常给力啊!
一个小问题, 单词翻译的翻译结果里可能有'`',需要把它replace掉。
还有查询结果过长的情况时怎样处理比较好呢?
上次由 skydark 在 2010-12-02 8:19,总共编辑 1 次。
头像
hasee.wu
帖子: 2089
注册时间: 2009-10-20 21:13
来自: Pacific Western University

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

#11

帖子 hasee.wu » 2010-12-01 10:14

主席的作品,前排支持.
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

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

#12

帖子 xiooli » 2010-12-01 10:21

查中文呢?
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

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

#13

帖子 eexpress » 2010-12-01 10:38

额。又翻出这了。
我的也有 就monitor-clip.pl
支持更多,比如各种关键词的url自动下载。

回想下,这样的用法,还是蛮好的。
● 鸣学
头像
leeaman
帖子: 30702
注册时间: 2007-02-02 18:14
系统: debian sid

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

#14

帖子 leeaman » 2010-12-01 10:41

好吧,支持主席和ee大pk :em05
醉了星星,醉月亮●●●●●The Long Way To Go(*^_^*)
lubcat
帖子: 2061
注册时间: 2010-09-27 12:59

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

#15

帖子 lubcat » 2010-12-01 10:45

:em11
生活里,有很多转瞬即逝,像在车站的告别,刚刚还相互拥抱,转眼已各自天涯。很多时候,你不懂,我也不懂,就这样,说着说着就变了,听着听着就倦了,看着看着就厌了,跟着跟着就慢了,走着走着就散了,爱着爱着就淡了,想着想着就算了。
回复