# -*- coding: UTF-8 -*-

#2024-11-01
# edit 2024-12-14

try:
    from resources.lib.utils import help, isBlockedHoster
except:
    from xbmc import executebuiltin
    executebuiltin('Quit')
    exit()

from scrapers.modules.tools import cParser  # re - alternative
from resources.lib.requestHandler import cRequestHandler
from scrapers.modules import cleantitle, source_utils
from resources.lib.control import getSetting, urljoin,quote,quote_plus
from resources.lib.utils import isBlockedHoster

SITE_IDENTIFIER = 'serien-stream'
SITE_DOMAIN = 'www.serien-stream.cc'
SITE_NAME = SITE_IDENTIFIER.upper()

class source:
    def __init__(self):
        self.priority = 1
        self.language = ['de']
        self.domain = getSetting('provider.' + SITE_IDENTIFIER + '.domain', SITE_DOMAIN)
        self.base_link = 'https://' + self.domain

        self.search_link = self.base_link + '/recherche?_token=jtVkMD6nTD1VurjrwSKR9L2vAjyLjg1xWDKHLLLz&q=%s'
        self.sources = []

    def run(self, titles, year, season=0, episode=0, imdb='', hostDict=None):
        if season==0 or episode==0: return self.sources
        isResolve = True
        t = [cleantitle.get(i) for i in set(titles) if i]
        links = []

        try:
            for sSearchText in titles:
                oRequest = cRequestHandler(self.search_link % sSearchText)
                oRequest.cacheTime = 60 * 60 * 6
                sHtmlContent = oRequest.request()
                pattern = 'with-mask">.*?alt="([^"]+).*?href="([^"]+)'
                isMatch, aResult = cParser.parse(sHtmlContent, pattern)
                for sName, sUrl in aResult:
                    sName=sName.strip()
                    if not cleantitle.get(sName) in t: continue
                    links.append(sUrl)

                if len(links) > 0: break

            if len(links) == 0: return self.sources

            for link in links:
                try:
                    oRequest = cRequestHandler(link)
                    oRequest.cacheTime = 60 * 60 * 6
                    sHtmlContent = oRequest.request()
                    pattern = 'short-img\simg-box">.+?title="([^"]+").*?href="([^"]+)'
                    isMatch, aResult = cParser.parse(sHtmlContent, pattern)
                    if not isMatch: continue
                    link = [i[1] for i in aResult if 'staffel %s"' % str(season) in i[0]][0]

                    oRequest = cRequestHandler(link)
                    oRequest.cacheTime = 60 * 60 * 6
                    sHtmlContent = oRequest.request()
                    pattern = '<a\stitle="[^"]+.*?href="([^"]+)'
                    isMatch, aResult = cParser().parse(sHtmlContent, pattern)
                    if not isMatch: return self.sources
                    sUrl = [i for i in aResult if 'folge-%s-' % str(episode) in i][0]
                    sHtmlContent = cRequestHandler(sUrl).request()
                    pattern = 'data-url="([^"]+)'
                    isMatch, aResult = cParser().parse(sHtmlContent, pattern)
                    if not isMatch: return self.sources
                    sQuality = '720p'
                    for sUrl in aResult:
                        if 'userload' in sUrl: continue
                        if sUrl.startswith('/'): sUrl = self.base_link + sUrl
                        isBlocked, hoster, url, prioHoster = isBlockedHoster(sUrl)
                        if isBlocked: continue
                        if url: self.sources.append({'source': hoster, 'quality': sQuality, 'language': 'de', 'url': url, 'direct': True, 'prioHoster': prioHoster})
                except:
                     continue

            return self.sources
        except:
            return self.sources

    def resolve(self, url):
        try:
            return url
        except:
            return
