命令行更新 cnblog 博客主页副标题,可同步发到闪存或随笔评论
2012-11-12 15:11
351 查看
事情是这样的:
我喜欢把博客的副标题当作 qq 的个人签名那样用,没事把心情发到副标题上。并且可以查看以前发表的心情。
但是网页操作需要设置好些步,太麻烦。
先是打算把心情同步发到某篇随笔的评论上(cnblog 的日记不支持评论),然后发现自己的评论会把最新评论给刷了。
随后发现 cnblog 的闪存,同步到这上面不错,http://home.cnblogs.com/ing/,还能即时收到网友的评论。
使用示例:
查看博客副标题、闪存,会发现已经更新。
源代码如下:
linux / python 2.7 / 2012:11:12 15:00 update
将上面代码保存为 cnblog,把其中的 username,password 改成自己的,加上可执行权限,即可像示例那样使用。
将该程序添加为自定义命令使在任何地方可运行,详见 linux 添加管理自定义命令。
原文:/article/6987073.html
我喜欢把博客的副标题当作 qq 的个人签名那样用,没事把心情发到副标题上。并且可以查看以前发表的心情。
但是网页操作需要设置好些步,太麻烦。
先是打算把心情同步发到某篇随笔的评论上(cnblog 的日记不支持评论),然后发现自己的评论会把最新评论给刷了。
随后发现 cnblog 的闪存,同步到这上面不错,http://home.cnblogs.com/ing/,还能即时收到网友的评论。
使用示例:
➜ ~ cnblog "超级塞亚人 孙悟空" login cnblog successfully. update subtitle successfully. post shancun successfully. ➜ ~
查看博客副标题、闪存,会发现已经更新。
源代码如下:
linux / python 2.7 / 2012:11:12 15:00 update
#!/usr/bin/env python #encoding=utf-8 import sys import codecs import urllib import httplib2 #httplib2.debuglevel = 1 import re import inspect h = httplib2.Http() user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4" #{{{----------------------------------------------------- #模拟登录,得到带有登录信息的cookie def cnblog_login(username, password): login_url = 'http://passport.cnblogs.com/login.aspx' headers_base = { "User-Agent":user_agent } resp, content = h.request(login_url, headers=headers_base) #print content pat_args = re.compile(r'<input type="hidden" name="(.*?)".*?value="(.*?)" />', re.S) args = pat_args.findall(content) if not args: print 'can not find the arguments of login, check the regex!' else: args = dict(args) args.update({ "tbUserName":username, "tbPassword":password, "btnLogin":"登 录", "txtReturnUrl":"http://www.cnblogs.com/" }) #print args headers_form = { "User-Agent":user_agent, "Content-Type":"application/x-www-form-urlencoded" } resp, content = h.request(login_url, method='POST', body=urllib.urlencode(args), headers=headers_form) #print resp cookie = resp.get('set-cookie') if not cookie: print 'fail in %s!' % inspect.getframeinfo(inspect.currentframe())[2] exit() else: print 'login cnblog successfully.' return cookie #}}}-------------------------------------------------------------- def cnblog_get_config(url, headers): #cookie 错误,也返回200 resp, content = h.request(url, headers=headers) #print content pat_args = re.compile( r''' name="__VIEWSTATE".*?value="(.*?)".*? name="Edit$txbTitle".*?value="(.*?)".*?.*? name="Edit$txbSubtitle".*?>(.*?)</textarea>.*? name="Edit$ReplyNotifyMail".*?value="(.*?)".*? name="Edit$ddlTimezone".*?selected.*?value="(.*?)".*? name="Edit$ddlLangLocale".*?selected.*?value="(.*?)".*? name="Edit$ddlSkin".*?selected.*?value="(.*?)".*? name="Edit$txbSecondaryCss".*?>(.*?)</textarea>.*? name="Edit$ckbIsDisableMainCss"(.*?)>.*? #8 checked name="Edit$txbEditorCss".*?>(.*?)</textarea>.*? name="Edit$EditorBody".*?>(.*?)</textarea>.*? name="Edit$txbPageBeginHtml".*?>(.*?)</textarea>.*? name="Edit$txbPageEndHtml".*?>(.*?)</textarea>.*? name="Edit$ckbAllowServiceAccess"(.*?)>.*? #13 checked name="Edit$lkbPost".*?value="(.*?)" '''.replace('$', '\$'), re.S | re.X) args = pat_args.findall(content) if not args: print 'can not find the arguments of config!' exit() else: args = list(args[0]) for index, arg in enumerate(args): args[index] = arg.strip() if index == 8 or index == 13: if arg.find('checked') == -1: args[index] = '' else: args[index] = 'on' #print args keys = [ "__VIEWSTATE", "Edit$txbTitle", "Edit$txbSubtitle", "Edit$ReplyNotifyMail", "Edit$ddlTimezone", "Edit$ddlLangLocale", "Edit$ddlSkin", "Edit$txbSecondaryCss", "Edit$ckbIsDisableMainCss", "Edit$txbEditorCss", "Edit$EditorBody", "Edit$txbPageBeginHtml", "Edit$txbPageEndHtml", "Edit$ckbAllowServiceAccess", "Edit$lkbPost", ] args = zip(keys, args) args = dict(args) return args def cnblog_set_config(url, subtitle, args, headers): if args['Edit$txbSubtitle'] == subtitle: print "same subtitle, don't update." return None args['Edit$txbSubtitle'] = subtitle resp, content = h.request(url, method='POST', body=urllib.urlencode(args), headers=headers) if resp['status'] == '200': print 'update subtitle successfully.' return True else: print 'fail in %s!' % inspect.getframeinfo(inspect.currentframe())[2] #print resp return False #blog_id 为博客文章url中日期后的一串数字 def cnblog_post_comment(headers, blog_id, comment): url = 'http://www.cnblogs.com/mvc/PostComment/New.aspx' body = '{"postId":%s,"Body":"%s","ParentCommentID":0}' % (blog_id, comment) resp, content = h.request(url, method='POST', body=body, headers=headers) if resp['status'] == '200': print 'post comment successfully.' return True else: print 'fail in %s!' % inspect.getframeinfo(inspect.currentframe())[2] #print resp return False def cnblog_post_shancun(headers, shancun): url = 'http://home.cnblogs.com/ajax/ing/Publish' body = '{"content":"%s","publicFlag":1}' % shancun resp, content = h.request(url, method='POST', body=body, headers=headers) if resp['status'] == '200': print 'post shancun successfully.' return True else: print 'fail in %s!' % inspect.getframeinfo(inspect.currentframe())[2] #print resp return False def write_to_file(file_name, txt): with codecs.open(file_name, "w", "utf-8") as f: f.write(txt) def read_from_file(file_name): with codecs.open(file_name, "r", "utf-8") as f: txt = f.read() txt = txt.encode('utf-8') return txt if __name__ == '__main__': if len(sys.argv) != 2: print 'Usage: cnblog "update subtitle"' sys.exit() else: subtitle = sys.argv[1].strip(''' "''') username = 'your username' password = 'your password' if 0: #保存cookie,但cookie失效需手动删除cookie文件 import os cookie_txt = '/tmp/cnblog_cookie.txt' if os.path.isfile(cookie_txt): cookie = read_from_file(cookie_txt) else: cookie = cnblog_login(username, password) write_to_file(cookie_txt, cookie) else: #不保存cookie cookie = cnblog_login(username, password) headers = { "Cookie":cookie, "User-Agent":user_agent } headers_form = { "Cookie":cookie, "Content-Type":"application/x-www-form-urlencoded", "User-Agent":user_agent } headers_json = { "Cookie":cookie, "Content-Type":"application/json; charset=UTF-8", "User-Agent":user_agent } config_url = 'http://www.cnblogs.com/' + username +'/admin/Configure.aspx' config_args = cnblog_get_config(config_url, headers) flag = cnblog_set_config(config_url, subtitle, config_args, headers_form) if flag: cnblog_post_shancun(headers_json, subtitle) #cnblog_post_comment(headers_json, blog_id, subtitle) if 0: #看到有幸运闪,刚试了下连闪10下,‘我的’里面倒是显示全了,全站里只显4下,他们说,小心进小黑屋 for i in range(10): shancun = '我' + '再' * i + '闪' print shancun cnblog_post_shancun(headers_json, shancun)
将上面代码保存为 cnblog,把其中的 username,password 改成自己的,加上可执行权限,即可像示例那样使用。
将该程序添加为自定义命令使在任何地方可运行,详见 linux 添加管理自定义命令。
原文:/article/6987073.html
相关文章推荐
- 这些年收藏的技术大牛博客分享(后续还会继续更新)大家可以在评论中分享自己关注的
- 7月22日本人正在学习Java每天所学晚上都会在博客更新,如果错了可以在评论指出。
- 社区评论可以同步到个人博客了!
- 有没有博客同步更新的工具可以推荐给我
- 随时更新———个人喜欢的关于模式识别、机器学习、推荐系统、图像特征、深度学习、数值计算、目标跟踪等方面个人主页及博客
- 这些年收藏的技术大牛博客分享(后续还会继续更新)大家可以在评论中分享自己关注的
- 社区评论可以同步到个人博客了!
- 博客同步更新说明
- 选择了好久,决定在CSDN安个家,sina博客也会同步更新。
- 社区评论可以同步到个人博客了!
- 同步更新博客地址
- 博客园博问闪存首页新随笔联系管理订阅 随笔- 153 文章- 0 评论- 41 Log4j架构及概念简介
- 社区评论可以同步到个人博客了!
- 有没有博客同步更新的工具可以推荐给我
- 2008年8月 Piggy 博客同步更新
- 同步更新博客
- SAE 平台代码实现数据库定时备份以及同步到本地 2014/09/11 09:39:01 分类: 技术随笔 1人评论 次浏览 SAE 只允许用户通过phpMyAdmin管理远程数据库,
- 博客随笔目录-实时更新
- <续>jmeter随笔(22)将测试结果同步写入数据库更新包
- 【SSH网上商城项目实战15】线程、定时器同步首页数据(类似于CSDN博客定期更新排名)