Last active
November 13, 2018 10:37
-
-
Save i-sync/02f08e0df3363a64c6ee0d82445f4a6f to your computer and use it in GitHub Desktop.
V2EX Login
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 python3 | |
#-*- coding=utf-8 -*- | |
import logging | |
import requests | |
import requests.utils | |
import sys | |
import re | |
import os.path | |
import json | |
import time | |
import telegram | |
path = '/root/v2ex' | |
image_path = '/data/file/picture' | |
host = 'http://yourdomain.com:8080/picture' | |
cookie_file = '{}/cookies_{}.json' #.format(path) | |
signin='https://www.v2ex.com/signin' | |
home='https://www.v2ex.com' | |
url='https://www.v2ex.com/mission/daily' | |
captcha = 'https://www.v2ex.com/_captcha?once={}' | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36', | |
'Origin': 'https://www.v2ex.com', | |
'Referer': 'https://www.v2ex.com/signin', | |
'Host': 'www.v2ex.com', | |
} | |
data={} | |
#telegram | |
token = '317386xxx:xxxxxxx' | |
group = {'id': -100xsdf87, 'name': 'asynchronize'} | |
def set_cookie(username, cookies): | |
''' | |
store cookie info | |
''' | |
cookie_file_path = cookie_file.format(path, username) | |
if os.path.exists(cookie_file_path): | |
os.remove(cookie_file_path) | |
try: | |
with open(cookie_file_path, 'w') as f: | |
#pickle.dump(requests.utils.dict_from_cookiejar(cookies), f) | |
json.dump(requests.utils.dict_from_cookiejar(cookies), f) | |
except: | |
pass | |
def get_cookie(username): | |
''' | |
get cookie info from file | |
''' | |
cookie_file_path = cookie_file.format(path, username) | |
if os.path.exists(cookie_file_path): | |
try: | |
with open(cookie_file_path, 'r') as f: | |
#cookies = requests.utils.cookiejar_from_dict(pickle.load(f)) | |
cookies = requests.utils.cookiejar_from_dict(json.load(f)) | |
return cookies | |
except: | |
pass | |
return None | |
def get_captcha(session, once): | |
''' | |
get captcha pic | |
''' | |
res = session.get(captcha.format(once)); | |
#captcha_name = time.time() | |
file_name = '{}.png'.format(int(time.time())) | |
file_path = '{}/{}'.format(image_path, file_name) | |
with open(file_path, 'wb') as f: | |
f.write(res.content) | |
return '{}/{}'.format(host, file_name) | |
def sign(username,passwd): | |
cookies = get_cookie(username) | |
session= requests.Session() | |
if not cookies: | |
session.headers=headers | |
res = session.get(signin,verify=False) | |
html = res.text | |
once = re.search(r'value="(\d{5})"', html).group(1) | |
inputs = re.findall(r'type="text" class="sl" name="(.*?)"', html) | |
passwordform = re.search(r'type="password" class="sl" name="(.*?)"', html).group(1) | |
#print(usernameform) | |
#print(passwordform) | |
#print(inputs) | |
data[inputs[0]]=username | |
data[passwordform]=passwd | |
data['once']=once | |
data['next']='/' | |
file_url = get_captcha(session, once) | |
print(file_url) | |
#captcha_code = input('captcha file: {}, please input captcha: '.format(file_name)) | |
captcha_code = input('please input captcha: ') | |
data[inputs[1]] = captcha_code | |
#print(data) | |
loginp=session.post(signin,data=data,verify=False) | |
''' | |
failed = re.search(r'class="problem"', loginp.text) | |
if failed: | |
logging.info('login failed...') | |
#rm cookie file | |
if os.path.exists(cookie_file): | |
os.remove(cookie_file) | |
#send telegram message | |
bot = telegram.Bot(token) | |
bot.send_message(chat_id=group['id'], text= 'login failed, need you manual handler.') | |
return | |
''' | |
else: | |
#session = requests.Session(cookies = cookies) | |
#print(cookies) | |
session.cookies = cookies | |
session.headers = headers | |
sign=session.get(url).content.decode('UTF-8') | |
login_failed = re.search(r'form method="post" action="/signin"', sign) | |
if login_failed: | |
logging.info('login failed...') | |
cookie_file_path = cookie_file.format(path, username) | |
#rm cookie file | |
if os.path.exists(cookie_file_path): | |
os.remove(cookie_file_path) | |
#send telegram message | |
bot = telegram.Bot(token) | |
bot.send_message(chat_id=group["id"], text= 'v2ex ({}) login failed, need you manual handler.'.format(username)) | |
return | |
qiandao=re.findall("location.href = '(.*?)'",sign)[0] | |
logging.info(qiandao) | |
if (qiandao == '/balance'): | |
logging.info("username:{} signed...".format(username)) | |
else: | |
session.get(home+qiandao,verify=False) | |
logging.info('username:{} sign success...'.format(username)) | |
#store cookie | |
set_cookie(username, session.cookies) | |
if __name__=='__main__': | |
logging.basicConfig(format='%(asctime)-15s %(message)s', level=logging.INFO, filename='{}/v2ex.log'.format(path)) | |
accounts = {'xxxxx': 'xxxxx'} | |
requests.packages.urllib3.disable_warnings() | |
try: | |
for username, passwd in accounts.items(): | |
sign(username,passwd) | |
except Exception as e: | |
logging.error(e) | |
logging.error(sys.exc_info()[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment