# -*- coding: iso-8859-1 -*-
u"""
    MoinMoin - GlamMetalRAdio macro Version 0.1 ALPHA
    This macro will include your a laut.fm radio over an object tag

"""

from MoinMoin import wikiutil
import simplejson as json
import urllib2
Dependencies = ['time'] #do not cache, because we want updates of new songs

def macro_GlamMetalRadio(macro):
    request = macro.request
    html = u""
    parameters = []

    #loading current song and output the artist name and title
    try:
        current = urllib2.urlopen("http://api.laut.fm/station/rockheavy/current_song",None,2).read()
        data = json.loads(current)
        track = wikiutil.escape(data["artist"]["name"]) + " mit '" + wikiutil.escape(data["title"]) + "'"
    except:
        track = ""
    try:
        track = track + " von " + wikiutil.escape(data["releaseyear"])
    except:
        pass
    
    #generate the html code and javascript popup function
    html = u"""
        <div class="GlamMetalRadio">
        
        <script language="javascript" type="text/javascript">
            <!--
            function popitup(url) {
    	        newwindow=window.open(url,'name','height=320,width=310');
            	if (window.focus) {newwindow.focus()}
    	        return false;
            }
        // -->
        </script>
        
        <a href="http://www.laut.fm/standalone.php?station=rockheavy" title"Glam Metal Radio" onclick="return popitup('http://www.laut.fm/standalone.php?station=rockheavy')">
            Glam Metal Radiostation
        </a>

        <p class="track">%s</p>
            
        </div>
    """ % track

    
    return macro.formatter.rawHTML(html)

