Skip navigation.

Posts tagged with "web"

dontclickit

,

每天点击上千次的手指是否已经不堪重负了?

http://www.dontclick.it/

做一个书签 II

, ,

上一次完成的功能只限于:通过javascript把页面的信息发送到服务器上。现在要处理这个信息了(用数据库sqlite存储)。

因为对sqlite一窍不通,所以只是在它的tutorial里面找一些例子,依葫芦画瓢,向数据库里写内容。

这是PostRemote的doPost方法,处理信息。
    @cherrypy.expose
    def doPost(self, url="", title="", notes="", tags=""):
        con = cherrypy.thread_data.db.cursor()
        user = ""
        info = (title, url, notes, user, tags, time.time())
        con.execute("insert into bookmark (title, url, notes, user, tags, time) values (?,?,?,?,?,?)", info)
        cherrypy.thread_data.db.commit()
        con.close()
        yield '...... done!<br>'
        yield 'go back to <a href="%s">%s</a>' % (url, title)


传入的信息包括url, title, notes,tags。其中tags只是一个摆设,我也不清楚以后怎么处理它。而user则是为以后可能要添加的用户功能作准备,目前是空值。

数据库每次打开,写入,关闭,当然很费时费事,不过自己使用无所谓了。

另外,保存数据以后如何立刻转会到原来的页面?不太清楚,只好加一个链接,再次链接回去完事。

做一个书签 I

, ,

美味书签是一个社会化书签系统,在网络上非常流行,国内已经有365key之类的网站进行模仿了。当然,这些都是旧闻了。

我用了一段时间美味书签,觉得很方便,特别是它提供的post to del.icio.us按钮,可以在浏览网页的时候随时保存,另外加上一些tag,使得归类是如此的简单。比起在Opera、FireFox、IE等浏览器上的书签系统要方便很多(难以想象在本地对几百个书签进行分类、查找)。

可能美味书签的很多用户和我一样并不太关心它所提供的其他社会化功能,只是做了一个网上的书签保存而已。而且服务器在国外,偶尔会有速度慢、无法访问的问题,这是比较恼人的。所以我想到自己做一个书签如何?

对于个人的书签来说,这是非常简单的任务。在客户端的浏览器上,只需要仿照post to del.icio.us,自己做一个按钮,指向自己的服务器。在服务器上,通过特定的URL处理接收到的数据,保存。当然,其他显示、搜索的功能也是需要的,但那是之后的任务了。

做一个post按钮,链接如下 ( 服务器的网址 http://localhost:8000 )
javascript:location.href='http://localhost:8000/?url='+encodeURIComponent(location.href)+';title='+encodeURIComponent(document.title)


完成这种任务对于python来说简直是小菜一碟。除了内置的CGIHTTPServer,还有其他极其丰富的网络框架。看了啄木鸟社区上面的Web应用平台系列,试了试CherryPy感觉很有意思。

于是写下这样一段代码

import os, time
import cherrypy
from cherrytemplate import renderTemplate
from pysqlite2 import dbapi2 as sqlite

# add this before cherrypy server start
def connect(thread_index):
    cherrypy.thread_data.db = sqlite.connect("contact.db")

cherrypy.server.on_start_thread_list.append(connect)

class BasePage:
    def __init__(self, title=None):
        self.title = 'Bookmark'
        if title:
            self.title  += ' : %s' % title
            
    @cherrypy.expose
    def default(self, *args):
        return '%s not available <a href="/">go back home</a>' % repr(args)
        
    def header(self):
        return renderTemplate(file='./template/header.html')

    def navibar(self):
        return renderTemplate(file='./template/navibar.html')
    
    def footer(self):
        yield '<p><font color="grey"> %s </font></p>' % repr(cherrypy.request.headers)
        yield renderTemplate(file='./template/footer.html')


class PostRemote(BasePage):
    """ supply remote post
    you can make a button on youre bowser, whose URL should be:
    
    javascript:location.href='http://localhost:8000/post?url='+
    encodeURIComponent(location.href)+';title='+encodeURIComponent(document.title)

    where 'http://localhost:8000/post' is the url of instance of PostRemote mounted on.
    """
    def __init__(self):
        BasePage.__init__(self, "post")

    @cherrypy.expose
    def index(self, url="", title=""):
        return renderTemplate(file='./template/form_postremote.html')

    @cherrypy.expose
    def doPost(self, url="", title="", notes="", tags=""):
        return "gotcha"
    
cherrypy.root = PostRemote()

if __name__=="__main__":
    cherrypy.config.update(file = 'tutorial.conf')
    cherrypy.server.start()


使用了CherryTemplate 作简单的模板, 把html都塞过去。

在doPost方法中应该对数据处理之后存入数据库的,不过我不会用数据库,只好搁置啦 ...

捕捉颜色的程序

,

下载 3c color converter and capturer v1.02

使用简单方便,可以捕捉屏幕颜色,通过调色板选择颜色,将颜色输出为RGB或者HEX格式。