Last active
August 14, 2016 08:59
-
-
Save jiacai2050/7d1fdc09cddc284562cd14b19bcf75d0 to your computer and use it in GitHub Desktop.
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 -*- | |
import requests | |
import re | |
import os | |
import copy | |
host = 'v2ex.com' | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36', | |
'Host': host | |
} | |
def sign(user_value, pwd_value): | |
with requests.Session() as s: | |
signin_url = 'http://%s/signin' % host | |
signin_html = s.get(signin_url).content | |
m = re.search('<input type="text" class="sl" name="(.*?)"', signin_html) | |
if m: | |
user_name = m.group(1) | |
else: | |
raise Exception('username not found') | |
m = re.search('<input type="password" class="sl" name="(.*?)"', signin_html) | |
if m: | |
pwd_name = m.group(1) | |
else: | |
raise Exception('password not found') | |
m = re.search('<input type="hidden" value="(.*?)" name="once" />', signin_html) | |
if m: | |
once_value = m.group(1) | |
else: | |
raise Exception('once not found') | |
signin_param = { | |
"once": once_value, | |
"next": "/" | |
} | |
signin_param[user_name] = user_value | |
signin_param[pwd_name] = pwd_value | |
signin_headers = copy.copy(headers) | |
signin_headers['Referer'] = signin_url | |
signin_r = s.post(signin_url, data=signin_param, headers=signin_headers) | |
if 'V2EX_TAB' in signin_r.cookies: | |
print('signin ok') | |
else: | |
raise Exception('signin error') | |
mission_url = 'http://v2ex.com/mission/daily' | |
mission_r = s.get(mission_url, headers=headers) | |
m = re.search("location.href = '/mission/daily/redeem\?once=(\d+)'", mission_r.content) | |
if m: | |
once_value = m.group(1) | |
else: | |
raise Exception("can't find once value for qiandao") | |
qiandao_url = 'http://v2ex.com/mission/daily/redeem?once=%s' % once_value | |
qiandao_r = s.get(qiandao_url, headers=headers) | |
print('qiandao ok') | |
if __name__=='__main__': | |
user_name_value = os.getenv("V2EX_USER", "your_username") | |
pass_word_value = os.getenv("V2EX_PWD", "your_password") | |
sign(user_name_value, pass_word_value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment