import json, requests, datetime, time
from api_auth import APICredentials, APIParams
channel_id = 'Hak3zjnPLSW5o0j8GMpzRMsa' # Replace with the ID for the desired live channel.
schedule_id = '2158c7452b7841158972e019a5bd5f12'  # Replace with the ID for the desired schedule entry.

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):
        """
        Update a schedule entry.
        """
        self._update_schedule_entry()

    def _update_schedule_entry(self):
        url = "{}{}{}{}{}".format(self.host, "/api/v4/channels/", channel_id, "/schedules/", schedule_id)

        start = int((time.time() + 7200) * 1000.0) # 2 hours from now
        start = convert_ts(start)

        payload = {            
            'start': start
        }

        headers = {'Content-Type': 'application/json'}

        response = requests.patch(
            url, params=APIParams(APICredentials()).get_params({}), data=json.dumps(payload), headers=headers 
        )

        print(response.json())

ScheduleEntry().run()