Need help with extracting text from curl command into variable


#1

Hi guys

so i want to use KM to execute a command every 5 seconds that is a text file in XML format, extract specific parts from it and send it to variable or text file

the command is curl:

curl http://localhost:52199/MCWS/v1/Playback/Info\?Zone\=-1 

and the output looks like this

<Response Status="OK">
<Item Name="ZoneID">10014</Item>
<Item Name="ZoneName">SonyBT</Item>
<Item Name="State">2</Item>
<Item Name="FileKey">1470603</Item>
<Item Name="NextFileKey">1470604</Item>
<Item Name="PositionMS">10499</Item>
<Item Name="DurationMS">101506</Item>
<Item Name="ElapsedTimeDisplay">0:10</Item>
<Item Name="RemainingTimeDisplay">-1:31</Item>
<Item Name="TotalTimeDisplay">1:41</Item>
<Item Name="PositionDisplay">0:10 / 1:41</Item>
<Item Name="PlayingNowPosition">7</Item>
<Item Name="PlayingNowTracks">13</Item>
<Item Name="PlayingNowPositionDisplay">8 of 13</Item>
<Item Name="PlayingNowChangeCounter">153</Item>
<Item Name="Bitrate">883</Item>
<Item Name="Bitdepth">16</Item>
<Item Name="SampleRate">44100</Item>
<Item Name="Channels">2</Item>
<Item Name="Chapter">0</Item>
<Item Name="Volume">0.76</Item>
<Item Name="VolumeDisplay">76% (-12.0 dB)</Item>
<Item Name="ImageURL">MCWS/v1/File/GetImage?File=1470603</Item>
<Item Name="Artist">Arcade Fire</Item>
<Item Name="Album">Everything Now</Item>
<Item Name="Name">Infinite_Content</Item>
<Item Name="Status">Playing</Item>
</Response>

I want to extract the Artist part (ie "Arcade Fire") and the Name (ie "Infinite_Content")

does KM do stuff like that?

Ideally after the extract i would send the album-name into a text file or a KM variable for further processing

I have this so far

best

Z


#2

Hi Z,

Yes, KM definitely does stuff like this. Here's a sample macro that shows how to extract the Artist and Name fields from a variable with the kind of output you listed and send them into their own variables:

Example Macro.kmmacros (4.0 KB)
image


#3

@gglick This is EPIC
thx so much for the guidance
Some additional questions arose if you done mind :smiley:
(Below in the screen shot is the current state of the macro)

  1. I cant seem to get the macro to execute every few seconds only manually, any clue what im missing here?
  2. i want to write the different variables into a text file. i have tried the write variable actions yet i cant seem to find whats the best way to clear the text file and then add the different variables with spaces between them (or even better with use defined separators) ie "Artist-Name")
  3. the regex fu is waay above my level :slight_smile: if i wanted to add a 3rd variable ("album") can you guide me how to do so or decipher the regex :slight_smile:

thx so much again its really appreciated!

Z


#4

You're welcome. Glad you're happy with it. To tersely answer your other questions:

  1. Based on the "when macro group is active" message in your screenshot, I'm guessing your macro is in a macro group that is not set to be active in all windows and applications. Try moving it to the default Global Macro Group and seeing if that doesn't resolve it.

  2. I'm unsure what you mean by "clear the text file", but assuming you want to keep adding to a single text file rather than erasing it and starting over every time the macro runs, I believe you should stick with all Append to File actions rather than the one Write to File you currently have for Artist. As for spacing and separators, I would use Append Text rather than Append Variable, as that lets you use as many line breaks or other separators you want. Example:

image


---------------------------------

Artist-Name
%Variable%LocalArtist%
  1. Try this modified regex:
    (?s)<Item Name="Artist">([^<]+)</Item>.*<Item Name="Album">([^<]+)</Item>.*<Item Name="Name">([^<]+)</Item>
    and make sure the corresponding action is set up like this:

image