import requests, datetime, time
from api_auth import APICredentials, APIParams
channel_id = 'Hak3zjnPLSW5o0j8GMpzRMsa' # Replace with the ID for the desired live channel.

def convert_ts(utc_tstamp):
    """
        Convert a UTC timestamp to ISO 8601 format (YYYY-MM-DDThh:mm:ss.sZ).
        :param utc_tstamp: The UTC timestamp to convert (expected milliseconds)
    """
    iso_format = "%Y-%m-%dT%H:%M:%S.%fZ"
    try:
        _ts = float("{0:.3f}".format(utc_tstamp/1000.0))
        return "{}{}".format(datetime.datetime.utcfromtimestamp(_ts).strftime(iso_format)[:-4], "Z")
    except Exception:
        print ("Invalid timestamp: {}".format(utc_tstamp))
        raise

class ScheduleEntry:   
    def __init__(self):
        self.host = "https://services.uplynk.com"

    def run(self):
        self._get_multiple_schedule_entries()

    def _get_multiple_schedule_entries(self):
        start = int(time.time() * 1000.0) # Now
        end = start + (120 * 60 * 1000)  # 120 minutes later
        start = convert_ts(start)
        end = convert_ts(end)
        url = "{}{}{}{}{}{}{}".format(self.host, "/api/v4/channels/", channel_id, "/schedules?start=", start, "&end=", end)

        headers = {'Content-Type': 'application/json'}

        response = requests.get(
            url, params=APIParams(APICredentials()).get_params({}), headers=headers
        )
  
        print(response.json())
  
ScheduleEntry().run()
