事情是这样的:
我喜欢把博客的副标题当作 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
1 #!/usr/bin/env python 2 #encoding=utf-8 3 4 import sys 5 import codecs 6 import urllib 7 import httplib2 8 #httplib2.debuglevel = 1 9 import re 10 import inspect 11 12 h = httplib2.Http() 13 user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4" 14 15 #{{{----------------------------------------------------- 16 #模拟登录,得到带有登录信息的cookie 17 def cnblog_login(username, password): 18 login_url = 'http://passport.cnblogs.com/login.aspx' 19 headers_base = { 20 "User-Agent":user_agent 21 } 22 resp, content = h.request(login_url, headers=headers_base) 23 #print content 24 25 pat_args = re.compile(r'<input type="hidden" name="(.*?)".*?value="(.*?)" />', re.S) 26 args = pat_args.findall(content) 27 if not args: 28 print 'can not find the arguments of login, check the regex!' 29 else: 30 args = dict(args) 31 32 args.update({ 33 "tbUserName":username, 34 "tbPassword":password, 35 "btnLogin":"登 录", 36 "txtReturnUrl":"http://www.cnblogs.com/" 37 }) 38 #print args 39 40 headers_form = { 41 "User-Agent":user_agent, 42 "Content-Type":"application/x-www-form-urlencoded" 43 } 44 resp, content = h.request(login_url, method='POST', body=urllib.urlencode(args), headers=headers_form) 45 #print resp 46 cookie = resp.get('set-cookie') 47 48 if not cookie: 49 print 'fail in %s!' % inspect.getframeinfo(inspect.currentframe())[2] 50 exit() 51 else: 52 print 'login cnblog successfully.' 53 54 return cookie 55 #}}}-------------------------------------------------------------- 56 57 def cnblog_get_config(url, headers): 58 #cookie 错误,也返回200 59 resp, content = h.request(url, headers=headers) 60 #print content 61 62 pat_args = re.compile( 63 r''' 64 name="__VIEWSTATE".*?value="(.*?)".*? 65 name="Edit$txbTitle".*?value="(.*?)".*?.*? 66 name="Edit$txbSubtitle".*?>(.*?)</textarea>.*? 67 name="Edit$ReplyNotifyMail".*?value="(.*?)".*? 68 name="Edit$ddlTimezone".*?selected.*?value="(.*?)".*? 69 name="Edit$ddlLangLocale".*?selected.*?value="(.*?)".*? 70 name="Edit$ddlSkin".*?selected.*?value="(.*?)".*? 71 name="Edit$txbSecondaryCss".*?>(.*?)</textarea>.*? 72 name="Edit$ckbIsDisableMainCss"(.*?)>.*? #8 checked 73 name="Edit$txbEditorCss".*?>(.*?)</textarea>.*? 74 name="Edit$EditorBody".*?>(.*?)</textarea>.*? 75 name="Edit$txbPageBeginHtml".*?>(.*?)</textarea>.*? 76 name="Edit$txbPageEndHtml".*?>(.*?)</textarea>.*? 77 name="Edit$ckbAllowServiceAccess"(.*?)>.*? #13 checked 78 name="Edit$lkbPost".*?value="(.*?)" 79 '''.replace('$', '\$'), re.S | re.X) 80 args = pat_args.findall(content) 81 if not args: 82 print 'can not find the arguments of config!' 83 exit() 84 else: 85 args = list(args[0]) 86 87 for index, arg in enumerate(args): 88 args[index] = arg.strip() 89 if index == 8 or index == 13: 90 if arg.find('checked') == -1: 91 args[index] = '' 92 else: 93 args[index] = 'on' 94 #print args 95 96 keys = [ 97 "__VIEWSTATE", 98 "Edit$txbTitle", 99 "Edit$txbSubtitle", 100 "Edit$ReplyNotifyMail", 101 "Edit$ddlTimezone", 102 "Edit$ddlLangLocale", 103 "Edit$ddlSkin", 104 "Edit$txbSecondaryCss", 105 "Edit$ckbIsDisableMainCss", 106 "Edit$txbEditorCss", 107 "Edit$EditorBody", 108 "Edit$txbPageBeginHtml", 109 "Edit$txbPageEndHtml", 110 "Edit$ckbAllowServiceAccess", 111 "Edit$lkbPost", 112 ] 113 args = zip(keys, args) 114 args = dict(args) 115 116 return args 117 118 def cnblog_set_config(url, subtitle, args, headers): 119 if args['Edit$txbSubtitle'] == subtitle: 120 print "same subtitle, don't update." 121 return None 122 args['Edit$txbSubtitle'] = subtitle 123 resp, content = h.request(url, method='POST', body=urllib.urlencode(args), headers=headers) 124 if resp['status'] == '200': 125 print 'update subtitle successfully.' 126 return True 127 else: 128 print 'fail in %s!' % inspect.getframeinfo(inspect.currentframe())[2] 129 #print resp 130 return False 131 132 #blog_id 为博客文章url中日期后的一串数字 133 def cnblog_post_comment(headers, blog_id, comment): 134 url = 'http://www.cnblogs.com/mvc/PostComment/New.aspx' 135 body = '{"postId":%s,"Body":"%s","ParentCommentID":0}' % (blog_id, comment) 136 resp, content = h.request(url, method='POST', body=body, headers=headers) 137 if resp['status'] == '200': 138 print 'post comment successfully.' 139 return True 140 else: 141 print 'fail in %s!' % inspect.getframeinfo(inspect.currentframe())[2] 142 #print resp 143 return False 144 145 def cnblog_post_shancun(headers, shancun): 146 url = 'http://home.cnblogs.com/ajax/ing/Publish' 147 body = '{"content":"%s","publicFlag":1}' % shancun 148 resp, content = h.request(url, method='POST', body=body, headers=headers) 149 if resp['status'] == '200': 150 print 'post shancun successfully.' 151 return True 152 else: 153 print 'fail in %s!' % inspect.getframeinfo(inspect.currentframe())[2] 154 #print resp 155 return False 156 157 158 def write_to_file(file_name, txt): 159 with codecs.open(file_name, "w", "utf-8") as f: 160 f.write(txt) 161 162 def read_from_file(file_name): 163 with codecs.open(file_name, "r", "utf-8") as f: 164 txt = f.read() 165 txt = txt.encode('utf-8') 166 return txt 167 168 169 if __name__ == '__main__': 170 if len(sys.argv) != 2: 171 print 'Usage: cnblog "update subtitle"' 172 sys.exit() 173 else: 174 subtitle = sys.argv[1].strip(''' "''') 175 176 username = 'your username' 177 password = 'your password' 178 179 if 0: 180 #保存cookie,但cookie失效需手动删除cookie文件 181 import os 182 cookie_txt = '/tmp/cnblog_cookie.txt' 183 if os.path.isfile(cookie_txt): 184 cookie = read_from_file(cookie_txt) 185 else: 186 cookie = cnblog_login(username, password) 187 write_to_file(cookie_txt, cookie) 188 189 else: 190 #不保存cookie 191 cookie = cnblog_login(username, password) 192 193 headers = { 194 "Cookie":cookie, 195 "User-Agent":user_agent 196 } 197 headers_form = { 198 "Cookie":cookie, 199 "Content-Type":"application/x-www-form-urlencoded", 200 "User-Agent":user_agent 201 } 202 headers_json = { 203 "Cookie":cookie, 204 "Content-Type":"application/json; charset=UTF-8", 205 "User-Agent":user_agent 206 } 207 208 config_url = 'http://www.cnblogs.com/' + username +'/admin/Configure.aspx' 209 config_args = cnblog_get_config(config_url, headers) 210 flag = cnblog_set_config(config_url, subtitle, config_args, headers_form) 211 if flag: 212 cnblog_post_shancun(headers_json, subtitle) 213 214 #cnblog_post_comment(headers_json, blog_id, subtitle) 215 216 if 0: 217 #看到有幸运闪,刚试了下连闪10下,‘我的’里面倒是显示全了,全站里只显4下,他们说,小心进小黑屋 218 for i in range(10): 219 shancun = '我' + '再' * i + '闪' 220 print shancun 221 cnblog_post_shancun(headers_json, shancun)
将上面代码保存为 cnblog,把其中的 username,password 改成自己的,加上可执行权限,即可像示例那样使用。
将该程序添加为自定义命令使在任何地方可运行,详见 linux 添加管理自定义命令。
原文:http://www.cnblogs.com/congbo/archive/2012/11/12/2766360.html