【Python】iiblogs ——命令行下的网页收藏夹

昨天和集训队的几位大大聊天,聊着聊着就聊到了博客的问题,发现几个人要么在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
);

 

 

  1 #!/usr/bin/python
  2 #coding=utf-8
  3 #python 2.7.6
  4 
  5 import MySQLdb
  6 import sys
  7 import getopt
  8 import os
  9 
 10 HOST = 'localhost'
 11 USER = 'root'
 12 PASSWORD = '×××××××'
 13 DBNAME = 'iiblogs'
 14 
 15 BROWSER = 'chromium-browser'
 16 
 17 def Usage():
 18     print "iiblogs [name] |  [option] [name]"
 19     print "\tiiblogs [name]                open his blog"
 20     print '\tiiblogs [-a|--add] [name]        add new address'
 21     print '\tiiblogs [-d|--del] [name]        delete address'
 22     print '\tiiblogs [-m|--modify] [name]        modify the address'
 23     print '\tiiblogs [-c|--change_name] [newname]     change the name'
 24     print '\tiiblogs [-l|--list]            list all'
 25     print '\tiiblogs [-h|--help]            help infomation'
 26     return
 27 
 28 def Connect():
 29     conn = MySQLdb.connect(host=HOST, user=USER, passwd=PASSWORD, db=DBNAME, charset = 'utf8')
 30     return conn
 31 
 32 def Add(name):
 33     conn = Connect()
 34     mycursor = conn.cursor()
 35     sql = "select name from blogs where name = %s"
 36     param = (name)
 37     n = mycursor.execute(sql, param)
 38     if n > 0:
 39         print 'This name exists'
 40         return
 41     addr = raw_input("blog's address:")
 42     sql = "insert into blogs values(null, %s, %s);"
 43     param = (name, addr)
 44     mycursor.execute(sql, param)
 45     conn.commit()
 46     conn.close()
 47     return;    
 48     
 49 def Delete(name):
 50     conn = Connect()
 51     mycursor = conn.cursor()
 52     sql = "delete from blogs where name = %s"
 53     param = (name)
 54     mycursor.execute(sql, param)
 55     conn.commit()
 56     conn.close()
 57     return;
 58 
 59 def Opensite(args):
 60     conn = Connect()
 61     mycursor = conn.cursor()
 62     sql = "select address from blogs where name=%s"
 63     weblist = []
 64     fail = []
 65     webs = ' '
 66     for name in args:
 67         param = (name)
 68         n = mycursor.execute(sql, param)
 69         if n < 1:
 70             print "'%s' does not exist" % (name)
 71             fail.append(name)
 72             continue
 73         else:
 74             print "'%s' OK." % (name)
 75         for one in mycursor.fetchone():
 76             one = one.encode("utf-8")    #utf8 ------------
 77             weblist.append(one)            
 78     if (len(weblist) == 0):
 79         return
 80     for index, item in enumerate(weblist):
 81         webs = webs + ' ' + item
 82     last = BROWSER + webs + ' &'
 83     os.system(last)
 84     conn.close()
 85     return
 86 
 87 def List():
 88     conn = Connect()
 89     mycursor = conn.cursor()
 90     sql = "select name, address from blogs"
 91     mycursor.execute(sql)
 92     for res in mycursor.fetchall():
 93         print res[0], ':    ', res[1]
 94     conn.close()
 95     return
 96 
 97 def Modify(name):
 98     conn = Connect()
 99     mycursor = conn.cursor()
100     sql = 'select name from blogs where name=%s'
101     param = (name)
102     n = mycursor.execute(sql, param)
103     if n < 1:
104         print "This name does not exist"
105         return
106     new = raw_input("please input the new address:")
107     sql = "update blogs set address=%s where name=%s"
108     param = (new, name)
109     mycursor.execute(sql, param)
110     conn.commit()
111     conn.close()
112     return
113 
114 def Changename(name):
115     conn = Connect()
116     mycursor = conn.cursor()
117     sql = 'select name from blogs where name=%s'
118     param = (name)
119     n = mycursor.execute(sql, param)
120     if n < 1:
121         print "This name does not exist"
122         return
123     new = raw_input("please input the new name:")
124     sql = "update blogs set name=%s where name=%s"
125     param = (new, name)
126     mycursor.execute(sql, param)
127     conn.commit()
128     conn.close()
129     return
130         
131 try:
132     opts, args = getopt.getopt(sys.argv[1:], 'lha:d:m:c:', ['list', 'help', 'add', 'del', 'modify', 'change'])
133 except getopt.GetoptError:
134     Usage()
135     sys.exit()
136 for o, a in opts:
137 #a = a.decode("gbk").encode("utf-8")
138     if o in ('-h', '--help'):
139         Usage()
140         sys.exit()
141     if o in ('-a', '--add'):
142         Add(a)
143         sys.exit()
144     if o in ('-d', '--del'):
145         Delete(a)    
146         sys.exit()
147     if o in ('-l', '--list'):
148         List()
149         sys.exit()
150     if o in ('-m', '--modify'):
151         Modify(a)
152         sys.exit()
153     if o in ('-c', '--change'):
154         Changename(a)
155         sys.exit()
156 if len(args) == 0:
157     Usage()
158 else:
159     Opensite(args)

 

posted @ 2014-08-27 22:42  Prime-Kv  阅读(328)  评论(0编辑  收藏  举报