Search code examples
javascriptajaxregexxbmc

Matching with javascript


I'm currently developing a webinterface for XBMC witch contains ajax. Due the limitations of the out ajax i was forced to make use of the local resources, instead of my ajax class, that normally generates the output. I have a string. The * means, this text can change.

This is the string:

Filename:smb://SERVER/Music/3 Doors Down/2000 The Better Life/07 Better Life.mp3 PlayStatus:Playing SongNo:6 Type:Audio Title:Better Life Track:7 Artist:3 Doors Down Album:The Better Life Genre:Alternative Year:2000 URL:smb://xbox:xbox@SERVER/Music/3 Doors Down/2000 The Better Life/07 Better Life.mp3 Lyrics: Bitrate:193 Samplerate:44 Thumb:DefaultAlbumCover.png Time:02:05 Duration:03:07 Percentage:66 File size:4509417 Changed:False

I want to know how i can match the Title, Arist, Time and Duration. I tried with regex, but no success, because i don't have so much JS knowledge.

Thanks, Brantje

EDIT: "Are you sure that's the string? All run together like that with no newlines? Edit: I edited the question to fix the formatting. – Ariel 2 hours ago"

Nope, The output from http://xbox/xbmcCmds/xbmcHttp?command=GetCurrentlyPlaying Looks like this when playing a video

HTML code:

<html> 
<li>Filename:smb://SERVER/Movies/Drive Angry/Drive Angry (2011) DVDRip XviD-MAXSPEED.avi
<li>PlayStatus:Playing
<li>VideoNo:0
<li>Type:Video
<li>Thumb:DefaultVideoCover.png
<li>Time:00:00:28
<li>Duration:01:44:31
<li>Percentage:0
<li>File size:1666804442
<li>Changed:False</html> 

When playing music its abit different.

<html> 
<li>Filename:smb://SERVER/Music/3 Doors Down/2000 The Better Life/01 Kryptonite.mp3
<li>PlayStatus:Playing
<li>SongNo:-1
<li>Type:Audio
<li>Title:Kryptonite
<li>Track:1
<li>Artist:3 Doors Down
<li>Album:The Better Life
<li>Genre:Alternative
<li>Year:2000
<li>URL:smb://xbox:xbox@SERVER/Music/3 Doors Down/2000 The Better Life/01 Kryptonite.mp3
<li>Lyrics:
<li>Bitrate:192
<li>Samplerate:44
<li>Thumb:DefaultAlbumCover.png
<li>Time:00:05
<li>Duration:03:54
<li>Percentage:2
<li>File size:5618471
<li>Changed:False</html> 

Solution

  • Assuming the following string:

    var str = "Filename:smb://SERVER/Music/3 Doors Down/2000 The Better Life/07 Better Life.mp3 PlayStatus:Playing SongNo:6 Type:Audio Title:Better Life Track:7 Artist:3 Doors Down Album:The Better Life Genre:Alternative Year:2000 URL:smb://xbox:xbox@SERVER/Music/3 Doors Down/2000 The Better Life/07 Better Life.mp3 Lyrics: Bitrate:193 Samplerate:44 Thumb:DefaultAlbumCover.png Time:02:05 Duration:03:07 Percentage:66 File size:4509417 Changed:False";
    

    Extract the properties into a dictionary/map:

    var dict = (" " + str).split(/ (\w+):/).reduce(function(acc, el, i, orig) {
        if (i % 2)
            acc[el] = orig[i + 1];
        return acc;
    }, {});
    

    Here's the same thing without a higher-order function:

    var i, dict = {}, pair = (" " + str).split(/ (\w+):/);
    for (i = 1; i < pair.length; i += 2)
        dict[pair[i]] = pair[i + 1];
    

    Retrieving values is now trivial:

    console.log(dict["Title"]);
    console.log(dict["Artist"]);
    console.log(dict["Time"]);
    console.log(dict["Duration"]);
    

    Output:

    Better Life
    3 Doors Down
    02:05
    03:07