0.020
thk thk Τετ. 28 Ιαν. 2009 19:16 tags python 0 views
It's nice when the server and the client talk, but it is more nice when the servers talk to each other.
Since I did not come upon any python code of implementing that , here is a python snippet to get icecast2 stats. This function gets stats of an icecast2 mount source you want to track.  In my case all I wanted was the listeners number. So i am calling it like self.getIcecastStats("/playlist",['listeners']). It returns a dictionary . In my case it will be like : {'listeners':2}.
..Oh I forgot. This functionality is now part of kaotonik since if you are logged in you can view in the start page , how many are listening to radio right now.
Handle with care , as you see there is no exception handling .

  
    def getIcecastStats(self,source_mount,  keys):
        """gets icecast2 stats  of the specified source_mount. For this source
        it will return the keys values in a dict."""
        import urllib2
        password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
        # create a password manager
        # Add the username and password.
        # If we knew the realm, we could use it instead of ``None``.
        top_level_url = "http://127.0.0.1:8000"
        username="admin"
        password="password"
        password_mgr.add_password(None, top_level_url, username, password)

        handler = urllib2.HTTPBasicAuthHandler(password_mgr)

        # create "opener" (OpenerDirector instance)
        opener = urllib2.build_opener(handler)

        # use the opener to fetch a URL
        f=opener.open(top_level_url + "/admin/stats.xml")
        xmls=f.read()

        from xml.dom import minidom
        domObj = minidom.parseString(xmls)
        nodes = domObj.getElementsByTagName('source')
        retD={}
        for nod in nodes:
            if nod.getAttribute('mount') == source_mount:
                for key in keys:
                    vnodes = nod.getElementsByTagName(key)
                    if vnodes:
                        retD[key]=vnodes[0].firstChild.nodeValue

                break
        return retD