Created
August 8, 2014 03:19
-
-
Save jackyyf/8eede6bb968bb978410a to your computer and use it in GitHub Desktop.
Gist by paste.py @ 2014-08-08 11:19:22.752072
This file contains 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 | |
# -*- encoding: utf-8 -*- | |
import requests | |
import requests.exceptions | |
import sys | |
import datetime | |
import time | |
import random | |
# Step 0: Initialize environment | |
# Fuck ASCII | |
reload(sys) | |
sys.setdefaultencoding('UTF-8') | |
del sys.setdefaultencoding | |
req = requests.Session() | |
req.headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2107.3 Safari/537.36' | |
req.headers['Accept-Language'] = 'Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2' | |
req.headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' | |
req.headers['Accept-Encoding'] = '' | |
req.headers['Cache-Control'] = 'max-age=0' | |
# Step 1: Go to UIS, fetch some cookie. | |
url1 = 'https://uis2.fudan.edu.cn/amserver/UI/Login?goto=http://tac.fudan.edu.cn/thirds/yiban.act' | |
req.get(url1) | |
# Step 2: Post to UIS, login & redirect to yiban. | |
stuid = 'STUID' | |
passwd = 'PASSWORD' | |
frm = { | |
'IDToken0' : '', | |
'IDToken1' : stuid, | |
'IDToken2' : passwd, | |
'IDButton' : 'Submit', | |
'goto' : 'aHR0cDovL3RhYy5mdWRhbi5lZHUuY24vdGhpcmRzL3lpYmFuLmFjdA==', # http://tac.fudan.edu.cn/thirds/yiban.act | |
'encoded' : 'true', | |
'inputCode' : '', | |
'gx_charset' : 'UTF-8', | |
} | |
url2 = 'https://uis2.fudan.edu.cn/amserver/UI/Login' | |
resp = req.post(url2, data=frm, headers={ | |
'Referer' : url1, | |
}, allow_redirects=True) | |
if not resp.history: | |
print 'Login Failed ...' | |
sys.exit(1) | |
assert resp.url.startswith('http://fudan.yiban.cn/') | |
print 'UIS Login OK. Yiban Login OK.' | |
# Step 3: Fetch the post. | |
url3 = 'http://fudan.yiban.cn/eclass/show?eclass_id=61528&area=61528&aid=10022482' | |
req.get(url3) | |
# Step 4: (Loop!) Some silly ajax. | |
expect_time = datetime.datetime(2014, 8, 8, 2, 0, 0, 0) # GMT Time, CST is GMT+0800 | |
url4 = 'http://fudan.yiban.cn/ajax/member_sys/frontend/check_tiezi_reply.php' | |
seg_min, seg_max = 30, 60 | |
while True: | |
try: | |
resp = req.post(url4, data={}, headers={ | |
'Referer' : url3, | |
'X-Requested-With' : 'XMLHttpRequest', | |
}, timeout=3) | |
except requests.exceptions.RequestException as e: | |
print '[Request Exception] %s' % str(e) | |
continue | |
print '[DEBUG] AJAX Response: ' + resp.text | |
cur = datetime.datetime.strptime(resp.headers['Date'], '%a, %d %b %Y %H:%M:%S %Z') | |
print 'Server Time: %s' % str(cur) | |
left = (expect_time - cur).seconds | |
print 'Time left: %d seconds' % left | |
if left < seg_max: | |
print 'Will open soon! Final sleep ...' | |
time.sleep(max(left - 0.5, 0)) | |
break | |
sleep_length = seg_min + (seg_max - seg_min) * random.random() | |
print 'Sleep %.2f seconds ... ' % sleep_length | |
time.sleep(sleep_length) | |
if random.randint(1, 5) == 1: | |
print 'Randomly refresh page ...' | |
try: | |
req.get(url3, timeout=3) | |
except requests.exceptions.RequestException as e: | |
print 'Refresh error ... Ignored.' | |
# Step 5: POST! | |
print 'GO! GO! GO!' | |
# content = 'Timing test. Plz just ignore this :) \n' + server_time | |
content = '余一夫、杨皓然、丁戍、章凌豪 + 711房间' | |
frm2 = { | |
'P_text' : content, | |
'button' : '回复中', | |
} | |
url5 = 'http://fudan.yiban.cn/reply_store.php?area=61528&aid=10022482' | |
while True: | |
try: | |
resp = req.post(url5, data=frm2, headers={ | |
'Referer' : url3, | |
}, timeout=3) | |
break | |
except requests.exceptions.RequestException as e: | |
continue | |
cur = datetime.datetime.strptime(resp.headers['Date'], '%a, %d %b %Y %H:%M:%S %Z') | |
print 'POST Done. Response status: ' + str(resp.status_code) | |
print 'Response time: ' + str(cur) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment