#coding=utf-8
#!/usr/bin/python
import sys
sys.path.append('..')
from base.spider import Spider
import base64
import math
import json
# [py578 adaptation] requests hard-dependency fallback (real requests preferred)
try:
    import requests
except Exception:
    requests = None
if requests is None:
    # --- embedded requests-compatible shim (urllib based) for Yingxia adaptation ---
    # Inserted by py578 adaptation: used only when real `requests` is unavailable.
    import json as _json
    import ssl as _ssl
    import urllib.error as _uerr
    import urllib.parse as _uparse
    import urllib.request as _ureq
    import http.cookiejar as _cookiejar


    class _ReqExc(Exception):
        pass


    class _Timeout(_ReqExc):
        pass


    class _ConnectionError(_ReqExc):
        pass


    class _HTTPError(_ReqExc):
        pass


    class _CaseDict(dict):
        def get(self, k, d=None):
            for kk, vv in self.items():
                if kk.lower() == str(k).lower():
                    return vv
            return d

        def __getitem__(self, k):
            v = self.get(k)
            if v is None:
                raise KeyError(k)
            return v


    class _Response:
        def __init__(self, status_code=0, headers=None, content=b"", url="", cookies=None):
            self.status_code = status_code
            self.headers = _CaseDict(headers or {})
            self.content = content or b""
            self.url = url
            self.cookies = cookies or {}
            self.encoding = None
            self._text = None

        @property
        def text(self):
            if self._text is None:
                enc = self.encoding
                if not enc:
                    ct = self.headers.get("Content-Type", "")
                    m = _uparse.parse_qs(ct.replace(";", "&")) if False else None
                    enc = "utf-8"
                    if "charset=" in ct:
                        enc = ct.split("charset=")[-1].split(";")[0].strip() or "utf-8"
                try:
                    self._text = self.content.decode(enc, "ignore")
                except Exception:
                    self._text = self.content.decode("utf-8", "ignore")
            return self._text

        def json(self):
            return _json.loads(self.text)

        def raise_for_status(self):
            if self.status_code >= 400:
                raise _HTTPError("HTTP %s" % self.status_code)

        def iter_content(self, chunk_size=65536):
            for i in range(0, len(self.content), chunk_size):
                yield self.content[i:i + chunk_size]

        def close(self):
            pass


    class _Session:
        def __init__(self):
            self.headers = {"User-Agent": "Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36 Chrome/120 Mobile Safari/537.36"}
            self.cookies = {}
            self._jar = _cookiejar.CookieJar()
            self._ctx = _ssl._create_unverified_context()
            self._opener = _ureq.build_opener(
                _ureq.HTTPCookieProcessor(self._jar),
                _ureq.HTTPSHandler(context=self._ctx))

        def request(self, method, url, headers=None, timeout=15, data=None,
                    json=None, params=None, cookies=None, verify=False,
                    allow_redirects=True, stream=False, **kw):
            if params:
                qs = _uparse.urlencode(params, doseq=True)
                url = url + ("&" if "?" in url else "?") + qs
            hd = dict(self.headers)
            if headers:
                hd.update(headers)
            body = None
            if json is not None:
                body = _json.dumps(json, ensure_ascii=False).encode("utf-8")
                hd.setdefault("Content-Type", "application/json")
            elif isinstance(data, dict):
                body = _uparse.urlencode(data, doseq=True).encode("utf-8")
                hd.setdefault("Content-Type", "application/x-www-form-urlencoded")
            elif isinstance(data, str):
                body = data.encode("utf-8")
            elif isinstance(data, (bytes, bytearray)):
                body = bytes(data)
            if cookies:
                hd["Cookie"] = "; ".join("%s=%s" % kv for kv in cookies.items())
            elif self.cookies:
                hd.setdefault("Cookie", "; ".join("%s=%s" % kv for kv in self.cookies.items()))
            req = _ureq.Request(url, data=body, headers=hd, method=method.upper())
            try:
                to = timeout
                if isinstance(to, (tuple, list)):
                    to = max(to)
                if to and float(to) > 120:
                    to = float(to) / 1000.0
                with self._opener.open(req, timeout=to or 15) as resp:
                    content = resp.read()
                    hdrs = dict(resp.headers.items())
                    final = resp.geturl()
                    status = resp.status
            except _uerr.HTTPError as e:
                try:
                    content = e.read()
                except Exception:
                    content = b""
                hdrs = dict(e.headers.items()) if e.headers else {}
                final = e.geturl() if hasattr(e, "geturl") else url
                status = e.code
            except Exception as e:
                raise _ConnectionError(str(e))
            for c in self._jar:
                self.cookies[c.name] = c.value
            return _Response(status, hdrs, content, final, dict(self.cookies))

        def get(self, url, **kw):
            return self.request("GET", url, **kw)

        def post(self, url, data=None, **kw):
            return self.request("POST", url, data=data, **kw)

        def head(self, url, **kw):
            return self.request("HEAD", url, **kw)

        def close(self):
            pass


    class _Exceptions:
        RequestException = _ReqExc
        Timeout = _Timeout
        ConnectTimeout = _Timeout
        ReadTimeout = _Timeout
        ConnectionError = _ConnectionError
        HTTPError = _HTTPError
        SSLError = _ConnectionError


    class _RequestsShim:
        Session = _Session
        exceptions = _Exceptions
        Response = _Response

        @staticmethod
        def request(method, url, **kw):
            return _Session().request(method, url, **kw)

        @staticmethod
        def get(url, **kw):
            return _Session().request("GET", url, **kw)

        @staticmethod
        def post(url, data=None, **kw):
            return _Session().request("POST", url, data=data, **kw)

        @staticmethod
        def head(url, **kw):
            return _Session().request("HEAD", url, **kw)


    requests = _RequestsShim
    # --- end embedded requests shim ---

    # --- py578 compat extras for deep requests usage (fallback only) ---
    class _HTTPAdapter:
        def __init__(self, *a, **k):
            pass


    class _Retry:
        def __init__(self, *a, **k):
            pass


    def _session_mount(self, *a, **k):
        return None


    _Session.mount = _session_mount
    _Session.put = lambda self, url, data=None, **kw: self.request("PUT", url, data=data, **kw)
    _Session.delete = lambda self, url, **kw: self.request("DELETE", url, **kw)
    _Session.patch = lambda self, url, data=None, **kw: self.request("PATCH", url, data=data, **kw)
    _Session.options = lambda self, url, **kw: self.request("OPTIONS", url, **kw)
    _Response.apparent_encoding = "utf-8"
    _Response.ok = property(lambda self: self.status_code < 400)
    _Response.history = property(lambda self: [])


    class _ReqUtils:
        quote = staticmethod(_uparse.quote)
        quote_plus = staticmethod(_uparse.quote_plus)
        unquote = staticmethod(_uparse.unquote)
        unquote_plus = staticmethod(_uparse.unquote_plus)


    class _ReqAdapters:
        HTTPAdapter = _HTTPAdapter


    class _ReqUrllib3:
        @staticmethod
        def disable_warnings(*a, **k):
            pass

        class exceptions:
            class InsecureRequestWarning(Warning):
                pass


    class _ReqPackages:
        urllib3 = _ReqUrllib3


    _RequestsShim.utils = _ReqUtils
    _RequestsShim.adapters = _ReqAdapters
    _RequestsShim.packages = _ReqPackages
    _RequestsShim.put = staticmethod(lambda url, data=None, **kw: _Session().request("PUT", url, data=data, **kw))
    _RequestsShim.delete = staticmethod(lambda url, **kw: _Session().request("DELETE", url, **kw))
    _RequestsShim.patch = staticmethod(lambda url, data=None, **kw: _Session().request("PATCH", url, data=data, **kw))
    # --- end py578 compat extras ---
