# Path: resources/lib/modules/hlshelper.py
# def get_max_bandwidth():
# def pick_bandwidth(url):


import requests
import sys
import threading
import resources.lib.modules.control as control
import xbmc
from urlparse import urlparse
from auth import gettoken
from auth import get_default_profile
from private_data import get_device_id
from resources.lib.modules import hlshelper

    def onPlayBackStarted(self):
        # Will be called when xbmc starts playing a file
        control.log("Playback has started!")
        # if self.offset > 0: self.seekTime(float(self.offset))

    def onPlayBackEnded(self):
        # Will be called when xbmc stops playing a file
        control.log("setting event in onPlayBackEnded ")

    def onPlayBackStopped(self):
        # Will be called when user stops xbmc playing a file
        control.log("setting event in onPlayBackStopped")

        if self.stopPlayingEvent:
            self.stopPlayingEvent.set()

    def individualize(self, is_live, content_id, provider, format='mpd'):

        token, account = gettoken()
        device_id = get_device_id()
        profile = get_default_profile(account, device_id, token)

        useragent = 'ios' if format == 'm3u8' else 'web'

        if is_live:
            url = 'https://apim.oi.net.br/app/oiplay/oapi/v1/media/accounts/%s/profiles/%s/live/%s/individualize?deviceId=%s&tablet=false&useragent=%s' % (account, profile, content_id, device_id, 'ios' if format == 'm3u8' else 'web')
        else:
            url = 'https://apim.oi.net.br/app/oiplay/oapi/v1/media/accounts/{account}/profiles/{profile}/content/{content}/{provider}/individualize?deviceId={deviceId}&tablet=false&useragent={useragent}'.format(account=account, profile=profile, content=content_id, provider=provider.encode('utf-8'), deviceId=device_id, useragent=useragent)


# Path: resources/lib/modules/hlshelper.py
# def get_max_bandwidth():
# def pick_bandwidth(url):


# Path: resources/lib/modules/oiplay/player.py
import requests
import sys
import threading
import resources.lib.modules.control as control
import xbmc
from urlparse import urlparse
from auth import gettoken
from auth import get_default_profile
from private_data import get_device_id
from resources.lib.modules import hlshelper


    def onPlayBackStarted(self):
        # Will be called when xbmc starts playing a file
        control.log("Playback has started!")
        # if self.offset > 0: self.seekTime(float(self.offset))

    def onPlayBackEnded(self):
        # Will be called when xbmc stops playing a file
        control.log("setting event in onPlayBackEnded ")

    def onPlayBackStopped(self):
        # Will be called when user stops xbmc playing a file
        control.log("setting event in onPlayBackStopped")

        if self.stopPlayingEvent:
            self.stopPlayingEvent.set()

    def individualize(self, is_live, content_id, provider, format='mpd'):

        token, account = gettoken()
        device_id = get_device_id()
        profile = get_default_profile(account, device_id, token)

        useragent = 'ios' if format == 'm3u8' else 'web'

        if is_live:
            url = 'https://apim.oi.net.br/app/oiplay/oapi/v1/media/accounts/%s/profiles/%s/live/%s/individualize?deviceId=%s&tablet=false&useragent=%s' % (account, profile, content_id, device_id, 'ios' if format == 'm3u8' else 'web')
        else:
            url = 'https://apim.oi.net.br/app/oiplay/oapi/v1/media/accounts/{account}/profiles/{profile}/content/{content}/{provider}/individualize?deviceId={deviceId}&tablet=false&useragent={useragent}'.format(account=account, profile=profile, content=content_id, provider=provider.encode('utf-8'), deviceId=device_id, useragent=useragent)


        headers = {