Last active
December 15, 2015 16:49
-
-
Save vincenting/5291960 to your computer and use it in GitHub Desktop.
xiami自动签到脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
__author__ = 'Vincent Ting' | |
import cookielib | |
import urllib2 | |
import urllib | |
import re | |
import random | |
import time | |
from apscheduler.scheduler import Scheduler | |
class Client(object): | |
def __init__(self, email=None, password=None): | |
""" | |
登录公共平台服务器,如果失败将报客户端登录异常错误 | |
:param email: | |
:param password: | |
:raise: | |
""" | |
if not email or not password: | |
raise ValueError | |
self.setOpener() | |
login_url = 'http://www.xiami.com/web/login' | |
index_url = 'http://www.xiami.com/web' | |
login_data = urllib.urlencode({'email': email, 'password': password, 'LoginButton': '登陆', }) | |
#确保每次签到时间错开一个小时。 | |
time.sleep(random.randint(1800, 7200)) | |
try: | |
self.opener.open(login_url, login_data, timeout=5).read() | |
self.login_response = self.opener.open(index_url, timeout=5).read() | |
except urllib2.URLError: | |
pass | |
def sign_in(self): | |
self.opener.addheaders = [('Referer', 'http://www.xiami.com/web')] | |
sign_in_pattern = re.compile(r'<a class="check_in" href="(.*?)">') | |
sign_in_result = sign_in_pattern.search(self.login_response) | |
sign_in_url = 'http://www.xiami.com' + sign_in_result.group(1) | |
print sign_in_url | |
try: | |
self.opener.open(sign_in_url, None, timeout=5).read() | |
except urllib2.URLError: | |
self.sign_in() | |
def setOpener(self): | |
""" | |
设置请求头部信息模拟浏览器 | |
""" | |
cookie = cookielib.CookieJar() | |
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) | |
self.opener.addheaders = [('Referer', 'http://www.xiami.com/web/login')] | |
self.opener.addheaders = [('User-Agent', 'Opera/9.60')] | |
if __name__ == "__main__": | |
sched = Scheduler() | |
sched.daemonic = False | |
sched.start() | |
@sched.interval_schedule(hours=24, start_date='2013-04-3 01:00:00') | |
def job_function(): | |
Client(email='[email protected]', password='xxxxx').sign_in() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment