今天看到同事参与小米的抢购,几经数个星期的尝试,终于抢到了一台小米电视……看了一下小米的抢购流程,似乎可以用程序可破。于是想写点东西玩玩(你懂的……),第一步肯定是先得模拟登录小米帐号,当练手吧。
用 Python 来实现吧,由于是写一个Web应用,那么框架就选 Tornado。
首先是定义应用的 URL:
代码如下:
def main():
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/", MainHandler),
(r"/mibuy/", MiBuyHandler),
],**settings)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
代码如下:
post_data = urllib.urlencode({'passToken':'', 'user': 'www.nowamagic.net', 'pwd': 'password', 'callback':'https://account.xiaomi.com', 'sid':'passport', 'qs':'%3Fsid%3Dpassport', 'hidden':'', '_sign':'KKkRvCpZoDC+gLdeyOsdMhwV0Xg='})
path = 'https://account.xiaomi.com/pass/serviceLoginAuth2'
代码如下:
class MiBuyHandler(tornado.web.RequestHandler):
def get(self):
cj = cookielib.CookieJar()
post_data = urllib.urlencode({'passToken':'', 'user': 'www.nowamagic.net', 'pwd': 'password', 'callback':'https://account.xiaomi.com', 'sid':'passport', 'qs':'%3Fsid%3Dpassport', 'hidden':'', '_sign':'KKkRvCpZoDC+gLdeyOsdMhwV0Xg='})
path = 'https://account.xiaomi.com/pass/serviceLoginAuth2'
cookieHandle = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookieHandle)
#opener.addheaders = [('User-agent', 'Opera/9.23')]
urllib2.install_opener(opener)
req = urllib2.Request(path, post_data)
response = urllib2.urlopen(req)
html = response.read()
self.render("mibuy.html",message=html)
代码如下:
hdcontrol(
{
stime: 1383645496,
status: {
allow: true,
miphone: {
hdurl: "",
duration: null,
hdstop: true,
reg: true,
pmstart: false,
hdstart: false
},
mibox: {
hdurl: "",
duration: null,
hdstop: true,
reg: true,
pmstart: false,
hdstart: false
},
mitv: {
hdurl: "",
duration: null,
hdstop: true,
reg: false,
pmstart: false,
hdstart: false
}
}
})