# [py578] import requests  (provided by adaptation above)
import urllib
from urllib import request, parse
import urllib.request
import re

class Spider(Spider):
	def getName(self):
		return "西瓜视频(个人中心)"
	def init(self,extend=""):
		self.userid=self.get_userid()
	def isVideoFormat(self,url):
		pass
	def manualVideoCheck(self):
		pass
	def homeContent(self,filter):
		result = {}
		cateManual = {
			"电视剧":"dianshiju",
			"电影":"dianying",
			"动漫":"dongman",
			"纪录片":"jilupian",
			"少儿":"shaoer",
			"综艺":"zongyi",
			"关注":"follow"

		}
		if self.userid=='':
			del 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
	userid=''
	def categoryContent(self,tid,pg,filter,extend):
		result = {}
		idTxt='电视剧'
		maximum=17
		url = 'https://www.ixigua.com/api/cinema/filterv2/albums'
		if tid=='dianying':
			idTxt='电影'
		elif tid=='zongyi':
			idTxt='综艺'
		elif tid=='dianshiju':
			idTxt='电视剧'
		elif tid=='dongman':
			idTxt='动漫'
		elif tid=='jilupian':
			idTxt='纪录片'
		elif tid=='shaoer':
			idTxt='少儿'	
		elif tid=='follow':
			offset=0 if int(pg)<2 else 20*int(pg)
			url='https://www.ixigua.com/api/userv2/follow/list?authorId={0}&sortType=desc&sortType=desc&cursor={1}'.format(self.userid,offset)
			maximum=12
		videos=[]
		if tid!='follow':
			offset=0 if int(pg)<2 else 18*int(pg)
			self.header['Referer']='https://www.ixigua.com/cinema/filter/'.format(tid)
			data=r'{"pinyin":"'+tid+'","filters":{"type":"'+idTxt+'","area":"全部地区","tag":"全部类型","sort":"综合排序","paid":"全部资费"},"offset":'+str(offset)+',"limit":18}'
			req = request.Request(url=url, data=bytes(data, encoding='utf8'),headers=self.header, method='POST')
			response = request.urlopen(req)
			urlTxt=response.read().decode('utf-8')
			videos= self.get_list_videoGroup_json(jsonTxt=urlTxt)
		else:
			rsp=self.fetch(url,headers=self.header)
			urlTxt=rsp.text
			videos= self.get_list_videoGroup_follow_json(jsonTxt=urlTxt)
		numvL = len(videos)
		result['list'] = videos
		result['page'] = pg
		result['pagecount'] = pg if int(numvL)<maximum else int(pg)+1
		result['limit'] = numvL
		result['total'] = numvL
		return result
	def get_userid(self):
		Url='https://www.ixigua.com/'
		rsp=self.fetch(Url,headers=self.header)
		htmlTxt = rsp.text
		userid= self.get_RegexGetText(Text=htmlTxt,RegexText=r'"identity":{"id":"(\d+?)",',Index=1)
		return userid
	def detailContent(self,array):
		result = {}
		aid = array[0].split('###')
		key = aid[1]
		title = aid[0]
		act=aid[2]
		logo = aid[3]
		Url='https://www.ixigua.com/api/albumv2/details?albumId={0}'.format(key)
		if len(aid)==5:
			Url='https://www.ixigua.com/api/videov2/author/new_video_list?to_user_id={0}'.format(key)
		rsp = self.fetch(Url,headers=self.header)
		htmlTxt = rsp.text
		typeName=''
		area=''
		dir=''
		cont=''
		vip='true'
		videoList=[]
		if len(aid)==5:
			jRoot = json.loads(htmlTxt)
			if jRoot['code']!=200:
				return result
			jo = jRoot['data']
			jsonList=jo['videoList']
			for value in jsonList:
					id="{0}${1}_false".format(value['title'],value.get('group_id'))
					videoList.append(id)
			dir=title
		elif htmlTxt.find('playlist')>2:
			jRoot = json.loads(htmlTxt)
			if jRoot['code']!=200:
				return result
			jo = jRoot['data']
			jsonList=jo['playlist']
			if jsonList is not None:
				for value in jsonList:
					id="{0}${1}?id={2}_{3}".format(value['title'],value['albumId'],value['episodeId'],vip)
					videoList.append(id)
			playFrom=[v for v in jo['albumInfo']['tagList']]
			typeName='/'.join(playFrom)
			playFrom=[v for v in jo['albumInfo']['areaList']]
			area='/'.join(playFrom)
			playFrom=[v['name'] for v in jo['albumInfo']['directorList']]
			dir='/'.join(playFrom)
			cont=jo['albumInfo']['intro']
		if len(videoList)<1:
			return result
		vod = {
			"vod_id":array[0],
			"vod_name":title,
			"vod_pic":logo,
			"type_name":typeName,
			"vod_year":'',
			"vod_area":area,
			"vod_remarks":"",
			"vod_actor":'',
			"vod_director":dir,
			"vod_content":cont
		}
		vod['vod_play_from'] = '西瓜'
		vod['vod_play_url'] = "#".join(videoList)
		result = {
			'list':[
				vod
			]
		}
		return result

	def verifyCode(self):
		pass

	def searchContent(self,key,quick):
		Url='https://www.ixigua.com/api/searchv2/lvideo/{0}/0'.format(urllib.parse.quote(key))
		rsp = self.fetch(Url,headers=self.header)
		htmlTxt = rsp.text
		videos=self.get_list(html=htmlTxt)
		'''
		Url='https://www.ixigua.com/api/searchv2/user/{0}/10'.format(urllib.parse.quote(key))
		rsp = self.fetch(Url,headers=self.header)
		htmlTxt1 = rsp.text
		videos=self.get_list_user(html=htmlTxt1)
		'''
		result = {
				'list': videos
			}
		return result

	def playerContent(self,flag,id,vipFlags):
		result={}
		UrlId=id.split('_')
		Url='https://www.ixigua.com/{0}'.format(UrlId[0])
		headers = {
			'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Mobile Safari/537.36'
		}
		jx=1 if UrlId[1]=='true' else 0
		result["parse"] = 1#0=直接播放,1=解析
		result["playUrl"] = ''
		result["url"] = Url
		result['jx'] = jx#VIP解析
		result["header"] =headers
		return result
	def get_RegexGetText(self,Text,RegexText,Index):
		returnTxt=""
		Regex=re.search(RegexText, Text, re.M|re.I)
		if Regex is None:
			returnTxt=""
		else:
			returnTxt=Regex.group(Index)
		return returnTxt	
	def get_RegexGetTextLine(self,Text,RegexText,Index):
		returnTxt=[]
		pattern = re.compile(RegexText)
		ListRe=pattern.findall(Text)
		if len(ListRe)<1:
			return returnTxt
		for value in ListRe:
			returnTxt.append(value)	
		return returnTxt
	def get_playlist(self,Text,headStr,endStr):
		circuit=""
		origin=Text.find(headStr)
		if origin>8:
			end=Text.find(endStr,origin)
			circuit=Text[origin:end]
		return circuit
	def removeHtml(self,txt):
		soup = re.compile(r'<[^>]+>',re.S)
		txt =soup.sub('', txt)
		return txt.replace("&nbsp;"," ")
	def get_webReadFile(self,urlStr):
		headers = {
			'Referer':urlStr,
			'User-Agent': 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36',
			'Host': 'www.ikmjw.com'
		}
		req = urllib.request.Request(url=urlStr, headers=headers)
		html = urllib.request.urlopen(req).read().decode('utf-8')
		return html
	def get_list(self,html):
		result={}
		jRoot = json.loads(html)
		if jRoot['code']!=0:
			return result
		jo = jRoot['data']
		if len(jo)<1:
			return result
		vodList = jo['data']
		if len(vodList)<1:
			return result
		videos=[]
		img='_'
		artist='_'
		for vod in vodList:
			if vod['type']!='lvideo':
				continue
			data=vod['data']
			display=data['display']
			title =display['name']
			if len(title)==0:
				continue
			episode_link=display.get('episode_link')
			asc_link=episode_link.get('asc_link')
			KeyName='album_id'
			if asc_link is None:
				asc_link=episode_link.get('desc_link')
				KeyName='album_id'
			id=asc_link[0].get('album_id')
			sslocal=asc_link[0]['scheme_url']
			if sslocal!=None and sslocal.find('sslocal:')<0:
				continue
			video_cover_info=display.get('video_cover_info')
			img=video_cover_info['url'] if video_cover_info is not None else '_'
			try:
				artist=display['actor']
			except Exception:
				artist=''
			remarks=display['rating']
			vod_id="{0}###{1}###{2}###{3}".format(title,id,artist,img)
			videos.append({
				"vod_id":vod_id,
				"vod_name":title,
				"vod_pic":img,
				"vod_remarks":remarks
			})
		return videos
	def get_list_user(self,html):
		result={}
		jRoot = json.loads(html)
		if jRoot['code']!=0:
			return result
		jo = jRoot['data']
		if len(jo)<1:
			return result
		vodList = jo['data']
		if len(vodList)<1:
			return result
		videos=[]
		img='_'
		artist='_'
		for vod in vodList:
			if vod['type']!='user':
				continue
			data=vod['data']
			img=data['avatar']
			title =data['name']
			log_pb=json.loads(data.get('log_pb'))
			user_auth_info=json.loads(data.get('user_auth_info'))
			remarks=user_auth_info['auth_info']
			id=log_pb.get('search_result_id')
			vod_id="{0}###{1}###{2}###{3}###{4}".format(title,id,artist,img,'user')
			videos.append({
				"vod_id":vod_id,
				"vod_name":title,
				"vod_pic":img,
				"vod_remarks":remarks
			})
		return videos
	def get_list_videoGroup_json(self,jsonTxt):
		result={}
		jRoot = json.loads(jsonTxt)
		if jRoot['code']!=200:
			return result
		jo = jRoot['data']
		vodList = jo['albumList']
		if len(vodList)<1:
			return result
		videos=[]
		img='_'
		artist='_'
		for vod in vodList:
			url =vod['albumId']
			title =vod['title']
			imgList =vod.get('coverList') 
			if len(imgList)>0:
				img=imgList[0]['url']
			remarks=vod['subTitle']
			artistList=vod.get('actorList') 
			if artistList is not None:
				artistList=artistList if len(artistList)<5 else artistList[0:4]
				artist='/'.join(artistList)
			if len(title)==0:
				continue
			#标题###地址###演员###封面
			vod_id="{0}###{1}###{2}###{3}".format(title,url,artist,img)
			videos.append({
				"vod_id":vod_id,
				"vod_name":title,
				"vod_pic":img,
				"vod_remarks":remarks
			})
		return videos
	def get_list_videoGroup_follow_json(self,jsonTxt):
		videos=[]
		jRoot = json.loads(jsonTxt)
		if jRoot['code']!=0:
			return videos
		jo = jRoot['data']
		vodList=jo['data']
		if len(vodList)<1:
			return videos
		img='_'
		artist=''
		for vod in vodList:
			url =vod.get('user_id') 
			title =vod['name']
			img =vod.get('avatar_url') 
			remarks=vod['description']
			artistList=vod.get('actorList') 
			artist=title
			if len(title)==0:
				continue
			#标题###地址###演员###封面
			vod_id="{0}###{1}###{2}###{3}###{4}".format(title,url,artist,img,'user')
			videos.append({
				"vod_id":vod_id,
				"vod_name":title,
				"vod_pic":img,
				"vod_remarks":remarks
			})
		return videos
	def get_lineList(self,Txt,mark,after):
		circuit=[]
		origin=Txt.find(mark)
		while origin>8:
			end=Txt.find(after,origin)
			circuit.append(Txt[origin:end])
			origin=Txt.find(mark,end)
		return circuit
	def get_EpisodesList(self,jsonList):
		vodItems=[]
		for value in jsonList:
			vodItems.append(value['title']+"$"+'https://www.ixigua.com/{0}?logTag=55abe18cfb733871bb04'.format(value['episodeId']))
		return vodItems
	config = {
		"player": {},
		"filter": {}
	}
	header = {
		"Cookie":"s_v_web_id=verify_lev3h43l_rrTPrFDG_ztWQ_4ugg_8WBA_yGVYsXlVyoBh; passport_csrf_token=80e0efe90bc8bd6681a896dd90cd08cc; passport_csrf_token_default=80e0efe90bc8bd6681a896dd90cd08cc; __ac_nonce=0643361890096533c765; __ac_signature=_02B4Z6wo00f01JPVVrAAAIDBcisHPfWA66CT91IAAEDK9840CE-PheNOCgA4VtrFG0-K.KkLmAR5KsI-Xx-6dBXxz.ABWU2OpEd22kF7biwGaVmGR7an4S1heLEU9xpv0ObRSHFHDslR7uL8fb; support_webp=true; support_avif=false; MONITOR_WEB_ID=45c3b6ab-7ad4-4805-b971-5962d1d6909a; ttwid=1%7CCueNR-HU9tGVF30WaiFCjXDxh0FUXoXsZr-cIb9Dogg%7C1681089268%7Cf0eeaa2016a602a277055494954f083e9f7fa8121c5dd1162db9195932fa167b; odin_tt=386a42a5740f9859d4670373fc8c70cf320ea5b227aed04bcc53fde26c233e8c952a8b0ba5f80fd3d46f9663fa595d8c; sid_guard=842b56710f55021912487890e7d5bef3%7C1681089337%7C3024001%7CMon%2C+15-May-2023+01%3A15%3A38+GMT; uid_tt=b7eb5b73cd72bed6d3c2e1e1e8aaa9f3; uid_tt_ss=b7eb5b73cd72bed6d3c2e1e1e8aaa9f3; sid_tt=842b56710f55021912487890e7d5bef3; sessionid=842b56710f55021912487890e7d5bef3; sessionid_ss=842b56710f55021912487890e7d5bef3; sid_ucp_v1=1.0.0-KGQ2YzBlMDFiMzIyMjY0YTIwMDg2MjZmZGQzMTE5MmFlYTYzY2EwMTMKFQjL2cnx9AIQucbNoQYYGCAMOAhABRoCaGwiIDg0MmI1NjcxMGY1NTAyMTkxMjQ4Nzg5MGU3ZDViZWYz; ssid_ucp_v1=1.0.0-KGQ2YzBlMDFiMzIyMjY0YTIwMDg2MjZmZGQzMTE5MmFlYTYzY2EwMTMKFQjL2cnx9AIQucbNoQYYGCAMOAhABRoCaGwiIDg0MmI1NjcxMGY1NTAyMTkxMjQ4Nzg5MGU3ZDViZWYz; csrf_session_id=c58adecac1d20d91d8c61e72ce0c6fdb; ixigua-a-s=3; msToken=St3ptsHkwBjPgGvRWhZfVdhHO_K16vTizxxna17draCvt4ekz6DQXO6c2Ctrp6gOnn9_Abm83-a9URQp5rKb-JIPs4mQPn2fpKlQRY6jLDBWUdLYTePIAACU6cY2fk40; tt_scid=nN3WPItw72gnB5PDiGcHveirXDZ8oDl5n.ihKN583mmZhoe.uLhFpy3JOL8wrsQO0ed1",
		"Referer": 'https://www.ixigua.com/cinema/filter/dianshiju/',
		'User-Agent':'User-Agent: Mozilla%2F5.0+(Windows+NT+10.0%3B+WOW64)+AppleWebKit%2F537.36+(KHTML%2C+like+Gecko)+Chrome%2F63.0.3239.132+Safari%2F537.36',
		'Host': 'www.ixigua.com',
		'Accept': 'application/json, text/plain, */*',
		'x-secsdk-csrf-token': '0001000000017b593ba6251b18bd7bce2753042917bb36e534867b9606317584c00b0ae836c61754314b7365128e',
		'tt-anti-token': 'oDr7A3PDDFq4pWzk-707faf92a9e3040f5c6ed4284d53b05b7091221852c0e9d32bca9fcfe5035225',
		'content-type': 'application/json'	
	}

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