#coding=utf-8
#!/usr/bin/python
import sys
sys.path.append('..') 
from base.spider import Spider
import json
import time
import base64

class Spider(Spider):  # 元类 默认的元类 type
    def getName(self):
        return "高中"
    def init(self,extend=""):
        print("============{0}============".format(extend))
        pass
    def isVideoFormat(self,url):
        pass
    def manualVideoCheck(self):
        pass
    def homeContent(self,filter):
        result = {}
        cateManual = {
    "高一语文":"高一语文",
    "高一数学":"高一数学",
    "高一英语":"高一英语",
    "高一历史":"高一历史",
    "高一地理":"高一地理",
    "高一生物":"高一生物",
    #"高一思想政治":"高一思想政治",
    "高一物理":"高一物理",
    "高一化学":"高一化学",
    "高二语文":"高二语文",
    "高二数学":"高二数学",
    "高二英语":"高二英语",
    "高二历史":"高二历史",
    "高二地理":"高二地理",
    "高二生物":"高二生物",
    #"高二思想政治":"高二思想政治",
    "高二物理":"高二物理",
    "高二化学":"高二化学",
    "高三语文":"高三语文",
    "高三数学":"高三数学",
    "高三英语":"高三英语",
    "高三历史":"高三历史",
    "高三地理":"高三地理",
    "高三生物":"高三生物",
    #"高三思想政治":"高三思想政治",
    "高三物理":"高三物理",
    "高三化学":"高三化学",
    #"高中信息技术":"高中信息技术",
    #"高中信息技术":"高中信息技术"
        }
        classes = []
        for k in cateManual:
            classes.append({
                'type_name':k,
                'type_id':cateManual[k]
            })
        result['class'] = classes
        if(filter):
            result['filters'] = self.config['filter']
        return result
    def homeVideoContent(self):
        result = {
            'list':[]
        }
        return result
    cookies = ''
    def getCookie(self):
        import requests
        import http.cookies
        # 这里填cookie
        raw_cookie_line = "innersign=0;buvid3=E0C6010B-08CF-AE9C-FD18-0EC14F2AA35429667infoc; b_lsid=D101097FB2_187C825C5D7; _uuid=29167C75-10922-A764-5757-EC106517B471831442infoc; buvid4=59789C83-B64B-148E-A8FD-585BB3D0300830827-123042821-WOiBDeHIjjEhSRIFCi5kfQ%3D%3D; buvid_fp=3ee98313cd55b76fe3cb63a36d9d41c9; b_nut=100; SESSDATA=4fe90c6b%2C1698242168%2C30912%2A41; bili_jct=faffed7de17218860f3aa958182ca2ee; DedeUserID=516036434; DedeUserID__ckMd5=4e5107a4bc891865; sid=q2dc0y62"
        simple_cookie = http.cookies.SimpleCookie(raw_cookie_line)
        cookie_jar = requests.cookies.RequestsCookieJar()
        cookie_jar.update(simple_cookie)
        return cookie_jar
    def get_dynamic(self,pg):
        result = {}
        
        url= 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/all?timezone_offset=-480&type=all&page={0}'.format(pg)
        
        rsp = self.fetch(url,cookies=self.getCookie())
        content = rsp.text
        jo = json.loads(content)
        if jo['code'] == 0:
            videos = []
            vodList = jo['data']['items']
            for vod in vodList:
                if vod['type'] == 'DYNAMIC_TYPE_AV':
                    ivod = vod['modules']['module_dynamic']['major']['archive']
                    aid = str(ivod['aid']).strip()
                    title = ivod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
                    img =  ivod['cover'].strip()
                    remark = str(ivod['duration_text']).strip()
                    videos.append({
                        "vod_id":aid,
                        "vod_name":title,
                        "vod_pic":img,
                        "vod_remarks":remark
                    })
                result['list'] = videos
                result['page'] = pg
                result['pagecount'] = 9999
                result['limit'] = 90
                result['total'] = 999999
        return result

    def get_hot(self,pg):
        result = {}
        url= 'https://api.bilibili.com/x/web-interface/popular?ps=20&pn={0}'.format(pg)
        rsp = self.fetch(url,cookies=self.getCookie())
        content = rsp.text
        jo = json.loads(content)
        if jo['code'] == 0:
            videos = []
            vodList = jo['data']['list']
            for vod in vodList:
                aid = str(vod['aid']).strip()
                title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
                img =  vod['pic'].strip()
                remark = str(vod['duration']).strip()
                videos.append({
                    "vod_id":aid,
                    "vod_name":title,
                    "vod_pic":img,
                    "vod_remarks":remark
                })
            result['list'] = videos
            result['page'] = pg
            result['pagecount'] = 9999
            result['limit'] = 90
            result['total'] = 999999
        return result
    def get_rank(self):
        result = {}
        url= 'https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all'
        rsp = self.fetch(url,cookies=self.getCookie())
        content = rsp.text
        jo = json.loads(content)
        if jo['code'] == 0:
            videos = []
            vodList = jo['data']['list']
            for vod in vodList:
                aid = str(vod['aid']).strip()
                title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
                img =  vod['pic'].strip()
                remark = str(vod['duration']).strip()
                videos.append({
                    "vod_id":aid,
                    "vod_name":title,
                    "vod_pic":img,
                    "vod_remarks":remark
                })
            result['list'] = videos
            result['page'] = 1
            result['pagecount'] = 1
            result['limit'] = 90
            result['total'] = 999999
        return result
    def categoryContent(self,tid,pg,filter,extend):	
        result = {}
        if tid == "热门":
            return self.get_hot(pg=pg)
        if tid == "排行榜" :
            return self.get_rank()
        if tid == '动态':
            return self.get_dynamic(pg=pg)
        url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&page={1}'.format(tid,pg)
        if len(self.cookies) <= 0:
            self.getCookie()
        rsp = self.fetch(url,cookies=self.getCookie())
        content = rsp.text
        jo = json.loads(content)
        if jo['code'] != 0:			
            rspRetry = self.fetch(url,cookies=self.getCookie())
            content = rspRetry.text		
        jo = json.loads(content)
        videos = []
        vodList = jo['data']['result']
        for vod in vodList:
            aid = str(vod['aid']).strip()
            title = tid + ":" + vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
            img = 'https:' + vod['pic'].strip()
            remark = str(vod['duration']).strip()
            videos.append({
                "vod_id":aid,
                "vod_name":title,
                "vod_pic":img,
                "vod_remarks":remark
            })
        result['list'] = videos
        result['page'] = pg
        result['pagecount'] = 9999
        result['limit'] = 90
        result['total'] = 999999
        return result
    def cleanSpace(self,str):
        return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
    def detailContent(self,array):
        aid = array[0]
        url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)

        rsp = self.fetch(url,headers=self.header,cookies=self.getCookie())
        jRoot = json.loads(rsp.text)
        jo = jRoot['data']
        title = jo['title'].replace("<em class=\"keyword\">","").replace("</em>","")
        pic = jo['pic']
        desc = jo['desc']
        typeName = jo['tname']
        vod = {
            "vod_id":aid,
            "vod_name":title,
            "vod_pic":pic,
            "type_name":typeName,
            "vod_year":"",
            "vod_area":"bilidanmu",
            "vod_remarks":"",
            "vod_actor":jo['owner']['name'],
            "vod_director":jo['owner']['name'],
            "vod_content":desc
        }
        ja = jo['pages']
        playUrl = ''
        for tmpJo in ja:
            cid = tmpJo['cid']
            part = tmpJo['part']
            playUrl = playUrl + '{0}${1}_{2}#'.format(part,aid,cid)

        vod['vod_play_from'] = 'B站'
        vod['vod_play_url'] = playUrl

        result = {
            'list':[
                vod
            ]
        }
        return result
    def searchContent(self,key,quick):
        search = self.categoryContent(tid=key,pg=1,filter=None,extend=None)
        result = {
            'list':search['list']
        }
        return result
    def playerContent(self,flag,id,vipFlags):
        # https://www.555dianying.cc/vodplay/static/js/playerconfig.js
        result = {}

        ids = id.split("_")
        url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid=%20%20{1}&qn=112'.format(ids[0],ids[1])
        rsp = self.fetch(url,cookies=self.getCookie())
        jRoot = json.loads(rsp.text)
        jo = jRoot['data']
        ja = jo['durl']
        
        maxSize = -1
        position = -1
        for i in range(len(ja)):
            tmpJo = ja[i]
            if maxSize < int(tmpJo['size']):
                maxSize = int(tmpJo['size'])
                position = i

        url = ''
        if len(ja) > 0:
            if position == -1:
                position = 0
            url = ja[position]['url']

        result["parse"] = 0
        result["playUrl"] = ''
        result["url"] = url
        result["header"] = {
            "Referer":"https://www.bilibili.com",
            "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
        }
        result["contentType"] = 'video/x-flv'
        return result

    config = {
        "player": {},
        "filter": {
        
"高一语文": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高一语文"},
{"n": "沪教版","v": "沪教版高一语文"},
{"n": "人教版","v": "人教版高一语文"},
{"n": "北师大版","v": "北师大版高一语文"},
{"n": "苏教版","v": "苏教版高一语文"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高一英语": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高一英语"},
{"n": "沪教版","v": "沪教版高一英语"},
{"n": "人教版","v": "人教版高一英语"},
{"n": "北师大版","v": "北师大版高一英语"},
{"n": "苏教版","v": "苏教版高一英语"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高一数学": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高一数学"},
{"n": "沪教版","v": "沪教版高一数学"},
{"n": "人教版","v": "人教版高一数学"},
{"n": "北师大版","v": "北师大版高一数学"},
{"n": "苏教版","v": "苏教版高一数学"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高一化学": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高一化学"},
{"n": "沪教版","v": "沪教版高一化学"},
{"n": "人教版","v": "人教版高一化学"},
{"n": "北师大版","v": "北师大版高一化学"},
{"n": "苏教版","v": "苏教版高一化学"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高一物理": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高一物理"},
{"n": "沪教版","v": "沪教版高一物理"},
{"n": "人教版","v": "人教版高一物理"},
{"n": "北师大版","v": "北师大版高一物理"},
{"n": "苏教版","v": "苏教版高一物理"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高一生物": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高一生物"},
{"n": "沪教版","v": "沪教版高一生物"},
{"n": "人教版","v": "人教版高一生物"},
{"n": "北师大版","v": "北师大版高一生物"},
{"n": "苏教版","v": "苏教版高一生物"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高一地理": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高一地理"},
{"n": "沪教版","v": "沪教版高一地理"},
{"n": "人教版","v": "人教版高一地理"},
{"n": "北师大版","v": "北师大版高一地理"},
{"n": "苏教版","v": "苏教版高一地理"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高一历史": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高一历史"},
{"n": "沪教版","v": "沪教版高一历史"},
{"n": "人教版","v": "人教版高一历史"},
{"n": "北师大版","v": "北师大版高一历史"},
{"n": "苏教版","v": "苏教版高一历史"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高二语文": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高二语文"},
{"n": "沪教版","v": "沪教版高二语文"},
{"n": "人教版","v": "人教版高二语文"},
{"n": "北师大版","v": "北师大版高二语文"},
{"n": "苏教版","v": "苏教版高二语文"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高二英语": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高二英语"},
{"n": "沪教版","v": "沪教版高二英语"},
{"n": "人教版","v": "人教版高二英语"},
{"n": "北师大版","v": "北师大版高二英语"},
{"n": "苏教版","v": "苏教版高二英语"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高二数学": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高二数学"},
{"n": "沪教版","v": "沪教版高二数学"},
{"n": "人教版","v": "人教版高二数学"},
{"n": "北师大版","v": "北师大版高二数学"},
{"n": "苏教版","v": "苏教版高二数学"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高二地理": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高二地理"},
{"n": "沪教版","v": "沪教版高二地理"},
{"n": "人教版","v": "人教版高二地理"},
{"n": "北师大版","v": "北师大版高二地理"},
{"n": "苏教版","v": "苏教版高二地理"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高二化学": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高二化学"},
{"n": "沪教版","v": "沪教版高二化学"},
{"n": "人教版","v": "人教版高二化学"},
{"n": "北师大版","v": "北师大版高二化学"},
{"n": "苏教版","v": "苏教版高二化学"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高二物理": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高二物理"},
{"n": "沪教版","v": "沪教版高二物理"},
{"n": "人教版","v": "人教版高二物理"},
{"n": "北师大版","v": "北师大版高二物理"},
{"n": "苏教版","v": "苏教版高二物理"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高二生物": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高二生物"},
{"n": "沪教版","v": "沪教版高二生物"},
{"n": "人教版","v": "人教版高二生物"},
{"n": "北师大版","v": "北师大版高二生物"},
{"n": "苏教版","v": "苏教版高二生物"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高二历史": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高二历史"},
{"n": "沪教版","v": "沪教版高二历史"},
{"n": "人教版","v": "人教版高二历史"},
{"n": "北师大版","v": "北师大版高二历史"},
{"n": "苏教版","v": "苏教版高二历史"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高三语文": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高三语文"},
{"n": "沪教版","v": "沪教版高三语文"},
{"n": "人教版","v": "人教版高三语文"},
{"n": "北师大版","v": "北师大版高三语文"},
{"n": "苏教版","v": "苏教版高三语文"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高三英语": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高三英语"},
{"n": "沪教版","v": "沪教版高三英语"},
{"n": "人教版","v": "人教版高三英语"},
{"n": "北师大版","v": "北师大版高三英语"},
{"n": "苏教版","v": "苏教版高三英语"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高三数学": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高三数学"},
{"n": "沪教版","v": "沪教版高三数学"},
{"n": "人教版","v": "人教版高三数学"},
{"n": "北师大版","v": "北师大版高三数学"},
{"n": "苏教版","v": "苏教版高三数学"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高三化学": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高三化学"},
{"n": "沪教版","v": "沪教版高三化学"},
{"n": "人教版","v": "人教版高三化学"},
{"n": "北师大版","v": "北师大版高三化学"},
{"n": "苏教版","v": "苏教版高三化学"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高三物理": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高三物理"},
{"n": "沪教版","v": "沪教版高三物理"},
{"n": "人教版","v": "人教版高三物理"},
{"n": "北师大版","v": "北师大版高三物理"},
{"n": "苏教版","v": "苏教版高三物理"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高三生物": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高三生物"},
{"n": "沪教版","v": "沪教版高三生物"},
{"n": "人教版","v": "人教版高三生物"},
{"n": "北师大版","v": "北师大版高三生物"},
{"n": "苏教版","v": "苏教版高三生物"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高三地理": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高三地理"},
{"n": "沪教版","v": "沪教版高三地理"},
{"n": "人教版","v": "人教版高三地理"},
{"n": "北师大版","v": "北师大版高三地理"},
{"n": "苏教版","v": "苏教版高三地理"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}],

"高三历史": [{"key": "tid","name": "分类","value": [{"n": "全部","v": "高三历史"},
{"n": "沪教版","v": "沪教版高三历史"},
{"n": "人教版","v": "人教版高三历史"},
{"n": "北师大版","v": "北师大版高三历史"},
{"n": "苏教版","v": "苏教版高三历史"}]},
{"key": "duration","name": "时长","value": [{"n": "全部","v": "0"},
{"n": "60分钟以上","v": "4"},
{"n": "30~60分钟","v": "3"},
{"n": "10~30分钟","v": "2"},
{"n": "10分钟以下","v": "1"}]}]
        
        }
    }
    header = {}

    def localProxy(self,param):
        return [200, "video/MP2T", action, ""]
