# Path: resources/lib/modules/workers.py
# class Thread(threading.Thread):
# class Workers:
#     def __init__(self, target, *args):
#     def run(self):
#     def get_result(self):
#     def globaltrace(self, frame, event, arg):
#     def localtrace(self, frame, event, arg):
#     def kill(self):
#     def __init__(self, concurrent=30, queue_size=None, target=None, expect_result=True):
#         def _queue_worker():
#     def put(self, item):
#     def join(self):
#     def terminate(self):
# 
# Path: resources/lib/modules/util.py
# class struct(object):
#     def __init__(self, kdict=None):
#     def __repr__(self):
#     def __getattr__(self, name):
#     def __len__(self):
#     def get(self, key):
# def getMPAAFromCI(ci):
# def getBestBitrateUrl(plugin, streams):
# def merge_dicts(x, *argv):
# def slugify(string):
# def unescape(text):
#     def fixup(m):
# def time_format(time_str=None, input_format=None):
# def get_signed_hashes(a):
# def J(a):
# def K(a):
# def L():
# def M(a):
# def N():
# def O(a):
# def P(a):
# def get_utc_delta():
# def strptime(date_string, format):
# def strptime_workaround(date_string, format='%Y-%m-%dT%H:%M:%S'):
# def get_total_seconds(timedelta):
# def get_total_seconds_float(timedelta):
# def get_total_hours(timedelta):
# def add_url_parameters(url, params):
# def is_number(s):
# G = 3600
# H = "=0xAC10FD"
# 
# Path: resources/lib/modules/cache.py
# def get(function, timeout_hour, *args, **kargs):
# def clear_item(function, *args, **kargs):
# def __get_from_cache(dbcur, table, f, a, timeout_hour):
# def __execute_origin(dbcur, dbcon, function, table, f, a, response, *args, **kargs):
# def delete_file():
# def clear(table=None):


import requests
import datetime
import player
from resources.lib.modules import workers
from resources.lib.modules import util
from resources.lib.modules import cache


def __merge_channel_data(channel, result):
    data = get_channel_epg_now(channel)
    result.append(data)


def get_channel_epg_now(channel):
    url = 'https://apim.oi.net.br/app/oiplay/ummex/v1/epg/{channel}/beforenowandnext?beforeCount=0&nextCount=0&includeCurrentProgram=true'.format(channel=channel)
    response = requests.get(url).json()

    now = response['schedules'][0]
    program = now['program']

    title = program['seriesTitle']
    series = u" (S" + str(program['seasonNumber']) + u':E' + str(program['episodeNumber']) + u")" if program['programType'] == 'Series' else u''
    episode_title = program['title'] + series if 'title' in program and program['title'] != title else ''
    studio = response['title']

    thumb = None
    fanart = None
    if 'programImages' in program and len(program['programImages']) > 0:
        thumb = next((image['url'] for image in program['programImages'] if image['type'] == 'Thumbnail'), None)
        fanart = next((image['url'] for image in program['programImages'] if image['type'] == 'Backdrop'), thumb) or thumb
        thumb = thumb or fanart

    logo = response['positiveLogoUrl']

    cast = [c['name'] for c in program['castMembers']]


# Path: resources/lib/modules/workers.py
# class Thread(threading.Thread):
# class Workers:
#     def __init__(self, target, *args):
#     def run(self):
#     def get_result(self):
#     def globaltrace(self, frame, event, arg):
#     def localtrace(self, frame, event, arg):
#     def kill(self):
#     def __init__(self, concurrent=30, queue_size=None, target=None, expect_result=True):
#         def _queue_worker():
#     def put(self, item):
#     def join(self):
#     def terminate(self):
# 
# Path: resources/lib/modules/util.py
# class struct(object):
#     def __init__(self, kdict=None):
#     def __repr__(self):
#     def __getattr__(self, name):
#     def __len__(self):
#     def get(self, key):
# def getMPAAFromCI(ci):
# def getBestBitrateUrl(plugin, streams):
# def merge_dicts(x, *argv):
# def slugify(string):
# def unescape(text):
#     def fixup(m):
# def time_format(time_str=None, input_format=None):
# def get_signed_hashes(a):
# def J(a):
# def K(a):
# def L():
# def M(a):
# def N():
# def O(a):
# def P(a):
# def get_utc_delta():
# def strptime(date_string, format):
# def strptime_workaround(date_string, format='%Y-%m-%dT%H:%M:%S'):
# def get_total_seconds(timedelta):
# def get_total_seconds_float(timedelta):
# def get_total_hours(timedelta):
# def add_url_parameters(url, params):
# def is_number(s):
# G = 3600
# H = "=0xAC10FD"
# 
# Path: resources/lib/modules/cache.py
# def get(function, timeout_hour, *args, **kargs):
# def clear_item(function, *args, **kargs):
# def __get_from_cache(dbcur, table, f, a, timeout_hour):
# def __execute_origin(dbcur, dbcon, function, table, f, a, response, *args, **kargs):
# def delete_file():
# def clear(table=None):


# Path: resources/lib/modules/oiplay/scraper_live.py
import requests
import datetime
import player
from resources.lib.modules import workers
from resources.lib.modules import util
from resources.lib.modules import cache



def __merge_channel_data(channel, result):
    data = get_channel_epg_now(channel)
    result.append(data)


def get_channel_epg_now(channel):
    url = 'https://apim.oi.net.br/app/oiplay/ummex/v1/epg/{channel}/beforenowandnext?beforeCount=0&nextCount=0&includeCurrentProgram=true'.format(channel=channel)
    response = requests.get(url).json()

    now = response['schedules'][0]
    program = now['program']

    title = program['seriesTitle']
    series = u" (S" + str(program['seasonNumber']) + u':E' + str(program['episodeNumber']) + u")" if program['programType'] == 'Series' else u''
    episode_title = program['title'] + series if 'title' in program and program['title'] != title else ''
    studio = response['title']

    thumb = None
    fanart = None
    if 'programImages' in program and len(program['programImages']) > 0:
        thumb = next((image['url'] for image in program['programImages'] if image['type'] == 'Thumbnail'), None)
        fanart = next((image['url'] for image in program['programImages'] if image['type'] == 'Backdrop'), thumb) or thumb
        thumb = thumb or fanart

    logo = response['positiveLogoUrl']

    cast = [c['name'] for c in program['castMembers']]


    date = util.strptime(now['startTimeUtc'], '%Y-%m-%dT%H:%M:%SZ') + util.get_utc_delta()