#!/bin/bash
#
# Bash script for playing Live streams of Olympic Games.
#
# Thanks to: Martin Beránek (mousehouse) - providing URL extraction method
#            hikikomori82 - compatible sed with older version
#
#  Copyright (c) 2010 Radek Novacek
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

if [ $# -lt 1 ];
then
    echo "Missing argument. Use one of following: CT4OH, CT2OH, CTHD, ZOH1, ZOH2, ZOH3, ZOH4, ZOH5, ZOH6"
    exit 1
fi

cat << EOF > /tmp/data.xml
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <GetPlaylistUrl xmlns="http://ivysilani.visual.cz/services">
            <request>
                <Format>WH</Format>
                <ClientAddress>NDU0NjQ2NDQzfDYzNDAxNjA2NzMxNjgzNTAwMA==</ClientAddress>
                <Expiration>2010-02-12T23:00:33.586584+01:00</Expiration>
                <Playlist>
                    <PlaylistItem>
                        <Type>Live</Type>
                        <Identifier>$1</Identifier>
                    </PlaylistItem>
                </Playlist>
            </request>
        </GetPlaylistUrl>
    </soap:Body>
</soap:Envelope>
EOF


curl -H "Content-Type: text/xml; charset=utf-8" \
     -H "SOAPAction: \"http://ivysilani.visual.cz/services/GetPlaylistUrl\"" \
     -d "@/tmp/data.xml" http://ctdir.visual.cz/ivysilani/services/streaming/slp.asmx | \
sed 's/</\n</g' | grep GetPlaylistUrlResult | sed 's/>/\n/' | grep http | \
# Replace with your favorite player
xargs smplayer -playlist

