1

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> 
Brantje
  • 90
  • 9
  • 2
    Any chance you could use [JSON](http://en.wikipedia.org/wiki/Json)? Please? – Matt Ball Jul 19 '11 at 21:25
  • 4
    "Due the limitations of ajax i was forced to make use of the local resources, instead of my ajax class, that normally generates the output." - can you explain this? – slandau Jul 19 '11 at 21:26
  • 2
    @slandau that normally means "due to the limitations of the developer" ;) – AlienWebguy Jul 19 '11 at 21:32
  • 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 Jul 19 '11 at 21:32
  • lol. i don't know what kind of limitations AJAX has Brantje. Anything you can do on the server, you can do with AJAX - just make the call! Also, as Matt Ball said, you should be returning info in JSON format, and with that, you can easily separate the properties and access them on the front end – slandau Jul 19 '11 at 21:33
  • People, quit trying to format the string as code. You just keep screwing it up. – Wayne Jul 19 '11 at 21:48
  • @slandau I'm editing the current XBMC interface so it show's the now playing, volume etc. lwburk, i'm limited to the output of the http://wiki.xbmc.org/index.php?title=Web_Server_HTTP_API webserver API of XBMC – Brantje Jul 19 '11 at 23:44

2 Answers2

1

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
Wayne
  • 59,728
  • 15
  • 131
  • 126
0

Here's how I did it with regular expressions:

var data = "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";

function matchField(field, str) {
    // regexp: space, field, colon, (fewest number of chars), space, fewest number of non-whitespace chars, colon
    var re = new RegExp("\\s" + field + ":(.+?)\\s\\S+?:");
    try {
        return(str.match(re)[1]);
    } catch(e) {}
    return("");
}

var songTitle = matchField("Title", data);
var artist = matchField("Artist", data);
var duration = matchField("Duration", data);

And you can see it in action here: http://jsfiddle.net/jfriend00/Y9g7x/.

Results:

Title: Better Life
Artist: 3 Doors Down
Duration: 03:07

I think it's actually possible to do it all with one regular expression, but that would force the order of the different fields to be exactly the same every time.

jfriend00
  • 683,504
  • 96
  • 985
  • 979