import json, 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 DeleteMultipleScheduleEntries:
    def __init__(self):
        self.host = "https://services.uplynk.com"

    def run(self):
        """
        Deletes a schedule entry.
        """
        self._delete_schedule_entry()

    def _delete_schedule_entry(self):
        start = int((time.time() + 3600) * 1000.0) # 1 hour from now
        start = convert_ts(start)

        end = int((time.time() + 7200) * 1000.0) # 2 hours from now
        end = convert_ts(end)

        url = "{}{}{}{}{}{}{}".format(self.host, "/api/v4/channels/", channel_id, "/schedules?start=", start, "&end=", end)
 
        response = requests.delete(
            url, params=APIParams(APICredentials()).get_params({})
        )

        print(response.json())

DeleteMultipleScheduleEntries().run()