json
#1
[WINDOWS]

I been googling and searching this forum for days to no avail. One of the "shortcomings" of XBMC is it doesn't know which DVD or Blu-Ray is in the drive. So, I'm writing a python script to query imdbapi and parse the json response.

It took me just an hour or so to get all this working in Python on Windows using the IDLE editor. Unfortunately, when I go to run it in XBMC, I get " ImportError: No module named json". I tried simplejson but ... import error.

I just want to take the json response from imdb and parse it "pythonically" without looking for squiggly-brackets, colons, quotes, commas ... yuch!

Here's a simple version of my code which works like a dream in IDLE but can't see json from XBMC. NOTE: for now, I hard-coded my "X:" drive.

Code:
import os
import subprocess
import json

try:
    import urllib.request as urllib
except:
    import urllib2 as urllib


for vol in os.popen('wmic logicaldisk where drivetype=5 get name,volumename').readlines():
    if vol[0:2] == 'X:':
        title = vol[6:].replace('_',' ')
        if len(title.strip()) == 0:
            title = 'no disc'
        else:
            imdbURL = urllib.quote('http://www.imdbapi.com/?t=%s' % title, safe="%/:=&?~#+!$,;'@()*[]")
            results = urllib.urlopen(imdbURL)
            imdbJson = json.loads(results.read().decode('utf-8'))
            if imdbJson['Response'] == 'True':
                title = imdbJson['Year'] + ' - ' + imdbJson['Title']
            else:
                title = imdbJson['Response']
print(title)
Reply
#2
jaja714 Wrote:Unfortunately, when I go to run it in XBMC, I get " ImportError: No module named json". I tried simplejson but ... import error.

in your addon.xml put the following inside the <requires> tag:
Code:
<import addon="script.module.simplejson" version="2.0.10"/>

(think you might have to restart xbmc for it to take effect?)

and then in your script you should be able to
Code:
import simplejson as json

hope that helps!
Reply
#3
Nope, using ...

Code:
try:
    import json
except:
    import simplejson as json

... in my script, I'm getting another import error...

Code:
14:01:05 T:4300 M:2142453760  NOTICE: -->Python Interpreter Initialized<--
14:01:05 T:4300 M:2139394048   ERROR: Error Type: exceptions.ImportError
14:01:05 T:4300 M:2139394048   ERROR: Error Contents: No module named simplejson
14:01:05 T:4300 M:2139389952   ERROR: Traceback (most recent call last):
                                              File "C:\Users\Jeff\AppData\Roaming\XBMC\addons\script.xbmc.xbmciso\default.py", line 12, in ?
                                                import simplejson as json
                                            ImportError: No module named simplejson

Here's my addon.xml:

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.xbmc.xbmciso"
       name="xbmciso"
       version="1.0.0"
       provider-name="jaja">
  <requires>
    <import addon="xbmc.python" version="1.0"/>
    <import addon="script.module.simplejson" version="2.0.10"/>
  </requires>
  <extension point="xbmc.python.script"
             library="default.py">
    <provides>video</provides>
  </extension>
  <extension point="xbmc.addon.metadata">
    <summary lang="en">xbmciso</summary>
    <description lang="en">xbmciso</description>
    <platform>all</platform>
  </extension>
</addon>
Reply
#4
hmmm, you've got me there.

i copied and pasted your addons.xml, made a default.py that just said
Code:
try:
    import json
except:
    import simplejson as json
    
print 'hello'

zipped it up and did 'install from zip' in a fresh install of xbmc.

you could see the simplejson package download and install in the addons directory and the code just printed 'hello' as expected.

don't know what else to suggest Sad
Reply
#5
I got so used to modifying and testing my .py script on the fly that I didn't know you had to zip/re-install when modifying addon.xml.

So, I was able to get past the first error but now I'm onto another error ... .quote!

All this stuff works in IDLE! Why can't XBMC use json? Why can't XBMC use urllib.request? Why is IDLE's version of python different than XBMC's anyway? I've been a programmer for 25 years and I've never seen a language like python where new versions are not backwards compatible with previous versions. I just want to be able to test in IDLE and then have it work in XBMC!
Reply
#6
jaja714 Wrote:I got so used to modifying and testing my .py script on the fly that I didn't know you had to zip/re-install when modifying addon.xml.

So, I was able to get past the first error but now I'm onto another error ... .quote!

All this stuff works in IDLE! Why can't XBMC use json? Why can't XBMC use urllib.request? Why is IDLE's version of python different than XBMC's anyway? I've been a programmer for 25 years and I've never seen a language like python where new versions are not backwards compatible with previous versions. I just want to be able to test in IDLE and then have it work in XBMC!

What version of Python are you using? XBMC uses an internal version of python(version 2.4) Though XBMC Eden(11) will use the an internal version of Python(2.6 -2.7 usually) There are subtle differences between version of Python, not too much really(newer versions have more modules and some that are re-arranged - such urllib and urllib2.)

Depending on your Result from your JSON call it should be fairly simple to convert it in to dict format that python really likes.

I'll check out your code a bit later and see what the result is, then I'll see if the way I am thinking will work...

This discussion probably should be move to the Python Development thread...
Reply
#7
I thought I was using the version of python that came with XBMC which I started using just a few months ago. I never downloaded a version of Python explicitly but, when I go into IDLE, it says I'm using 3.2. I guess a new version of python must have come along with my download of IDLE.

If python is going to evolve such that new versions are not 100% backward compatible with previous versions, there should be ONE include file which reconciles those differences (ex; include simplejson as json). Instead, they just leave it to each individual developer to wrestle for days and weeks until they stumble upon each answer after pouring through hundreds of pages of forum posts.
Reply
#8
Guys, I couldn't wait for an answer on json, so I moved on. Instead of querying imdbapi (which returns json), I am now querying tmdb (which returns XML). I can then parse the XML response but the code is twice as long. Oh well.
Reply
#9
Oh, and one more thing ... I forgot to tell you ... now am am stuck with a very complicated problem. I doubt anyone will understand this rocket science but, in IDLE, the "print('text')" command works just fine. In xmbc, it doesn't.

Unbelievable. Is there any aspect of python which doesn't change with each release?
Reply
#10
jaja714 Wrote:Oh, and one more thing ... I forgot to tell you ... now am am stuck with a very complicated problem. I doubt anyone will understand this rocket science but, in IDLE, the "print('text')" command works just fine. In XBMC, it doesn't.

Unbelievable. Is there any aspect of python which doesn't change with each release?

The worst is over, it should be a lot more stable in eden than after hopefully.
print should work and have always, it will not print to console though.
If you have problems please read this before posting

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.

Image

"Well Im gonna download the code and look at it a bit but I'm certainly not a really good C/C++ programer but I'd help as much as I can, I mostly write in C#."
Reply

Logout Mark Read Team Forum Stats Members Help
json0