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

【Python】iiblogs ——命令行下的网页收藏夹的更多相关文章

  1. [兴趣使然]用python在命令行下画jandan像素超载鸡

    下午刷煎蛋的时候看到 Dthalo 蛋友发的系列像素超载鸡,就想自己试试用python脚本画一个,老男孩视频里的作业真没兴趣,弄不好吧没意思,往好了写,自己控制不好,能力不够. 所以还是找自己有兴趣的 ...

  2. Ubuntu:命令行下浏览网页

    前述 兴起,试一下不用图形化界面浏览 安装w3m 直接进入root账号 apt-get install w3m 检验是否成功 w3m www.baidu.com 就这样成功的进入baidu了,纯文本模 ...

  3. ubuntu 命令行下查看网页 w3m

    w3m localhost/index.php

  4. window的cmd命令行下新增/删除文件夹及文件

    新增文件夹 (md / mkdir) md <folderName>: folderName 就是文件路径,只输入文件夹名称时表示在当前目录下创建文件夹. 比如:md F:\test\pr ...

  5. Windows命令行下pip安装python whl包

    因为做网页爬虫,需要用到一个爬新闻的BeautifulSoup 的包,然后再关网上下的是whl包,第一次装,虽然花了点时间,最后还是装上去了,记录一下,方便下次. 先发一下官方文档地址.http:// ...

  6. python命令行下tab键补全命令

    在python命令行下不能使用tab键将命令进行补全,手动输入又很容易出错. 解决:tab.py #/usr/bin/env python # -*- coding:utf-8 -*- ''' 该模块 ...

  7. Python安装后在CMD命令行下出现“应用程序无法启动.............”问题

    问题存在之一:系统是刚刚重做的精简版服务器系统(阉割版) AN就是在阿里云上刚开的Windows Server 2008 系统上碰到的  吓尿了都 症状:            正常安装python环 ...

  8. 【Python】iichats —— 命令行下的局域网聊天程序

    转载请声明出处:http://www.cnblogs.com/kevince/p/3941728.html   ——By Kevince ii系列工具第三弹,命令行下的局域网聊天程序 原理: 程序启动 ...

  9. 命令行下查看python和numpy的版本和安装位置

    命令行下查看python和numpy的版本和安装位置 1.查看python版本 方法一: python -V 注意:‘-V‘中‘V’为大写字母,只有一个‘-’ 方法二: python --versio ...

随机推荐

  1. Problem 1008 Hay Points

    Problem Description Each employee of a bureaucracy has a job description - a few paragraphs that des ...

  2. 使用Python管理Azure(1):基础配置

    Azure提供了丰富的Python SDK来对Azure进行开发管理,包括使用Azure的开源框架在Azure上创建web应用程序,对Azure的虚拟机,存储等进行管理,本系类会简单介绍如何在ASM和 ...

  3. PHP中的错误处理

    程序只要在运行,就免不了会出现错误!或早或晚,只是时间问题罢了. 错误很常见,比如Notice,Warning等等.此时一般使用set_error_handler来处理: <?php set_e ...

  4. Ubuntu 安装 Eclipse C/C++开发环境

    所需软件清单: 1.eclipse-linuxtools-indigo-SR1-incubation-linux-gtk.tar.gz 2.jre-7u2-linux-i586.tar.gz 先将上述 ...

  5. unity3D 锁屏再开程序崩溃

    在Uniyt3d 调用Android Plugin 的时候,会出现锁屏后再开,程序就崩溃的的现象,解决办法就是在 AndroidManifest.xml 加入  android:configChang ...

  6. Easy UI treegrid 分页实例

    转自:http://www.jeasyuicn.com/jquery-easyui-treegird-page-processing.html

  7. 《编写高质量代码 改善Java程序的151个建议》书摘

    例子1:三元操作符的陷阱 int i = 80; String str1 = String.valueOf(i < 100 ? 90 : 100); String str2 = String.v ...

  8. iOS 四种延时的方法

    - (void)initBlock{     //延时的方法     //1:GCD延时 此方式在能够在參数中选择运行的线程. 是一种非堵塞的运行方式,没有找到取消运行的方法.     double ...

  9. 实用的JavaScript技巧、窍门和最佳实践

    JavaScript是世界上第一的编程语言,它是Web的语言,是移动混合应用(mobile hybrid apps)的语言(比如 PhoneGap或者 Appcelerator),是服务器端的语言(比 ...

  10. springmvc jstl

    springmvc运用maven的jetty插件运行成功,部署在tomcat6报错:ClassNotFoundException: javax.servlet.jsp.jstl.core.Config ...