【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
);
#!/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 ——命令行下的网页收藏夹的更多相关文章
- [兴趣使然]用python在命令行下画jandan像素超载鸡
下午刷煎蛋的时候看到 Dthalo 蛋友发的系列像素超载鸡,就想自己试试用python脚本画一个,老男孩视频里的作业真没兴趣,弄不好吧没意思,往好了写,自己控制不好,能力不够. 所以还是找自己有兴趣的 ...
- Ubuntu:命令行下浏览网页
前述 兴起,试一下不用图形化界面浏览 安装w3m 直接进入root账号 apt-get install w3m 检验是否成功 w3m www.baidu.com 就这样成功的进入baidu了,纯文本模 ...
- ubuntu 命令行下查看网页 w3m
w3m localhost/index.php
- window的cmd命令行下新增/删除文件夹及文件
新增文件夹 (md / mkdir) md <folderName>: folderName 就是文件路径,只输入文件夹名称时表示在当前目录下创建文件夹. 比如:md F:\test\pr ...
- Windows命令行下pip安装python whl包
因为做网页爬虫,需要用到一个爬新闻的BeautifulSoup 的包,然后再关网上下的是whl包,第一次装,虽然花了点时间,最后还是装上去了,记录一下,方便下次. 先发一下官方文档地址.http:// ...
- python命令行下tab键补全命令
在python命令行下不能使用tab键将命令进行补全,手动输入又很容易出错. 解决:tab.py #/usr/bin/env python # -*- coding:utf-8 -*- ''' 该模块 ...
- Python安装后在CMD命令行下出现“应用程序无法启动.............”问题
问题存在之一:系统是刚刚重做的精简版服务器系统(阉割版) AN就是在阿里云上刚开的Windows Server 2008 系统上碰到的 吓尿了都 症状: 正常安装python环 ...
- 【Python】iichats —— 命令行下的局域网聊天程序
转载请声明出处:http://www.cnblogs.com/kevince/p/3941728.html ——By Kevince ii系列工具第三弹,命令行下的局域网聊天程序 原理: 程序启动 ...
- 命令行下查看python和numpy的版本和安装位置
命令行下查看python和numpy的版本和安装位置 1.查看python版本 方法一: python -V 注意:‘-V‘中‘V’为大写字母,只有一个‘-’ 方法二: python --versio ...
随机推荐
- Substrings(hd1238)
Substrings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- MYSQL 表分区的 3 方法
背景知识: 表分区是把逻辑上同一范围的数据保存到同一个文件中,就和超市一样,把同类商品放在同一个区域,把不同的商品放在不同的地方.不同的是超市中 是根据用途分类的,表分区是根据它的取值区间来分的. 分 ...
- AprioriTID algorithm
What is AprioriTID? AprioriTID is an algorithm for discovering frequent itemsets (groups of items ap ...
- QProgressBar和QProgressDialog的简单实用
在QT中可以用QProgressBar或着QProgressDialog来实现进度条. QProgressBar的使用 首先在designer中拖一个按钮和进度条部件,按下面初始化 ui->pr ...
- tls和ssl
一个存在于 SSL 3.0 协议中的新漏洞于被披露,通过此漏洞,第三方可以拦截通过采用 SSL 3.0 的服务器传输的重要信息. 问题出在哪里? 与此问题相关的不是 SSL 证书本身,而是进行加密处理 ...
- wordpress All in one Seo
原文地址:http://www.7adesign.com/155.html WordPress插件All-in-one-seo-pack详细设置: I enjoy this plugin and ha ...
- [Leetcode][Python]44:Wildcard Matching
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...
- C++匈牙利命名法
匈牙利命名法 匈牙利命名法是一种编程时的命名规范.基本原则是:变量名=属性+类型+对象描述,其中每一对象的名称都要求有明确含义,可以取对象名字全称或名字的一部分.命名要基于容易记忆容易理解的原则.保证 ...
- odd number、 even number
odd number 奇数 even number 偶数
- 如何用SQL SERVER 2005连接SQL SERVER 2008
原先使用sql server 2005数据库,后来由于工作需要升级为sql server 2008 开发版,升级过程很简单,基本没有什么问题 下面主要说说,如何使用sql server 2005 st ...