From 2346be671e0c54c6e2a983130a83da8d345e49e6 Mon Sep 17 00:00:00 2001
From: martin <martin@martin-ThinkPad-T410.(none)>
Date: Thu, 3 Mar 2011 20:13:55 -0500
Subject: [PATCH] -initial changes for CC-1944

---
 application/controllers/ApiController.php |  7 ++-
 application/models/Schedule.php           |  2 +-
 pypo/pypofetch.py                         | 11 +++++
 pypo/pypopush.py                          |  2 +-
 pypo/scripts/lib.liq                      | 51 ++++++++++++++++++++++
 pypo/scripts/ls_script.liq                | 52 ++++++-----------------
 6 files changed, 82 insertions(+), 43 deletions(-)
 create mode 100644 pypo/scripts/lib.liq

diff --git a/application/controllers/ApiController.php b/application/controllers/ApiController.php
index 22c97fe2e..0c0040135 100644
--- a/application/controllers/ApiController.php
+++ b/application/controllers/ApiController.php
@@ -126,7 +126,12 @@ class ApiController extends Zend_Controller_Action
         $from = $this->_getParam("from");
         $to = $this->_getParam("to");
         if (Schedule::ValidPypoTimeFormat($from) && Schedule::ValidPypoTimeFormat($to)) {
-            echo Schedule::ExportRangeAsJson($from, $to);
+            $result = Schedule::ExportRangeAsJson($from, $to);
+            $result['stream_metadata'] = array();
+            $result['stream_metadata']['format'] = "2";
+            $result['stream_metadata']['station_name'] = "z103";
+            $result['stream_metadata']['show_name'] = "dj danny D";
+            echo json_encode($result);
         }
     }
 
diff --git a/application/models/Schedule.php b/application/models/Schedule.php
index f3f74aa69..34063770d 100644
--- a/application/models/Schedule.php
+++ b/application/models/Schedule.php
@@ -688,7 +688,7 @@ class Schedule {
         $result['playlists'] = $playlists;
         $result['check'] = 1;
 
-        print json_encode($result);
+        return $result;
     }
 
 
diff --git a/pypo/pypofetch.py b/pypo/pypofetch.py
index fd79f3b1f..0134fd6a4 100644
--- a/pypo/pypofetch.py
+++ b/pypo/pypofetch.py
@@ -8,6 +8,7 @@ import pickle
 import random
 import string
 import json
+import telnetlib
 
 from api_clients import api_client
 from util import CueFile
@@ -82,11 +83,21 @@ class PypoFetch:
         if status == 1:
             logger.info("dump serialized schedule to %s", self.schedule_file)
             schedule = response['playlists']
+            stream_metadata = response['stream_metadata']
             try:
                 schedule_file = open(self.schedule_file, "w")
                 pickle.dump(schedule, schedule_file)
                 schedule_file.close()
 
+                tn = telnetlib.Telnet(LS_HOST, LS_PORT)
+
+                #encode in latin-1 due to this bug: http://bugs.python.org/issue1772794
+                tn.write(('vars.stream_metadata_type %s\n' % stream_metadata['format']).encode('latin-1'))
+                tn.write(('vars.show_name %s\n' % stream_metadata['show_name']).encode('latin-1'))
+                tn.write(('vars.station_name %s\n' % stream_metadata['station_name']).encode('latin-1'))
+                tn.write('exit\n')
+                logger.debug(tn.read_all())
+
             except Exception, e:
                 logger.critical("Exception %s", e)
                 status = 0
diff --git a/pypo/pypopush.py b/pypo/pypopush.py
index 3b2a39d67..782853060 100644
--- a/pypo/pypopush.py
+++ b/pypo/pypopush.py
@@ -130,7 +130,7 @@ class PypoPush:
             logger.debug('sleeping for %s s' % (sleep_time))
             time.sleep(sleep_time)
 
-            tn = telnetlib.Telnet(LS_HOST, 1234)
+            tn = telnetlib.Telnet(LS_HOST, LS_PORT)
 
             #skip the currently playing song if any.
             logger.debug("source.skip\n")
