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

    转载:http://blog.csdn.net/xu_zh_h/article/details/2294233 4 请求失败4xx 4xx应答定义了特定服务器响应的请求失败的情况.客户端不应当在不更改 ...

  2. SQL Server 中可以被锁住的 12 种资源

    第1种: DB 整个数据库 第2种: file 数据库文件 第3种: table 第4种: hobt(堆)BTree(B树) 第5种: extent 一个区(8个8KB页面) 第6种: page 数据 ...

  3. Android中Handle详解

    上图为本人总结的Handler,网上发现一片总结很好的博客就copy过来:作为参考 Handler有何作用?如何使用? 一 .Handler作用和概念 包含线程队列和消息队列,实现异步的消息处理机制, ...

  4. 伪造 UDP 包源 IP 地址

    Raw sockets 方式 raw socket 可通过参数  IPV6_HDRINCL 或 IP_HDRINCL 自定义IP头——伪造UDP报文源IP就全靠它了. 限制:从xp sp2之后的所有非 ...

  5. (转载博文)VC++API速查

    窗口处理 2.1 窗口简介 2.2.1 创建普通窗口(CreateWindow.CreateWindowEx) 2.2.2 关闭窗口(CloseWindow) 2.2.3 销毁窗口(DestroyWi ...

  6. hdu 5533 Dancing Stars on Me(数学,水)

    Problem Description The sky was brushed clean by the wind and the stars were cold in a black sky. Wh ...

  7. CentoS6.x网络配置

    一.配置文件 在CentoS系统里面,跟网络有关的主要配置文件有    1./etc/host.conf # 配置域名服务客户端的控制文件 2./etc/hosts # 配置主机名映射为IP的功能 3 ...

  8. Android自己主动化測试之Monkeyrunner用法及实例

    眼下android SDK里自带的现成的測试工具有monkey 和 monkeyrunner两个.大家别看这俩兄弟名字相像,但事实上是完全然全不同的两个工具,应用在不同的測试领域.总的来说,monke ...

  9. 【递推】【HDU2585】【hotel】

    Hotel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  10. log4j是什么

    一.什么是log4jLog4j 是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务器.NT的事 件记录器.UNIX S ...