使用示例:

➜  ~  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

转载于:https://www.cnblogs.com/congbo/archive/2012/11/15/2770880.html

cnblog 闪存刷星星,每一条闪存都是星星相关推荐

  1. 闪存我自己来——HDS公布闪存路线图

    近日,HDS全面升级了其云基础架构解决方案,包括HUS VM全闪存阵列.HUS与HNAS平台以及增强版UCP(融合基础架构).HDS中国区解决方案与专业服务事业部总监陈戈表示,HDS从虚拟化.统一.融 ...

  2. mysql怎么刷题_面试刷题mysql1:一条sql语句是如何经过mysql的体系结构的?

    {port} -u${user} -p ,输入密码. 使用连接器连接服务端: 连接成功之后,权限修改不会影响当前连接,连接的有效期默认是8个小时: 连接之后,执行过程中使用内存会持续增加,应该定时重置 ...

  3. rust读条闪退_rust更新后无法连接服务器 | 手游网游页游攻略大全

    发布时间:2015-12-25 手游我叫MT2在更新之后呢,有许多玩家反映一直出现卡读条进不去游戏的情况!这可急坏了不少玩家呢!那么我叫mt2更新后卡读条进不去服务器怎么办?下面且看小编给大家带来的我 ...

  4. rust读条闪退_rust读条慢卡loding | 手游网游页游攻略大全

    发布时间:2016-07-16 游戏过程中,小编表示刚看完开场CG后,选择完难度就无限读取中,究竟卡读条怎么办呢?今天小编为大家带来巫师3卡读条怎么办 卡读条解决方法!希望对大家也有帮助. 卡读条解决 ...

  5. java一打开就闪退怎么解决(如何解决java 闪退)

    若手机打开软件出现抄闪退.强制关闭等情况,建议:1.可能是该软件缓存较多导致无法正常运行,建议清除软件袭缓存尝试:设置-查找应用程序管理器"-(全部)-查找该软件-(存储)-清除数据(注:该 ...

  6. oracle9i能闪回吗,[转]Oracle 9i的闪回查询概述

    key words: Oracle闪回 flash 1.Oracle 9i的闪回查询功能 在Oracle 9i之前,如果用户错误操作数据后,除了不完全恢复外,没有好的解决办法.Oracle 9i中提供 ...

  7. 内存和显存_详谈服务器内存和显存知识

    原创 Hardy 早期内存通过存储器总线和北桥相连,北桥通过前端总线与CPU通信.从Intel Nehalem起,北桥被集成到CPU内部,内存直接通过存储器总线和CPU相连. 所以,在AMD采用Soc ...

  8. 小程序商店刷榜_机刷8毛,人刷2块2,好评app都是刷出来的?苹果:刷榜app将从应用商店移除...

    (原标题:机刷8毛,人刷2块2,好评app都是刷出来的?苹果:刷榜app将从应用商店移除) 随着智能手机的普及,手机上的应用软件与我们的生活越来越密不可分.当你在手机上下载应用软件的时候,你会去看这个 ...

  9. 笔记本光驱在计算机里不显示器,电脑开机硬盘灯一直亮不闪的 光驱没反应显示器不显示 风扇都转的...

    电脑开机硬盘灯一直亮不闪的 光驱没反应显示器不显示 风扇都转的以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 电脑开机硬 ...

  10. oracle闪回能保存多长时间,Oracle闪回

    Oracle闪回 作用:自动基于磁盘的备份与恢复,能把表恢复到过去的某个时间点或者SCN. 1.查看闪回区 SQL>show parameter db_recovery_file_dest; 查 ...

最新文章

  1. js 获取鼠标在画布的位置_javascript求鼠标在canvas画布里的坐标
  2. lucene LZ4 会将doc存储在一个chunk里进行Lz4压缩 ES的_source便如此
  3. Excel 数据分析技巧
  4. Google Mock启蒙篇 [1] (Google C++ Mocking Framework for Dummies 翻译)
  5. Invalid bound statement (not found):出现的原因和解决方法
  6. android前台进程视频教程,Android Twilio视频通话,唤醒应用程序并进入前台
  7. is_file()和file_exists()
  8. 涨薪慢,该不该跳槽?
  9. Android 自动轮播图+滑动效果
  10. tt桌球瞄准器手机版_传闻老任将进军手机市场?任天堂独立直面会将于周四凌晨举行...
  11. python排版word文档 效率,【效率工具】用Python根据excel中数据批量生成word文档(适用劳...
  12. If 表达式和条件语句
  13. X1000 Kernel 3.10 Linux V8.2编译
  14. 电源纹波怎么测量,纹波和噪声的区别
  15. Ubuntu20.04+Window10双系统开机引导界面美化
  16. AUTOSAR架构软件结构简介
  17. 8086/88系统中CLK引脚需要的8284时钟发生器
  18. 智慧警保综合管理平台
  19. 初识Java调用百度API实现图像识别
  20. 和我一起学 Three.js【初级篇】:3. 掌握摄影机

热门文章

  1. 异步fifo_面试必杀技:异步FIFO(上) CDC的那些事(5)
  2. python基础之迭代器、生成器、装饰器
  3. VS2008环境下编译使用SGI STL(using stlport 5.2.1)
  4. 从零开始学Vue(一)—— Vue.js 入门
  5. Android开发入门的正确姿势,你get到了吗?
  6. oracle 自增加列的实现
  7. iPhone为何优越过 Android呢
  8. Syndication
  9. Android中WebView和JavaScript进行简单通信
  10. Periodic Call 1.0