#!/usr/bin/env python
'''
# -*- coding: utf-8 -*-
'''

import time
import requests
from http import cookiejar
from bs4 import BeautifulSoup

class Spider:
    def __init__(self,url,captcha_url,login_url,cookie_filename,data=None):
        # 前面的这些参数可以用**keyargs来输入
        self.url = url
        self.headers = data.get('headers')
        self.login_name = data.get('login_name')
        self.passwd = data.get('passwd')
        self.proxies = data.get('proxies')
        self.captcha_url = captcha_url
        self.login_url = login_url
        # 获取session会话，用来保持跨请求的参数
        self.session = requests.session()
        self.cookie_filename = cookie_filename
        self.session.cookies = cookiejar.LWPCookieJar(filename=self.cookie_filename)
        self.session.keep_alive = False

    def get_login(self,login_name=None,passwd=None):
        if self.login_name is None:
            self.login_name = input('用户名：') if login_name is None else login_name
        if self.passwd is None:
            self.passwd = input('密码：') if passwd is None else passwd

    def get_xsrf(self):
        res = self.session.get(url=self.url,headers=self.headers)
        soup = BeautifulSoup(res.content,'html.parser')
        xsrf = soup.find(name='input',attrs={'name':'_xsrf'}).get('value')
        return xsrf

    def get_captcha(self):
        """
        验证码的问题，当在浏览器登陆过，即便是退出登录再登陆也不会要输入验证码，该问题是浏览器的cookies导致的
        清空浏览器的cookies，再次登陆就会出现需要输入验证码
        知乎浏览器验证码地址：https://www.zhihu.com/captcha.gif?r=1497427163063&type=login&lang=cn
        注意到后面有一个&lang=cn(个人认为是知乎网识别到登陆客户端是中文环境，给出的图片是中文汉字，识别复杂)
        手动将&lang=cn改为en，或者直接将&lang=cn去掉，验证码就变成字母了，然后就可以使用自动识别的三方库来完成
        手动填写验证码点击登陆后，审查元素才能跟踪到email文件，进行能查看到表单输入
        （中文图片需要点击提示的文字，captcha传入的是图片的大小，以及点击的位置的坐标）
        :return:
        """
        t = str(int(time.time()*1000))
        if self.captcha_url is None:
            print("captcha_url is None!!!")
            self.captcha_url = input('请输入captcha_url：')
        #通过验证码地址，获取验证码图片
        self.captcha_url = self.captcha_url % t
        print(self.captcha_url)
        res_jpg = self.session.get(url=self.captcha_url,headers=self.headers)
        #将验证码保存在文件中
        with open('captcha.jpg','wb') as f:
            f.write(res_jpg.content)
        captcha = input("请识别图片中的验证码：")
        return captcha

    def login(self):
        # 根据登录的方式不同，login_url不一样
        # 邮箱登录，如：'https://www.zhihu.com/login/email
        # 手机号登录，如：https://www.zhihu.com/login/phone_num
        # login_url = self.login_url;
        self.get_login()
        data = {
            'email':self.login_name,
            'password':self.passwd,
            '_xsrf':self.get_xsrf(),
            'captcha':self.get_captcha(),
            'remember_me': 'true'
        }
        print(self.login_name)
        print(self.passwd)
        print(data)
        # 返回json
        # requests.adapters.DEFAULT_RETRIES = 5
        # session.keep_alive = False
        res_json = self.session.post(url=self.login_url,data=data,headers=self.headers,proxies=self.proxies)
        print(res_json.json())
        # json结果样例
        # 成功：{'r': 0, 'msg': '登录成功'}
        # 失败：{'r': 1, 'errcode': 1991829, 'data': {'captcha': '请提交正确的验证码 :(', 'name': 'ERR_VERIFY_CAPTCHA'}, 'msg': '请提交正确的验证码 :('}
        if res_json.json().get('r') == 0:
            self.session.cookies.save()
            print(res_json.json().get('msg'))
        else:
            print("login err!")
            print(res_json.json().get('msg'))
            exit(1)


class Spider_zhihu(Spider):
    def __init__(self,data):
        self.url = data.get('url')
        self.captcha_url = data.get('captcha_url')
        self.login_url = data.get('login_url')
        self.cookie_filename = data.get('cookie_filename')
        self.headers = data.get('headers')
        self.login_name = data.get('login_name')
        self.passwd = data.get('passwd')
        super().__init__(url=self.url,captcha_url=self.captcha_url,login_url=self.login_url,cookie_filename=self.cookie_filename,data=data)

    def login_zhihu(self):
        session = requests.session()
        session.cookies = cookiejar.LWPCookieJar(filename=self.cookie_filename)
        try:
            session.cookies.load(ignore_discard=True)
        except:
            print("没有cookie信息")
            self.login()
            session.cookies.load(ignore_discard=True)
        finally:
            res = session.get(url=self.url,headers=self.headers,proxies=self.proxies)
            print(res.content.decode('utf-8'))




if __name__ == '__main__':
    headers = {
        "Host": "www.zhihu.com",
        "Referer": "https://www.zhihu.com/",
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87'
    }
    t = str(int(time.time() * 1000))
    proxies = {
        'http':'http://49.86.62.143:808',
        'https':'http://123.169.87.222:808'
    }
    data = {
        'url':'https://www.zhihu.com/'
        ,'captcha_url':'https://www.zhihu.com/captcha.gif?r=%s&type=login'
        ,'login_url':'https://www.zhihu.com/login/email'
        ,'cookie_filename':'cookies_zhihu.txt'
        ,'headers':headers
        # ,'proxies':proxies
        ,'login_name':'793679231@qq.com'
        ,'passwd':'ZlMr09301024'
    }

    s_zh = Spider_zhihu(data=data)
    s_zh.login_zhihu()
    # url,captcha_url,login_url,cookie_filename,data=None
    # s = Spider(url=data.get('url'),captcha_url=data.get('captcha_url'),login_url=data.get('login_url'),cookie_filename=data.get('cookie_filename'),data=data)
    # xs = s.get_xsrf()