stuck at parsing xml from URL

Hello everyone,

 

I'm new to python programming and I use plex for about 2-3 weeks now because it gave me almost every feature which I needed. So I thought while reading the plugin walkthrough how hard could it be to write my own metadata Agent so I gave it a shot. Now I'm stuck and I don't know how to fix my problem.

 

What do I try to do exactly? Like I said, I try to write a metadata Agent for the Anime Database myanimelist.net because all my anime are organized with myanimelist in mind so why not use alle the data from myanimelist.net?! Since myanimelist provide a REST Interface with HTTP Authentication.

 

First of some code:

import sys
import os
import urllib2
import base64
from xml.dom import minidom

USERNAME = Prefs[“username”]
PASSWORD = Prefs[“password”]
BASE_URL = “http://myanimelist.net/api/anime/search.xml?q=

def Start():
Log(“Starting MALAgent”)

class MALAgentTV(Agent.TV_Shows):

name = “MyAnimeList.net
languages = [ Locale.Language.English ]
primary_provider = True

def search(self, results, media, lang):

Log("Search for anime: " + media.show)

malURL = BASE_URL + media.show
Log(malURL)
malRequest = urllib2.Request(malURL)
base64string = base64.encodestring('%s:%s' % (USERNAME, PASSWORD)).replace('

', ‘’)
malRequest.add_header(“Authorization”, “Basic %s” % base64string)
malData = urllib2.urlopen(malRequest)

xmldoc = minidom.parse(malData)

return

def update(self, metadata, media, lang):
return

the problem is that with some requests I'll get an error

ExpatError: undefined entity: line 14, column 169

 

which is xmldoc = minidom.parse(malData)

 

the funny thing about this error is that it doesn't get it on every search

 

after googling, mailinglists, forum posts and other sources it could be that the XML response is incorrect (some say because of the doctype)

 

here is a response if i search for bleach

<?xml version="1.0" encoding="utf-8"?>

  
    269
    Bleach
    Bleach
    
    366
    8.01
    TV
    Finished Airing
    2004-10-05
    2012-03-27
    15-year-old Kurosaki Ichigo is not your everyday high school student. He has from as far he can remember always had the ability to see ghosts and spirits. A fateful day arrives as Ichigo encounters the shinigami Kuchiki Rukia, who saves him and his family from a Hollow at the cost of injuring herself. During this encounter, with Rukia unable to defeat the hollow she transfers her shinigami powers into Ichigo. In the aftermath, unable to continue with her job, Rukia allows Ichigo to take on the role of a shinigami in her place as they together defeat the Hollows plaguing Ichigo's town. 
    http://cdn.myanimelist.net/images/anime/3/20349.jpg
  

But actually I have no Idea what I need to do to fix that. I tried Minidom, ElementTree nothing seems to work.

 

So if anyone could help me with this I would be really grateful

Hi!

You can use build-in framework functions for almost everything you need:

USERNAME = Prefs["username"]
PASSWORD = Prefs["password"]
BASE_URL = "http://myanimelist.net/api/anime/search.xml?q="

def Start():
Log(“Starting MALAgent”)

class MALAgentTV(Agent.TV_Shows):

name = “MyAnimeList.net
languages = [ Locale.Language.English ]
primary_provider = True

def search(self, results, media, lang):

Log("Search for anime: " + media.show)

mal_url = BASE_URL + String.Quote(media.show, usePlus=True)
Log(mal_url)

auth = String.Base64Encode('%s:%s' % (USERNAME, PASSWORD))
headers = {'Authorization': 'Basic %s' % auth}

xml = XML.ElementFromURL(mal_url, headers=headers)


return

def update(self, metadata, media, lang):
return

Many thanks sander1 this helped me a lot to finish the agent.

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