python help video addon: how to add items returned from a web url into a subdirectory
#1
hi i am trying to learn python/xbmc programming so i'm trying to modify this onion news addon i made (located here: http://hyper.homeftp.net/xbmc/ ) so it scrapes academic earth.

Code:
xbmc@XBMCLive:~$ cat /live/image/dotXBMC/addons/schneidz-ae/default.py
'''This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import re
import urllib2
import xbmcgui, xbmcplugin

plugin_handle = int(sys.argv[1])

def parse_subjects(url, infolabels, img=''):
    listitem = xbmcgui.ListItem(infolabels['title'], iconImage=img,
                                thumbnailImage=img)
    listitem.setInfo('video', infolabels)
    listitem.setProperty('IsPlayable', 'false')
    xbmcplugin.addDirectoryItem(plugin_handle, url, listitem, isFolder=True)

html = urllib2.urlopen('http://www.academicearth.org/subjects/engineering/page:1/sort:title/direction:asc/show:500').read()
for v in re.finditer('<a   href="(\/courses\/.+?)"><h3>(.+?)<\/h3><\/a>', html):
    course, title = v.groups()
    if course:
       s1 = "http://www.academicearth.org" + course + "/page:1/show:500"
    if title:
       s2 = title
#    print "s1 = ", s1, " s2 = ", s2 #, " date = ", date , " y = ", y, " m = ", m, " d = ", d
    parse_subjects('%s' % (s1), {'title': '%s' % (s2), 'aired': '11-11-2010'}, 'http://o.onionstatic.com/img/onn/podcast_300300.jpg')
print " sys.argv[0] = " + sys.argv[0]
print " sys.argv[1] = " + sys.argv[1]
print " sys.argv[2] = " + sys.argv[2]

xbmcplugin.endOfDirectory(plugin_handle)
this code provides a list of web url's. when i clik on one it only gives a '..' in the subdirectory.
i think i want to call urlopen().read, re.finditer(), and addDirectoryItem() for all the courses returned on the selected page but i dont understand how to call those functions in this code.

can someone please provide some pointers ?
Reply
#2
hi, i think i would be able to continue if i knew how to code the last debugging line below:
Code:
import re
import urllib2
import xbmcgui, xbmcplugin

plugin_handle = int(sys.argv[1])

def parse_subjects(url, infolabels, img=''):
    listitem = xbmcgui.ListItem(infolabels['title'], iconImage=img,
                                thumbnailImage=img)
    listitem.setInfo('video', infolabels)
    listitem.setProperty('IsPlayable', 'false')
    xbmcplugin.addDirectoryItem(plugin_handle, url, listitem, isFolder=True)

html = urllib2.urlopen('http://www.academicearth.org/subjects/engineering/page:1/sort:title/direction:asc/show:500').read()
for v in re.finditer('<a   href="(\/courses\/.+?)"><h3>(.+?)<\/h3><\/a>', html):
    course, title = v.groups()
    if course:
       s1 = "http://www.academicearth.org" + course + "/page:1/show:500"
    if title:
       s2 = title
#    print "s1 = ", s1, " s2 = ", s2 #, " date = ", date , " y = ", y, " m = ", m, " d = ", d
    parse_subjects('%s' % (s1), {'title': '%s' % (s2), 'aired': '11-11-2010'}, 'http://o.onionstatic.com/img/onn/podcast_300300.jpg')


xbmcplugin.endOfDirectory(plugin_handle)
print " debug-log: this is the selected item = ", item_name
Reply
#3
for some reason it seems like this addon is only running once (i only see 'NOTICE: -->Python Interpreter Initialized' once while starting the addon; it doesnt appear again after selecting a listitem item).

can anyone help me figure out what i need to do in order to scrape the url that the user selects ?:
Code:
11:37:04 T:2978405232 M:1587331072  NOTICE: -->Python Interpreter Initialized<--
11:37:15 T:2978405232 M:1587060736  NOTICE:  sys.argv[0] = plugin://plugin.video.schneidz.ae/
11:37:15 T:2978405232 M:1587060736  NOTICE:  sys.argv[1] = 0
11:37:15 T:2978405232 M:1587060736  NOTICE:  sys.argv[2] =
11:37:15 T:3062085520 M:1587167232 WARNING: GetLabel - Unknown nodetype requested 6
nothing is appended to the log when i select one of the listitem's on the screen with the remote.
Reply
#4
have a look at my template for noobies


https://dl.dropbox.com/u/35759481/plugin...plugin.zip
Reply
#5
thanks mikey, i am able to follow your example code -- the comments are extremely helpful.

i am still wondering why my example in the first post does not initialize the python interpreter after a listitem is selected. my understanding is that after each selection, the python interpreter should restart.

thanks,
Reply
#6
(2012-10-18, 00:29)schneidz Wrote: thanks mikey, i am able to follow your example code -- the comments are extremely helpful.

i am still wondering why my example in the first post does not initialize the python interpreter after a listitem is selected. my understanding is that after each selection, the python interpreter should restart.

thanks,

You're not adding the complete url. The plugin url needs to be in the url. Otherwise XBMC has no way of knowing what to do.
So you could add something like:
Code:
pluginUrl = sys.argv[0]
and then change the list item url to something like
Code:
s1 = pluginUrl + '?browse=' + "http://www.academicearth.org" + course + "/page:1/show:500"

Then XBMC will know where to go. But you'll still need to add code to handle what to do with the ?browse= part.
Reply
#7
thanks atv-user, that solves my mystery eventhough mikey's code is more elegant. i'll also play around with your corrections to see if i can get it to bahave as i expected.
Reply
#8
Or you can have a look to jbel's nice add-on framework xbmcswift.
My GitHub. My Add-ons:
Image
Reply
#9
thanks again mikey1234. i finished this academic earth addon. it is kinda' buggy so i am open to suggestions. the source is located here:
http://hyper.homeftp.net/xbmc/
Reply

Logout Mark Read Team Forum Stats Members Help
python help video addon: how to add items returned from a web url into a subdirectory0