昨天和集训队的几位大大聊天,聊着聊着就聊到了博客的问题,发现几个人要么在CSDN 要么在博客园上, 要记住他们的所有的地址还真是不便,于是灵机一动,何不自己写一款小工具来存储打开他们的博客呢?于是将这款工具取名为iiblogs,意为ii系列的博客工具,其实本质上就是个收藏夹,打开某位大牛博客的方法就是直接终端下输入:iiblogs [大牛的名字] 。

各种操作比如添加,删除,修改,改名都可以在使用选项来完成,比如

增加-a --add

删除-d --del

修改-m --modify

改名-c --change

考虑到锻炼自己的原因,存储结构选择了MySQL,虽说大材小用,但对程序总体的性能和稳定性上贡献还是比较大的。

下面给出代码:

MySQL 建库语句(考虑到中文存储问题,默认utf8):

drop database if exists iiblogs;

CREATE DATABASE iiblogs DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
use iiblogs; create table blogs
(
id int unsigned auto_increment primary key,
name varchar(50) not null,
address varchar(100) not null
);
 #!/usr/bin/python
#coding=utf-8
#python 2.7.6 import MySQLdb
import sys
import getopt
import os HOST = 'localhost'
USER = 'root'
PASSWORD = '×××××××'
DBNAME = 'iiblogs' BROWSER = 'chromium-browser' def Usage():
print "iiblogs [name] | [option] [name]"
print "\tiiblogs [name] open his blog"
print '\tiiblogs [-a|--add] [name] add new address'
print '\tiiblogs [-d|--del] [name] delete address'
print '\tiiblogs [-m|--modify] [name] modify the address'
print '\tiiblogs [-c|--change_name] [newname] change the name'
print '\tiiblogs [-l|--list] list all'
print '\tiiblogs [-h|--help] help infomation'
return def Connect():
conn = MySQLdb.connect(host=HOST, user=USER, passwd=PASSWORD, db=DBNAME, charset = 'utf8')
return conn def Add(name):
conn = Connect()
mycursor = conn.cursor()
sql = "select name from blogs where name = %s"
param = (name)
n = mycursor.execute(sql, param)
if n > 0:
print 'This name exists'
return
addr = raw_input("blog's address:")
sql = "insert into blogs values(null, %s, %s);"
param = (name, addr)
mycursor.execute(sql, param)
conn.commit()
conn.close()
return; def Delete(name):
conn = Connect()
mycursor = conn.cursor()
sql = "delete from blogs where name = %s"
param = (name)
mycursor.execute(sql, param)
conn.commit()
conn.close()
return; def Opensite(args):
conn = Connect()
mycursor = conn.cursor()
sql = "select address from blogs where name=%s"
weblist = []
fail = []
webs = ' '
for name in args:
param = (name)
n = mycursor.execute(sql, param)
if n < 1:
print "'%s' does not exist" % (name)
fail.append(name)
continue
else:
print "'%s' OK." % (name)
for one in mycursor.fetchone():
one = one.encode("utf-8") #utf8 ------------
weblist.append(one)
if (len(weblist) == 0):
return
for index, item in enumerate(weblist):
webs = webs + ' ' + item
last = BROWSER + webs + ' &'
os.system(last)
conn.close()
return def List():
conn = Connect()
mycursor = conn.cursor()
sql = "select name, address from blogs"
mycursor.execute(sql)
for res in mycursor.fetchall():
print res[0], ': ', res[1]
conn.close()
return def Modify(name):
conn = Connect()
mycursor = conn.cursor()
sql = 'select name from blogs where name=%s'
param = (name)
n = mycursor.execute(sql, param)
if n < 1:
print "This name does not exist"
return
new = raw_input("please input the new address:")
sql = "update blogs set address=%s where name=%s"
param = (new, name)
mycursor.execute(sql, param)
conn.commit()
conn.close()
return def Changename(name):
conn = Connect()
mycursor = conn.cursor()
sql = 'select name from blogs where name=%s'
param = (name)
n = mycursor.execute(sql, param)
if n < 1:
print "This name does not exist"
return
new = raw_input("please input the new name:")
sql = "update blogs set name=%s where name=%s"
param = (new, name)
mycursor.execute(sql, param)
conn.commit()
conn.close()
return try:
opts, args = getopt.getopt(sys.argv[1:], 'lha:d:m:c:', ['list', 'help', 'add', 'del', 'modify', 'change'])
except getopt.GetoptError:
Usage()
sys.exit()
for o, a in opts:
#a = a.decode("gbk").encode("utf-8")
if o in ('-h', '--help'):
Usage()
sys.exit()
if o in ('-a', '--add'):
Add(a)
sys.exit()
if o in ('-d', '--del'):
Delete(a)
sys.exit()
if o in ('-l', '--list'):
List()
sys.exit()
if o in ('-m', '--modify'):
Modify(a)
sys.exit()
if o in ('-c', '--change'):
Changename(a)
sys.exit()
if len(args) == 0:
Usage()
else:
Opensite(args)
05-06 11:22