View Single Post
Posts: 8 | Thanked: 22 times | Joined on Jan 2010
#1
Media player in Maemo 5 stores the internet radio station in a sqlite database. The database is located in '/home/seadog/.mafw.db' and contains only one table 'iradiobookmarks' with three columns 'id', 'key', 'value'.

Id is a integer value and it's not unique
Key is a string defining the type of data stored in value.
Value is a blob build in a special way explained later.

Each station must have at least three entries in the database with the same id (so they are grouped) and with keys "title", "mime-type" and "uri". Other keys also exist like "last-played" etc.

"Title" defines the title which is shown in the UI
"Mime-type" is "audio" for radio stations
"Uri" is the location of the stream

The "value" column contains the actual value of the "key" column. Because "value" can store different kind of data, depending on the "key" column, is defined as a binary blob. The format of the blob is "\x01\x00\x00\x00@\x00\x00\x00string that we want\x00"

Because value is a blob we need some easy python scripting to produce valid entries in our database.

Code:
    def findLargestId(cursor):
        """ Finds the largest id so we can add stations after that """
        cursor.execute("SELECT MAX(id) FROM iradiobookmarks")
        row = cursor.fetchone()
        if row:
            return row[0]
        else:
            return -1

    def buildBlob(self, string):
        """ Builds the needed blob for value column """
        return sqlite3.Binary(chr(1) + chr(0) * 3 + '@' + chr(0) * 3 + string + chr(0) )

    # Check if station in already listed and return it's id.
    # Used to do updates        
    def findStationId(self, cursor, station):
        """ finds the id of an existing station """
        cursor.execute('SELECT id FROM iradiobookmarks WHERE key="title" and value=?', (self.buildBlob(station),))
        row = cursor.fetchone()
        if row:
            return row[0]
        else:
            return -1

    def insertIntoDB(cursor, title, uri):
            """ inserts station in db"""
            nextID = self.findLargestId(cursor)+1
            stationId = self.findStationId(cursor, title)
            if stationId == -1:
                # station is not listed. Add it to the list with the next ID
                stationId = nextID
                nextID += 1
                cursor.execute('INSERT INTO iradiobookmarks VALUES (?, "title", ?)', (stationId, self.buildBlob(title),))
                cursor.execute('INSERT INTO iradiobookmarks VALUES (?, "mime-type", ?)', (stationId, self.buildBlob("audio"),))
                cursor.execute('INSERT INTO iradiobookmarks VALUES (?, "uri", ?)', (stationId, self.buildBlob(uri),))
            else:
                # station is listed. Update the uri entry
                cursor.execute('UPDATE iradiobookmarks SET value = ? WHERE id = ? AND key = "uri" ', (self.buildBlob(uri), stationId))


conn = sqlite3.connect("/home/user/.mafw.db")
cursor = conn.cursor()

So for example to store a station with name "Foobar FM" and stream location "mms://example.com/stream.asx" we call insertIntoDb(cursor, "Foobar FM", "mms://example.com/stream.asx") and the function updates the station uri if the station is already there, or makes a new entry.

Note: Before updating the database you should stop the media play internet daemon by typing

/usr/bin/mafw.sh stop mafw-iradio-source

and after updating enable it again

/usr/bin/mafw.sh start mafw-radio-source

If you ignore this step you want see your new entries until you reboot and you may destroy the database.

You can find a working example of database updating the internet radio stations list under the devel repository named 'greekiradio'. The application creates feeds for greek internet radio stations from www.eradio.gr and inserts them into the database.

http://maemo.org/packages/view/greekiradio/
 

The Following 15 Users Say Thank You to seadog For This Useful Post: