from urllib.request import Request, urlopen
import json
import fn
import tdx
import sqllite_hy as sql
import sqllite_hy_list as hy_list
import sys
import gn

# 执行概念爬取
gn_Sbider = gn.Spider()
gn_Sbider.go()

# 实例化 通达信 行业类
tdx_hy = tdx.hy()


class Spider():
    url = 'http://51.push2.eastmoney.com/api/qt/clist/get?pn=1&pz=100&po=1&np=1&fltt=2&invt=2&fid=f8&fs=m:0+t:6,m:0+t:13,m:0+t:80,m:1+t:2,m:1+t:23'

    count_hy = []
    param = ''

    def __init__(self, param):  # 构造函数带参数
        self.param = param

    def __fetch_content(self):
        # 包装头部
        firefox_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}

        r = Request(Spider.url, headers=firefox_headers)
        html = urlopen(r)
        # 获取数据
        data = html.read()
        data_json = json.loads(data)

        if data_json['data'] != '':
            return data_json['data']['diff']
        else:
            return []

    # 行业统计结果输出
    def __op(self):
        # list = ['ST股', '公告', '公告', 'ST股', 'ST股', 'VR&AR', 'ST股', '5G', '其他', '公告', '公告', '次新股', 'ST股', '次新股', '知识产权','数字货币', '二胎', '智能制造', 'ST股', '油服']
        res = fn.get_array_count(self.count_hy)

        i = 1
        _info = ''
        for row in res:
            if isinstance(row, tuple):
                _info += '[' + str(i) + ']' + str(row[0].strip()) + ':' + str(row[1]) + '  '
                i += 1
        # 插入数据库
        open_time = fn.get_time()
        if fn.is_open(open_time) > 0 or self.param == '-f':
            sql.insert_row(open_time, _info)

        # 输出
        sql.show()

    # 结果输出
    def __show(self, lists):
        for i in range(0, len(lists)):
            price = fn.fill(lists[i]['f2'], 7)
            zf = str(lists[i]['f3']) + '%'
            stock_code = lists[i]['f12']
            stock_name = fn.fill(lists[i]['f14'], 8)
            stock_hand = '[换]' + fn.fill(str(lists[i]['f8']) + '%', 7)
            hy = fn.fill(tdx_hy.get_hy(stock_code), 8)

            # 行业入库 分析
            self.count_hy.append(hy)

            # 行业入库
            open_time = fn.get_time()
            if fn.is_open(open_time) > 0 or self.param == '-f':
                hy_list.save(hy, lists[i], fn.get_time(), int(i + 1))

            # 输出前面10条
            if i <= 20:
                i = fn.fill(str(i + 1), 3)
                print("%s %s %s %s %s %s %s" % (i, stock_code, stock_name, stock_hand, hy, price, zf))

        # 行业统计结果输出
        self.__op()

    def go(self):
        lists = self.__fetch_content()
        self.__show(lists)

        # 更新概念
        # gn.spider.go()


spider = Spider(sys.argv[1])
# spider = Spider('n')
spider.go()
