Help with XML Parsing

Hi!



I’m playing around with the plugins capabilities of Plex. I more or less understand the structure and the workflow of plugins, I can do general programming in Python, but I don’t know too much on XML.



I’m trying to parse the contents of a query to the Spotify public API, which is in fact an HTML file as the following one:



<br />
<br />
<?xml version="1.0" encoding="utf-8"?> <br />
<artists xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.spotify.com/ns/music/1"> <br />
  <opensearch:Query role="request" startPage="1" searchTerms="u2"/> <br />
  <opensearch:totalResults>10</opensearch:totalResults> <br />
  <opensearch:startIndex>0</opensearch:startIndex> <br />
  <opensearch:itemsPerPage>100</opensearch:itemsPerPage> <br />
  <artist href="spotify:artist:51Blml2LZPmy7TTiAg47vQ"> <br />
    <name>U2</name> <br />
    <popularity>0.86445</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:3CtDccxbY3gFwNHTjRM9aq"> <br />
    <name>Various Artists - U2 Tribute</name> <br />
    <popularity>0.42919</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:5V1upZJErBcgcOP9ZHd4Vy"> <br />
    <name>Made famous by U2</name> <br />
    <popularity>0.34118</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:3dhoDqkI6atVLE43nkx8VZ"> <br />
    <name>LMC vs U2</name> <br />
    <popularity>0.33760</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:7JgPUKQFABI9fZlYIuNJmY"> <br />
    <name>Karaoke - U2</name> <br />
    <popularity>0.30951</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:79sIFMbGMA0vvACW90IwUt"> <br />
    <name>U2;Willie Nelson;Mickey Raphael</name> <br />
    <popularity>0.30162</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:7ypJwUskH6rxpPPCDTdV5V"> <br />
    <name>U2 Tribute Band</name> <br />
    <popularity>0.28478</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:0I1dbLRjzgDmDSLllie5aS"> <br />
    <name>The Allstars Cover LMC vs. U2</name> <br />
    <popularity>0.26643</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:02WKZN2nUu91lcaheGVcd8"> <br />
    <name>Bossa n U2 by Michael Sound</name> <br />
    <popularity>0.25545</popularity> <br />
  </artist> <br />
  <artist href="spotify:artist:4KaIn2quiXipktScBBt3jI"> <br />
    <name>Karaoke - U2 / Paul McCartney</name> <br />
    <popularity>0.20478</popularity> <br />
  </artist> <br />
</artists> <br />




My question is: could somebody help writing a parser for such a structure? It shouldn't be difficult for somebody familiar with XML, but for me it will mean struggling many hours with the XML library for a quite small piece of code.

Thanks in advance for your valuable time.

Regards.

are you able to parse html inside of a plugin? the difference is very minor.

Not actually… My problem is not technical, but conceptual.



I’m trying with a code I haven taken from another place:



<br />
    data = XML.ElementFromURL("http://ws.spotify.com/search/1/artist?q=U2")<br />
    PLUGIN_NAMESPACE   = {'opensearch':'http://a9.com/-/spec/opensearch/1.1/'}<br />
    Log(data.xpath('//artist', namespaces=PLUGIN_NAMESPACE))<br />




But it (obviously does not work). What I don't see is how should I use namespaces when querying the XML object. In the XML that I get in response, the xml starts with:


<artists xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.spotify.com/ns/music/1"> 



What I have understood from examples is that I should use the namescape "opensearch':'http://a9.com/-/spec/opensearch/1.1/" when querying for "totalResults", "startIndex", etc., I am wrong?

But, what about the artist blocks? I don't see how can I use xpath to obtain the list of artist in that XML?

I hope I explain better now.

Thanks for the quick response.

check out the deviant art plugin for a reference on namespaces. you can put both namespaces into one variable ( i believe that’s the right terminology) I helped pooploser write that one for his first plugin. It looks awefully similar to what you are trying to do.

I see. My problem was that I did no try to add a name to the anonymous namespace in my XML. Now I correctly parse the XML. Thanks!!!



Now I have another problem. I’m creating TrackItems for each of the parsed tracks. This TrackItems call a callback (this works) but I do not know what return here. My idea is to call a helper from the callback function which carries out some task and the return. But, what should I return from the callback? Plex is not intended to reproduce anything, but to just launch the actual reproductor in background. Is all this possible?



Thanks again!

After a little googling, it turns out there is a spotify app for plex. I would imagine that a lot of what you need help with (helper app) and what you may need help with in the future will be in that code as examples for you.

After re-reading my last post, I don’t want you to think I don’t want to help. I just wanted to point out a good resource for you. If you have any questions after looking at the spotify app, please continue to post them.

Hi tonitonae!



The callback function has to return a url to the location of the actual audio file.


No problem. I will take a look at the Spotify plugin, although my approach is a bit different (I'm only using the open API to query for music information, not the API to access directly to the music). Anyway, thanks for trying helping. Most of the times there's only the need for somebody to point in the right direction to find out what you were doing wrong.

Regards

Hi!,



thanks for your response.




Ok. So, what if I do not want to actually reproduce the music file with Plex but to launch a helper which does it in the background (without gaining focus). Is this possible? Should I use another Media Item to show the results of my query?

Regards, and thanks again for your time.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.