#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/12/1 10:55
# @Author  : zhujinghui
# @File    : xueqiu.py
# @Software: PyCharm
import requests
import random

agents = [
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.0 Safari/532.5",
    "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/532.9 (KHTML, like Gecko) Chrome/5.0.310.0 Safari/532.9",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.514.0 Safari/534.7",
    "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/534.14 (KHTML, like Gecko) Chrome/9.0.601.0 Safari/534.14",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.14 (KHTML, like Gecko) Chrome/10.0.601.0 Safari/534.14",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.672.2 Safari/534.20",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.27 (KHTML, like Gecko) Chrome/12.0.712.0 Safari/534.27",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.24 Safari/535.1",
    "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.36 Safari/535.7",
    "Mozilla/5.0 (Windows; U; Windows NT 6.0 x64; en-US; rv:1.9pre) Gecko/2008072421 Minefield/3.0.2pre",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10",
    "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)",
    "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 GTB5",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; tr; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 ( .NET CLR 3.5.30729; .NET4.0E)",
    "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
    "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
    "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0a2) Gecko/20110622 Firefox/6.0a2",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0b4pre) Gecko/20100815 Minefield/4.0b4pre",
    "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0 )",
    "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90)",
    "Mozilla/5.0 (Windows; U; Windows XP) Gecko MultiZilla/1.6.1.0a",
    "Mozilla/2.02E (Win95; U)",
    "Mozilla/3.01Gold (Win95; I)",
    "Mozilla/4.8 [en] (Windows NT 5.1; U)",
    "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.4) Gecko Netscape/7.1 (ax)",
]
URLS = {
    'BABA':'https://xueqiu.com/S/BABA',
    'AAPL':'https://xueqiu.com/S/AAPL',
    'AMD':'https://xueqiu.com/S/AMD'
}

def getUrlCookie(URL):
    session = requests.Session()
    agent = random.choice(agents)
    session.headers.update({'User-Agent': agent})
    response = session.get(URL,verify=False)
    jsonStr = response.content.decode('utf-8')
    cookie = session.cookies.get_dict()
    return cookie

class XueQiuCookie(object):
    def __init__(self):
        self.cookies = {}
        for code,url in URLS.items():
            cookie = getUrlCookie(url)
            self.cookies[code] = cookie

    def getCookie(self):
        keys = self.cookies.keys()
        key = random.choice(list(keys))
        return (key, self.cookies[key])

    def upDateCookie(self,code):
        url = URLS[code]
        cookie = getUrlCookie(url)
        self.cookies[code] = cookie

class Downloader():
    def __init__(self):
        self.cookiemanager = XueQiuCookie()
    def download(self,codes,num=3):
        url = "https://xueqiu.com/v4/stock/quote.json?code={}".format(','.join(codes))
        agent = random.choice(agents)

        code_cookie = self.cookiemanager.getCookie()
        code = code_cookie[0]
        cookie = code_cookie[1]

        session = requests.session()
        session.headers.update({"User-Agent": agent})
        session.cookies.update(cookie)

        rsp = session.get(url,verify=False)
        data_json = rsp.json()
        if 'error_code' in data_json.keys():
            print("xxxxxxxxxxxxxxxxxx")
            n = num - 1
            if n > 0:
                self.cookiemanager.upDateCookie(code)
                return self.download(url,num=n)
            else:
                return
        else:
            return data_json


# url = "https://xueqiu.com/v4/stock/quote.json?code=AAPL"
# xueqiudownloader = Downloader()
# data_json = xueqiudownloader.download(['AMD'])
#
# print(data_json)