cnblog 闪存刷星星,每一条闪存都是星星

使用示例:

➜  ~  shancun 天亮说晚安

1: post shancun successfully.

2: post shancun successfully.

3: post shancun successfully.

4: post shancun successfully.

Has become lucky star!~ 

ps:闪存若包含空格,加双引号。

 

查看闪存:

 

源代码如下:

linux / python 2.7 / 2012:11:15 03:44 update

为求简单,出错即退出。玩具程序,娱乐即可。

  1 #!/usr/bin/env python

  2 #encoding=utf-8

  3 

  4 import sys

  5 import urllib

  6 import httplib2

  7 #httplib2.debuglevel = 1

  8 import re

  9 from lxml import etree

 10 import codecs

 11 import inspect

 12 

 13 h = httplib2.Http()

 14 user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"

 15 

 16 #{{{-----------------------------------------------------

 17 #模拟登录,得到带有登录信息的cookie

 18 def cnblog_login(username, password):

 19     login_url = 'http://passport.cnblogs.com/login.aspx'

 20     headers_base = {

 21         "User-Agent":user_agent

 22     }

 23     resp, html = h.request(login_url, headers=headers_base)

 24     #print html

 25 

 26     pat_args = re.compile(r'<input type="hidden" name="(.*?)".*?value="(.*?)" />', re.S)

 27     args = pat_args.findall(html)

 28     if not args:

 29         print 'can not find the arguments of login, check the regex!'

 30     else:

 31         args = dict(args)

 32 

 33     args.update({

 34         "tbUserName":username,

 35         "tbPassword":password,

 36         "btnLogin":"登  录",

 37         "txtReturnUrl":"http://www.cnblogs.com/"

 38     })

 39     #print args

 40 

 41     headers_form = {

 42         "User-Agent":user_agent,

 43         "Content-Type":"application/x-www-form-urlencoded"

 44     }

 45     resp, html = h.request(login_url, method='POST', body=urllib.urlencode(args), headers=headers_form)

 46     #print resp

 47     cookie = resp.get('set-cookie')

 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_post_shancun(headers, shancun):

 58     url = 'http://home.cnblogs.com/ajax/ing/Publish'

 59     #body = '{"html":"%s","publicFlag":1}' % shancun

 60     body = '{"content":"%s","publicFlag":1}' % shancun

 61     resp, html = h.request(url, method='POST', body=body, headers=headers)

 62     if resp['status'] != '200':

 63         print 'fail in %s!' % inspect.getframeinfo(inspect.currentframe())[2]

 64         print resp

 65         exit()

 66 

 67 def get_shancun_html(headers):

 68     url = 'http://home.cnblogs.com/ing/'

 69     resp, html = h.request(url, headers=headers)

 70     if resp['status'] != '200':

 71         print 'fail in %s!' % inspect.getframeinfo(inspect.currentframe())[2]

 72         print resp

 73         exit()

 74     #print 'get shancun html successfully.'

 75     return html

 76 

 77 def get_shancun_content(html):

 78     expr = '//*[@id="feed_list"]'

 79     html = html.decode("utf-8", 'ignore')

 80     content = etree.HTML(html)

 81     content = content.xpath(expr)

 82     if not content:

 83         print 'get shancun content fail, check xpath!'

 84         exit()

 85     content = etree.tostring(content[0], encoding="utf-8")

 86     #print content[:600]

 87     #print 'get shancun content successfully.'

 88     return content

 89 

 90 def isnot_lucky_shancun(username, shancun, content):

 91     pat = re.compile(

 92         r'''

 93         href="/u/%s/"\ class="big_font\ blue".*?<span.*?id="ing_body_(.*?)">

 94         (.*?)</span>

 95         (.*?)class="ing_time"

 96         ''' % username, re.S | re.X

 97     )

 98     #<img src="http://static.cnblogs.com/images/ing_lucky.png" class="ing_icon_lucky" alt="" title="这是幸运闪">

 99     my_shancun = pat.findall(content)

100     if not my_shancun:

101         print 'can not find shancun, check the regex!'

102         exit()

103     else:

104         my_shancun = my_shancun[0]

105     shancun_id, my_lastest_shancun, about_star = my_shancun

106     #print shancun_id, my_lastest_shancun, about_star

107 

108     #XXX 发送同一闪存,或者闪存的post方法发生变化,会导致闪存不显示

109     if shancun != my_lastest_shancun:

110         # 闪存中若有http:// 或 https:// 会自动添加格式

111         if shancun.find('http://') == -1 and shancun.find('https://') == -1:

112             #print '[',shancun,']'

113             #print '[',my_lastest_shancun,']'

114             print '%s is not display correctly!' % shancun

115             exit()

116     if about_star.find('ing_icon_lucky') != -1:

117         return None

118     else:

119         return shancun_id

120 

121 def delete_shancun(headers, shancun_id):

122     url = 'http://home.cnblogs.com/ajax/ing/del'

123     body = '{ingId:%s}' % shancun_id

124     resp, html = h.request(url, method='POST', body=body, headers=headers)

125     if resp['status'] != '200':

126         print 'fail in %s!' % inspect.getframeinfo(inspect.currentframe())[2]

127         print resp

128         exit()

129     #print 'delete shancun successfully.'

130 

131 

132 def write_to_file(file_name, txt):

133     with codecs.open(file_name, "w", "utf-8") as f:

134         f.write(txt)

135 

136 def read_from_file(file_name):

137     with codecs.open(file_name, "r", "utf-8") as f:

138         txt = f.read()

139         txt = txt.encode('utf-8')

140     return txt

141 

142 

143 if __name__ == '__main__':

144     if len(sys.argv) != 2:

145         print 'Usage: shancun "要发的闪存"'

146         sys.exit()

147     else:

148         shancun = sys.argv[1].strip(''' "''')

149 

150     username = 'you username'

151     password = 'your password'

152 

153     if 1:

154         #保存cookie,但cookie失效需手动删除cookie文件

155         import os

156         cookie_txt = '/tmp/cnblog_cookie.txt'

157         if os.path.isfile(cookie_txt):

158             cookie = read_from_file(cookie_txt)

159         else:

160             cookie = cnblog_login(username, password)

161             write_to_file(cookie_txt, cookie)

162 

163     else:

164         #不保存cookie

165         cookie = cnblog_login(username, password)

166 

167     headers = {

168         "Cookie":cookie,

169         "User-Agent":user_agent

170     }

171     headers_json = {

172         "Cookie":cookie,

173         "Content-Type":"application/json; charset=UTF-8",

174         "User-Agent":user_agent

175     }

176 

177 

178     #一页里一个人最多连续5条,不能发布相同内容

179     count = 0

180     while True:

181         cnblog_post_shancun(headers_json, shancun)

182         count += 1

183 

184         html = get_shancun_html(headers)

185         content = get_shancun_content(html)

186         shancun_id = isnot_lucky_shancun(username, shancun, content)

187         print '%s: post shancun successfully.' % count

188         if not shancun_id:

189             print 'Has become lucky star!'

190             break

191         else:

192             delete_shancun(headers_json, shancun_id)

193             if count >= 33:

194                 print '人品差了点,歇一会再试吧!'

195                 break

 

将上面代码保存为 shancun,把其中的 username,password 改成自己的,加上可执行权限,即可像示例那样使用。

将该程序添加为自定义命令使在任何地方可运行,详见 linux 添加管理自定义命令

 

原文:http://www.cnblogs.com/congbo/archive/2012/11/15/2770880.html

你可能感兴趣的:(Blog)