首页 > 代码库 > 【Python】iiblogs ——命令行下的网页收藏夹
【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 4 import MySQLdb 5 import sys 6 import getopt 7 import os 8 9 HOST = ‘localhost‘ 10 USER = ‘root‘ 11 PASSWORD = ‘××××××ב 12 DBNAME = ‘iiblogs‘ 13 14 BROWSER = ‘chromium-browser‘ 15 16 def Usage(): 17 print "iiblogs [name] | [option] [name]" 18 print "\tiiblogs [name] open his blog" 19 print ‘\tiiblogs [-a|--add] [name] add new address‘ 20 print ‘\tiiblogs [-d|--del] [name] delete address‘ 21 print ‘\tiiblogs [-m|--modify] [name] modify the address‘ 22 print ‘\tiiblogs [-c|--change_name] [newname] change the name‘ 23 print ‘\tiiblogs [-l|--list] list all‘ 24 print ‘\tiiblogs [-h|--help] help infomation‘ 25 return 26 27 def Connect(): 28 conn = MySQLdb.connect(host=HOST, user=USER, passwd=PASSWORD, db=DBNAME, charset = ‘utf8‘) 29 return conn 30 31 def Add(name): 32 conn = Connect() 33 mycursor = conn.cursor() 34 sql = "select name from blogs where name = %s" 35 param = (name) 36 n = mycursor.execute(sql, param) 37 if n > 0: 38 print ‘This name exists‘ 39 return 40 addr = raw_input("blog‘s address:") 41 sql = "insert into blogs values(null, %s, %s);" 42 param = (name, addr) 43 mycursor.execute(sql, param) 44 conn.commit() 45 conn.close() 46 return; 47 48 def Delete(name): 49 conn = Connect() 50 mycursor = conn.cursor() 51 sql = "delete from blogs where name = %s" 52 param = (name) 53 mycursor.execute(sql, param) 54 conn.commit() 55 conn.close() 56 return; 57 58 def Opensite(args): 59 conn = Connect() 60 mycursor = conn.cursor() 61 sql = "select address from blogs where name=%s" 62 weblist = [] 63 fail = [] 64 webs = ‘ ‘ 65 for name in args: 66 param = (name) 67 n = mycursor.execute(sql, param) 68 if n < 1: 69 print "‘%s‘ does not exist" % (name) 70 fail.append(name) 71 continue 72 else: 73 print "‘%s‘ OK." % (name) 74 for one in mycursor.fetchone(): 75 one = one.encode("utf-8") #utf8 ------------ 76 weblist.append(one) 77 if (len(weblist) == 0): 78 return 79 for index, item in enumerate(weblist): 80 webs = webs + ‘ ‘ + item 81 last = BROWSER + webs + ‘ &‘ 82 os.system(last) 83 conn.close() 84 return 85 86 def List(): 87 conn = Connect() 88 mycursor = conn.cursor() 89 sql = "select name, address from blogs" 90 mycursor.execute(sql) 91 for res in mycursor.fetchall(): 92 print res[0], ‘: ‘, res[1] 93 conn.close() 94 return 95 96 def Modify(name): 97 conn = Connect() 98 mycursor = conn.cursor() 99 sql = ‘select name from blogs where name=%s‘100 param = (name)101 n = mycursor.execute(sql, param)102 if n < 1:103 print "This name does not exist"104 return105 new = raw_input("please input the new address:")106 sql = "update blogs set address=%s where name=%s"107 param = (new, name)108 mycursor.execute(sql, param)109 conn.commit()110 conn.close()111 return112 113 def Changename(name):114 conn = Connect()115 mycursor = conn.cursor()116 sql = ‘select name from blogs where name=%s‘117 param = (name)118 n = mycursor.execute(sql, param)119 if n < 1:120 print "This name does not exist"121 return122 new = raw_input("please input the new name:")123 sql = "update blogs set name=%s where name=%s"124 param = (new, name)125 mycursor.execute(sql, param)126 conn.commit()127 conn.close()128 return129 130 try:131 opts, args = getopt.getopt(sys.argv[1:], ‘lha:d:m:c:‘, [‘list‘, ‘help‘, ‘add‘, ‘del‘, ‘modify‘, ‘change‘])132 except getopt.GetoptError:133 Usage()134 sys.exit()135 for o, a in opts:136 #a = a.decode("gbk").encode("utf-8")137 if o in (‘-h‘, ‘--help‘):138 Usage()139 sys.exit()140 if o in (‘-a‘, ‘--add‘):141 Add(a)142 sys.exit()143 if o in (‘-d‘, ‘--del‘):144 Delete(a) 145 sys.exit()146 if o in (‘-l‘, ‘--list‘):147 List()148 sys.exit()149 if o in (‘-m‘, ‘--modify‘):150 Modify(a)151 sys.exit()152 if o in (‘-c‘, ‘--change‘):153 Changename(a)154 sys.exit()155 if len(args) == 0:156 Usage()157 else:158 Opensite(args)
【Python】iiblogs ——命令行下的网页收藏夹
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。