Skip to content

Instantly share code, notes, and snippets.

@anthonyeden
Last active May 4, 2020 08:16
Show Gist options
  • Save anthonyeden/121cb0695fcb1eaa8ae93ca4b7ea9a3f to your computer and use it in GitHub Desktop.
Save anthonyeden/121cb0695fcb1eaa8ae93ca4b7ea9a3f to your computer and use it in GitHub Desktop.
Telos ZipStream - Sample XML Metadata Filter
-- ------------------------------------------------------------------
-- MetaData Filter. Copyright (C) 2011 Tls Corporation
-- Author: Ioan L. Rus
--
-- Lines that start with two dashes (like this line) are comments.
--
-- Metadata filters are written in a very simple programming
-- language named Lua. Additional information about the language
-- and it's syntax can be found at http://www.lua.org.
--
-- This file implements a metadata filter that parses XML data.
-- For this example, the XML data expected will look like this:
--
-- <CurrentEvent>
-- <CutID>638</CutID>
-- <Type>MUS</Type>
-- <Duration>215000000</Duration>
-- <Title>I Will Follow</Title>
-- <Artist>Chris Tomlin</Artist>
-- <Album></Album>
-- <TimeStamp>2/16/2011 6:00:56 PM</TimeStamp>
-- </CurrentEvent>
--
-- ------------------------------------------------------------------
-- This tells the application that we wish to use an XML parser.
-- It is also possible (and in some cases more appropriate) to
-- use a line or character parser. Please see the other filter
-- examples for details.
UseXmlParser()
-- This function is called when a connection to the metadata
-- server is first established.
function OnConnect(clientAddress)
-- The print function sends data back to the client.
-- For this example we don't send anything back.
-- print("Hello ",clientAddress,"\r\n")
end
-- These functions are called by the application when a piece
-- of XML data is parsed. The functions extract the metadata
-- info then call SendMetaDataSong(title,url) to pass the info
-- back to the application to be included in the out going
-- streams.
-- TODO: Fill in the url to point to your website (or
-- any website where the listener can get more info
-- about the artist or song being played.
url=""
-- Work fields
currentElement=""
cuttype=""
title=""
artist=""
album=""
-- Called when an XML element start has been parsed. The name
-- of the element and a table of attributes are provided.
function OnElementStart(elementName,elementAttributeTable)
if elementName=="CurrentEvent" then
-- Reset all work fields
currentElement=""
cuttype=""
title=""
artist=""
album=""
else
-- Remember which element this is
currentElement=elementName
end
end
-- Called when the end of an element is detected.
function OnElementEnd(elementName)
-- LogInfo("XML Parser Sample: OnElementEnd\n")
if elementName=="CurrentEvent" then
-- Only send the title for entries that have a "MUS"
-- category. This will filter out metadata for ads.
if cuttype=="MUS" then
-- TODO: Modify this line if you wish to change the order of the items
local display=title
if artist~="" then
-- If the artist is not blank then add
-- a dash as a separator and the artist
display=display .. " - " .. artist
end
if album~="" then
-- If the album is not blank then add
-- a dash as a separator and the album
display=display .. " - " .. album
end
LogInfo("XML-Sample2: SendInfo('",display,"','",url,"')\n")
SendMetaDataSong(display,url)
end
end
end
-- Called when character data (outside of element tags) is
-- parsed. Please note that this will include any spaces,
-- tab characters or new line characters used to format the
-- XML data.
--
-- NOTE: You do not need to define functions that are not used.
function OnCharacterData(ch)
local char=string.char(ch) -- Convert character number to letter
if currentElement=="Type" then
-- Append character to cuttype variable
cuttype=cuttype .. char
elseif currentElement=="Title" then
-- Append character to title variable
title=title .. char
elseif currentElement=="Artist" then
-- Append character to artist variable
artist=artist .. char
elseif currentElement=="Album" then
-- Append character to album variable
album=album .. char
end
end
-- Called when an XML comment is detected. The comment string
-- contains the text of the comment.
--
-- NOTE: You do not need to define functions that are not used.
-- function OnComment(comment)
-- end
-- Marks the start of a CDATA section. Not used very often.
--
-- NOTE: You do not need to define functions that are not used.
-- function OnCdataStart()
-- end
-- Marks the start of a CDATA section. Not used very often.
--
-- NOTE: You do not need to define functions that are not used.
-- function OnCdataEnd()
-- end
-- NOTE: The line and character notification functions are
-- still called even for the XML parser. If you do not need
-- them, then just don't define them.
-- This function is called by the application each time a metadata
-- character is received.
function OnCharacterReceived(charNumber)
-- The lines below will echo the characters sent by the
-- metadata source back. This is useful when developing
-- the filter and for debugging. In this example, we
-- do nothing.
--
--local letter=string.char(charNumber) -- Convert character number to letter
--print(letter)
end
-- ------------------------------------------------------------------
-- MetaData Filter. Copyright (C) 2011 Tls Corporation
-- Author: Ioan L. Rus
--
-- Lines that start with two dashes (like this line) are comments.
--
-- Metadata filters are written in a very simple programming
-- language named Lua. Additional information about the language
-- and it's syntax can be found at http://www.lua.org.
--
--{This file implements a metadata filter that parses XML data.
-- For this example, the XML data expected will look like this:
--
-- <CurrentEvent> <Type>MUS</Type> <Title><![CDATA[Oleo]]></Title> <Artist><![CDATA[Miles Davis]]></Artist> </CurrentEvent>
-- ------------------------------------------------------------------
-- This tells the application that we wish to use an XML parser.
-- It is also possible (and in some cases more appropriate) to
-- use a line or character parser. Please see the other filter
-- examples for details.
UseXmlParser()
-- This function is called when a connection to the metadata
-- server is first established.
function OnConnect(clientAddress)
-- The print function sends data back to the client.
-- For this example we don't send anything back.
-- print("Hello ",clientAddress,"\r\n")
end
-- These functions are called by the application when a piece
-- of XML data is parsed. The functions extract the metadata
-- info then call SendMetaDataSong(title,url) to pass the info
-- back to the application to be included in the out going
-- streams.
-- TODO: Fill in the url to point to your website (or
-- any website where the listener can get more info
-- about the artist or song being played.
url="https://example.com"
-- Work fields
currentElement=""
title=""
artist=""
insideCdata=false
-- Called when an XML element start has been parsed. The name
-- of the element and a table of attributes are provided.
function OnElementStart(elementName,elementAttributeTable)
if elementName=="CurrentEvent" then
-- Reset all work fields
currentElement=""
title=""
artist=""
insideCdata=false
else
-- Remember which element this is
currentElement=elementName
end
end
-- Called when the end of an element is detected.
function OnElementEnd(elementName)
-- LogInfo("XML Parser Sample: OnElementEnd\n")
if elementName=="CurrentEvent" then
-- TODO: Modify this line if you wish to change the order of the items
local display=title
if artist~="" then
-- If the artist is not blank then add
-- a dash as a separator and the artist
display=display .. " - " .. artist
end
LogInfo("XML-Sample2: SendInfo('",display,"','",url,"')\n")
SendMetaDataSong(display,url)
end
end
-- Called when character data (outside of element tags) is
-- parsed. Please note that this will include any spaces,
-- tab characters or new line characters used to format the
-- XML data.
--
-- NOTE: You do not need to define functions that are not used.
function OnCharacterData(ch)
local char=string.char(ch) -- Convert character number to letter
--LogInfo("{"..char.."}")
if insideCdata then
if currentElement=="Title" then
-- Append character to title variable
title=title .. char
elseif currentElement=="Artist" then
-- Append character to artist variable
artist=artist .. char
end
end
end
-- Called when an XML comment is detected. The comment string
-- contains the text of the comment.
--
-- NOTE: You do not need to define functions that are not used.
-- function OnComment(comment)
-- end
-- Marks the start of a CDATA section. Not used very often.
--
-- NOTE: You do not need to define functions that are not used.
function OnCdataStart()
--LogInfo("<CdataStart>")
insideCdata=true
end
-- Marks the start of a CDATA section. Not used very often.
--
-- NOTE: You do not need to define functions that are not used.
function OnCdataEnd()
--LogInfo("<CdataEnd>")
insideCdata=false
end
-- NOTE: The line and character notification functions are
-- still called even for the XML parser. If you do not need
-- them, then just don't define them.
-- This function is called by the application each time a metadata
-- character is received.
function OnCharacterReceived(charNumber)
-- The lines below will echo the characters sent by the
-- metadata source back. This is useful when developing
-- the filter and for debugging. In this example, we
-- do nothing.
--
--local letter=string.char(charNumber) -- Convert character number to letter
--LogInfo("["..letter.."]")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment