#!/usr/local/bin/emacs -script

(require 'json)
(require 'parse-time)

(dolist (tz '((-8 . "PST") (-7 . "MST") (-6 . "CST") (-5 . "EST") (-4 . "AST")))
  (with-current-buffer (get-buffer-create
                        (format "TSN Schedule %s" (cdr tz)))
    (erase-buffer)
    (setq buffer-file-coding-system 'raw-text)
    (insert "-----------------\n")
    (insert (format "### %s (%d) ###\n" (cdr tz) (car tz)))
    (insert "-----------------\n")
    (dolist (ch '(1 2 3 4 5))    
      (insert (format "*** TSN-%d ***\n" ch))
      (let ((data nil))
        (with-current-buffer
            (let ((url (concat
                        "https://capi.9c9media.com/destinations/tsn_web/platforms/desktop/channelAffiliates/tsn%d/schedules"
                        "?starttime="
                        (format-time-string
                         "%FT%T%z"
                         (current-time) -28800)
                        "&endtime="
                        (format-time-string
                         "%FT%T%z"
                         (time-add (current-time)
                                   (* 7 24 60 60)) -28800))))
              (url-retrieve-synchronously
               (format url ch)))
          (goto-char (point-min))
          (search-forward "{" nil t)
          (setq data (json-read-from-string
                      (buffer-substring-no-properties
                       (- (point) 3) (point-max)))))
        (dolist  (a (append (cdr (assoc 'Items data)) '()))
          (insert
           (format "%30s"
                   (format-time-string
                    "%A %B %d %l:%S%p"
                    (parse-iso8601-time-string
                     (alist-get 'StartTime a))
                    (* (car tz) (* 60 60))))
           (format "   %s" (alist-get 'Name a))
           "\n")))
      (insert "\n\n"))
    (write-region (point-min) (point-max)
                  (format "~/gopher/tsn/tsn-%s.txt" (cdr tz)))
    (write-region (point-min) (point-max)
                  (format "~/html/tsn-%s.txt" (cdr tz)))))





(dolist (tz '((-8 . "PST") (-7 . "MST") (-6 . "CST") (-5 . "EST") (-4 . "AST")))
  (with-current-buffer (get-buffer-create
                        (format "TSN Schedule %s" (cdr tz)))
    (erase-buffer)
    (setq buffer-file-coding-system 'raw-text)		
		(let ((allprogs nil)
					(today (encode-time (parse-time-string (format-time-string  "%FT00:00:00%z" (current-time) (* 60 60 (car tz)))))))
			(dolist (i '(1 2 3 4 5))
				(let* ((url (concat
										 (format "https://capi.9c9media.com/destinations/tsn_web/platforms/desktop/channelAffiliates/tsn%d/schedules" i)
								     "?starttime="
                     (format-time-string
                      "%FT%T%z"
                      today (* (car tz) 60 60))
                     "&endtime="
                     (format-time-string
                      "%FT%T%z"
                      (time-add (current-time)
                                (* 7 24 60 60))
											(* (car tz) 60 60)))) 
							 (req (url-retrieve-synchronously url)))	
;;;(setq buf req)
					(with-current-buffer req
						(goto-char (point-min))
						(search-forward "{" nil t)
						(let ((data (json-read-from-string (buffer-substring-no-properties (- (point) 3) (point-max)))))
							(dolist  (a (append (cdr (assoc 'Items data)) '()))
								(let* ((tm (seq-take (parse-iso8601-time-string (alist-get 'StartTime a)) 2))
											 (prog (alist-get 'Name a)))
									(if (null (assoc tm allprogs))
											(push (cons tm (list (cons i prog))) allprogs)
										(push (cons i prog) (cdr (assoc tm allprogs))))
									))))))
;;;(setq ap allprogs)
			(let ((st today)
						(prev-prog '((1) (2) (3) (4) (5))))
				(dotimes (i (* 24 2 7))
					(let ((tm (time-add st (* i 60 30))))
						(insert (format-time-string "\n%A %B %d %l:%M%p" tm -28800))
						(insert "\n")
						(let* ((tm1 (seq-take (time-convert tm) 2))
								   (progs (reverse (alist-get (list (car tm1) (cadr tm1)) allprogs nil nil 'equal))))
							(dolist (i '(1 2 3 4 5))
								(insert (format "TSN-%d - " i))
								(let* ((progname (alist-get i progs))
										   (prog (or progname (alist-get i prev-prog) "")))
									(insert prog "\n")
									(setf (cdr (assoc i prev-prog)) prog))))))
				(write-region (point-min) (point-max) (format "~/gopher/tsn/tsn-alt-%s.txt" (cdr tz)))
				(write-region (point-min) (point-max) (format "~/html/tsn-alt-%s.txt" (cdr tz)))
				))))