diff --git a/pypo/scripts/lib.liq b/pypo/scripts/lib.liq
new file mode 100644
index 000000000..68fd55459
--- /dev/null
+++ b/pypo/scripts/lib.liq
@@ -0,0 +1,51 @@
+def notify(m)
+  system("./notify.sh --data='#{!pypo_data}' --media-id=#{m['schedule_table_id']}")
+  print("./notify.sh --data='#{!pypo_data}' --media-id=#{m['schedule_table_id']}")
+end
+
+# A function applied to each metadata chunk
+def append_title(m) =  
+  if !stream_metadata_type == 1 then
+    [("artist","#{!show_name} - #{m['title']}")]
+  elsif !stream_metadata_type == 2 then
+    [("artist", ""), ("title", !show_name)]
+  elsif !stream_metadata_type == 3 then
+    [("artist",!station_name), ("title", !show_name)]
+  else
+    []
+  end
+end
+
+def crossfade(s)
+  s = fade.in(type="log", s)
+  s = fade.out(type="log", s)
+  fader = fun (a,b) -> add(normalize=false,[b,a])
+  cross(fader,s)
+end
+
+# Define a transition that fades out the
+# old source, adds a single, and then 
+# plays the new source
+def to_live(old,new) = 
+  # Fade out old source
+  old = fade.final(old)
+  # Compose this in sequence with
+  # the new source
+  sequence([old,new])
+end
+
+# Add a skip function to a source
+# when it does not have one
+# by default
+def add_skip_command(s)
+ # A command to skip
+ def skip(_)
+   source.skip(s)
+   "Done!"
+ end
+ # Register the command:
+ server.register(namespace="source",
+                 usage="skip",
+                 description="Skip the current song.",
+                 "skip",skip)
+end
diff --git a/pypo/scripts/ls_script.liq b/pypo/scripts/ls_script.liq
index e7fbfeb3a..a9ca8708b 100644
--- a/pypo/scripts/ls_script.liq
+++ b/pypo/scripts/ls_script.liq
@@ -10,48 +10,18 @@ queue = audio_to_stereo(queue)
 
 pypo_data = ref '0'
 web_stream_enabled = ref false
+stream_metadata_type = ref 0
+station_name = ref ''
+show_name = ref ''
 
-def notify(m)
-  system("./notify.sh --data='#{!pypo_data}' --media-id=#{m['schedule_table_id']}")
-  print("./notify.sh --data='#{!pypo_data}' --media-id=#{m['schedule_table_id']}")
-end
+%include "lib.liq"
 
-def crossfade(s)
-  s = fade.in(type="log", s)
-  s = fade.out(type="log", s)
-  fader = fun (a,b) -> add(normalize=false,[b,a])
-  cross(fader,s)
-end
+server.register(namespace="vars", "pypo_data", fun (s) -> begin pypo_data := s "Done" end)
+server.register(namespace="vars", "web_stream_enabled", fun (s) -> begin web_stream_enabled := (s == "true") string_of(!web_stream_enabled) end)
+server.register(namespace="vars", "stream_metadata_type", fun (s) -> begin stream_metadata_type := int_of_string(s) s end)
+server.register(namespace="vars", "show_name", fun (s) -> begin show_name := s s end)
+server.register(namespace="vars", "station_name", fun (s) -> begin station_name := s s end)
 
-# Define a transition that fades out the
-# old source, adds a single, and then 
-# plays the new source
-def to_live(old,new) = 
-  # Fade out old source
-  old = fade.final(old)
-  # Compose this in sequence with
-  # the new source
-  sequence([old,new])
-end
-
-# Add a skip function to a source
-# when it does not have one
-# by default
-def add_skip_command(s)
- # A command to skip
- def skip(_)
-   source.skip(s)
-   "Done!"
- end
- # Register the command:
- server.register(namespace="source",
-                 usage="skip",
-                 description="Skip the current song.",
-                 "skip",skip)
-end
-
-server.register(namespace="vars", "pypo_data", fun (s) ->  begin pypo_data := s "Done" end)
-server.register(namespace="vars", "web_stream_enabled", fun (s) ->  begin web_stream_enabled := (s == "true") string_of(!web_stream_enabled) end)
 
 default = single(conservative=true, "/opt/pypo/files/basic/silence.mp3")
 default = rewrite_metadata([("artist","Airtime"), ("title", "offline")],default)
@@ -60,7 +30,6 @@ s = fallback(track_sensitive=false, [queue, default])
 
 s = on_metadata(notify, s)
 s = crossfade(s)
-
 # Attach a skip command to the source s:
 add_skip_command(s)
 
@@ -78,6 +47,9 @@ s = switch(track_sensitive = false,
     ]
 )
 
+s = map_metadata(append_title, s)
+
+
 if output_sound_device then
 	out_device = out(s)
 end