# 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

        if id is None:
            return

        try:
            url = self.geturl(id, encrypted=encrypted)
        except Exception as ex:
            control.log(traceback.format_exc(), control.LOGERROR)
            control.okDialog(u'TNT Play', str(ex))
            return

        if encrypted and not control.is_inputstream_available():
            control.okDialog(u'TNT Play', control.lang(34103).encode('utf-8'))
            self.stop_content(id, encrypted=encrypted)
            return

        control.log("media url: %s" % url)

        self.offset = float(meta['milliseconds_watched']) / 1000.0 if 'milliseconds_watched' in meta else 0

        self.isLive = not encrypted

        parsed_url = urlparse(url)

        # if ".m3u8" in parsed_url.path:
        #     self.url, mime_type, stop_event, cookies = hlshelper.pick_bandwidth(url)
        # else:
        #     self.url = url
        #     mime_type, stop_event, cookies = None, None, None


# 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


        if id is None:
            return

        try:
            url = self.geturl(id, encrypted=encrypted)
        except Exception as ex:
            control.log(traceback.format_exc(), control.LOGERROR)
            control.okDialog(u'TNT Play', str(ex))
            return

        if encrypted and not control.is_inputstream_available():
            control.okDialog(u'TNT Play', control.lang(34103).encode('utf-8'))
            self.stop_content(id, encrypted=encrypted)
            return

        control.log("media url: %s" % url)

        self.offset = float(meta['milliseconds_watched']) / 1000.0 if 'milliseconds_watched' in meta else 0

        self.isLive = not encrypted

        parsed_url = urlparse(url)

        # if ".m3u8" in parsed_url.path:
        #     self.url, mime_type, stop_event, cookies = hlshelper.pick_bandwidth(url)
        # else:
        #     self.url = url
        #     mime_type, stop_event, cookies = None, None, None


        proxy_handler = MediaProxy(control.proxy_url)