码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-08-27 23:13:48      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   使用   io   for   ar   问题   

昨天和集训队的几位大大聊天,聊着聊着就聊到了博客的问题,发现几个人要么在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         return
105     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     return
112 
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         return
122     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     return
129         
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 ——命令行下的网页收藏夹

标签:style   blog   color   os   使用   io   for   ar   问题   

原文地址:http://www.cnblogs.com/kevince/p/3936306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!