# Path: resources/lib/hlsproxy/simpleproxy.py
# class MediaProxy:
# 
#     @property
#     def stop_event(self):
#         return self.stopPlaying
# 
#     def __init__(self, proxy=None):
#         log('MediaProxy Init | proxy: %s' % proxy)
#         self.host_name = HOST_NAME
#         self.port = PROXY_PORT
# 
#         self.stopPlaying = threading.Event()
#         self.stopPlaying.clear()
# 
#         t = threading.Thread(target=self.__start, args=(self.stopPlaying, proxy,))
#         t.daemon = True
#         t.start()
# 
#     def __start(self, stop_event, proxy):
#         log('MediaProxy Start')
#         socket.setdefaulttimeout(10)
# 
#         RequestHandler.protocol_version = "HTTP/1.1"
#         RequestHandler.proxy = proxy
# 
#         httpd = None
#         try:
#             ThreadedHTTPServer.daemon_threads = True
#             httpd = ThreadedHTTPServer((self.host_name, self.port, ), RequestHandler, stop_event)
# 
#             log("Simple Proxy Started - %s:%s" % (self.host_name, self.port))
# 
#             while not stop_event.isSet():
#                 httpd.handle_request()
#         finally:
#             try:
#                 if httpd:
#                     httpd.server_close()
#             finally:
#                 log("Simple Proxy Stopped %s:%s" % (self.host_name, self.port))
# 
#     def resolve(self, url):
# 
#         log('MediaProxy resolve: %s' % url)
# 
#         path = urlparse.urlparse(url).path
#         extension = os.path.splitext(path)[1] or 'm3u8'
#         extension = extension.replace('.', '')
# 
#         link = PROXY_URL_FORMAT % (extension, urllib.quote_plus(url))
# 
#         return link  # make a url that caller then call load into player


import threading
import sys
import urllib
import resources.lib.modules.control as control
import requests
import xbmc
import traceback
from urlparse import urlparse
from resources.lib.hlsproxy.simpleproxy import MediaProxy
from auth import get_token, get_device_id, logout

        device_id = get_device_id()

        platform = 'PCTV' if not encrypted else vod_platform  # 'PCTV_DASH'

        url = 'https://apac.ti-platform.com/AGL/1.0/R/%s/%s/TNTGO_LATAM_BR/CONTENT/CDN/?id=%s&type=VOD&asJson=Y&accountDeviceId=%s' % (LANGUAGE, platform, content_id, device_id)

        retry = True if not cookie else False

        cookie = cookie or get_token()

        headers = {
            'Accept': 'application/json',
            'cookie': 'avs_cookie=' + cookie,
            'User-Agent': 'Tnt/2.2.13.1908061505 CFNetwork/1107.1 Darwin/19.0.0'
        }

        control.log('TNT GET ' + url)
        control.log(headers)

        result = requests.get(url, headers=headers, proxies=proxy).json() or {}

        control.log(result)

        if 'message' in result and result['message'] == u'Token not valid' and retry:
            cookie = get_token(True)
            return self.geturl(content_id, cookie, encrypted)

        if result.get('resultCode', u'KO') == u'KO':
            logout()

# Path: resources/lib/hlsproxy/simpleproxy.py
# class MediaProxy:
# 
#     @property
#     def stop_event(self):
#         return self.stopPlaying
# 
#     def __init__(self, proxy=None):
#         log('MediaProxy Init | proxy: %s' % proxy)
#         self.host_name = HOST_NAME
#         self.port = PROXY_PORT
# 
#         self.stopPlaying = threading.Event()
#         self.stopPlaying.clear()
# 
#         t = threading.Thread(target=self.__start, args=(self.stopPlaying, proxy,))
#         t.daemon = True
#         t.start()
# 
#     def __start(self, stop_event, proxy):
#         log('MediaProxy Start')
#         socket.setdefaulttimeout(10)
# 
#         RequestHandler.protocol_version = "HTTP/1.1"
#         RequestHandler.proxy = proxy
# 
#         httpd = None
#         try:
#             ThreadedHTTPServer.daemon_threads = True
#             httpd = ThreadedHTTPServer((self.host_name, self.port, ), RequestHandler, stop_event)
# 
#             log("Simple Proxy Started - %s:%s" % (self.host_name, self.port))
# 
#             while not stop_event.isSet():
#                 httpd.handle_request()
#         finally:
#             try:
#                 if httpd:
#                     httpd.server_close()
#             finally:
#                 log("Simple Proxy Stopped %s:%s" % (self.host_name, self.port))
# 
#     def resolve(self, url):
# 
#         log('MediaProxy resolve: %s' % url)
# 
#         path = urlparse.urlparse(url).path
#         extension = os.path.splitext(path)[1] or 'm3u8'
#         extension = extension.replace('.', '')
# 
#         link = PROXY_URL_FORMAT % (extension, urllib.quote_plus(url))
# 
#         return link  # make a url that caller then call load into player


# Path: resources/lib/modules/tntplay/player.py
import threading
import sys
import urllib
import resources.lib.modules.control as control
import requests
import xbmc
import traceback
from urlparse import urlparse
from resources.lib.hlsproxy.simpleproxy import MediaProxy
from auth import get_token, get_device_id, logout


        device_id = get_device_id()

        platform = 'PCTV' if not encrypted else vod_platform  # 'PCTV_DASH'

        url = 'https://apac.ti-platform.com/AGL/1.0/R/%s/%s/TNTGO_LATAM_BR/CONTENT/CDN/?id=%s&type=VOD&asJson=Y&accountDeviceId=%s' % (LANGUAGE, platform, content_id, device_id)

        retry = True if not cookie else False

        cookie = cookie or get_token()

        headers = {
            'Accept': 'application/json',
            'cookie': 'avs_cookie=' + cookie,
            'User-Agent': 'Tnt/2.2.13.1908061505 CFNetwork/1107.1 Darwin/19.0.0'
        }

        control.log('TNT GET ' + url)
        control.log(headers)

        result = requests.get(url, headers=headers, proxies=proxy).json() or {}

        control.log(result)

        if 'message' in result and result['message'] == u'Token not valid' and retry:
            cookie = get_token(True)
            return self.geturl(content_id, cookie, encrypted)

        if result.get('resultCode', u'KO') == u'KO':
            logout()

            raise Exception('%s: %s' % (result.get('message', u'STREAM URL ERROR'), result.get('errorDescription', u'UNKNOWN')))