|
|
| (18 intermediate revisions by the same user not shown) |
| Line 1: |
Line 1: |
| This is a staging area for some rewrites to the Python add-on documentation. Ok, let's go!
| |
|
| |
|
| = Python add-ons =
| |
|
| |
| == Before we get started ==
| |
| {{See also|Add-on structure}}
| |
|
| |
| The first step to making an add-on is to put the basic structure in place. Make sure your directory is properly named, e.g. <code>plugin.video.my-addon</code>, and that you have a properly-constructed <code>[[addon.xml]]</code> file. Once these are done, you can get started writing the actual code for your add-on!
| |
|
| |
| == Hello, World! ==
| |
|
| |
| The simplest form of Python add-on is one that gets run, adds some list items, and exits to let XBMC take over the navigation; people who have written server-side code for the web should be familiar with how this works. (More complex add-ons can process user input in real-time, but we'll discuss those later.) Let's start by looking at an extremely basic example script:
| |
|
| |
| <source lang="python">
| |
| import sys
| |
| import xbmcgui
| |
| import xbmcplugin
| |
|
| |
| addon_id = int(sys.argv[1])
| |
|
| |
| url = 'http://localhost/some_video.mkv'
| |
| li = xbmcgui.ListItem('My First Video!', iconImage='DefaultVideo.png')
| |
| li.setInfo(type='video')
| |
| xbmcplugin.addDirectoryItem(handle=addon_id, url=url, listitem=li)
| |
|
| |
| xbmcplugin.endOfDirectory(addon_id)
| |
| </source>
| |
|
| |
| While not particularly exciting, this shows the minimal amount of code you need to get an XBMC add-on up and running in Python. Most important is that we first create an <code>xbmcgui.ListItem</code> with the name and icon we want (<code>'DefaultVideo.png'</code> comes from the XBMC skin), set it to be a video item, and then add it to the directory. Once we're done adding items, we need to be sure to call <code>xbmcplugin.endOfDirectory</code> to let XBMC know we're done!
| |
|
| |
| == The entry point ==
| |
|
| |
| As we saw above, XBMC passes some arguments to us via <code>sys.argv</code>. This is important, since it's what will let us tailor the output on the add-on based on user input. Remember, much like a web site, each folder (or page) in an XBMC add-on is the result of a separate invocation of our script. The arguments available to us are:
| |
|
| |
| {| class="wikitable"
| |
| ! Index !! Description
| |
| |-
| |
| | 0 || The base URL of your add-on, e.g. <code>'plugin://plugin.video.myaddon/'</code>
| |
| |-
| |
| | 1 || The process handle for this add-on, as a numeric string
| |
| |-
| |
| | 2 || The query string passed to your add-on, e.g. <code>'?foo=bar&baz=quux'</code>
| |
| |}
| |
|
| |
| Of particular interest is <code>sys.argv[2]</code>, which is what we'll generally use to show different things in the add-on.
| |
|
| |
| == Navigating between pages ==
| |
|
| |
| <source lang="python">
| |
| import sys
| |
| import urllib
| |
| import urlparse
| |
| import xbmcgui
| |
| import xbmcplugin
| |
|
| |
| xbmcplugin.setContent(addon_id, 'movies')
| |
|
| |
| base_url = sys.argv[0]
| |
| addon_id = int(sys.argv[1])
| |
| args = urlpase.parse_qs(sys.argv[2])
| |
|
| |
| def build_url(query):
| |
| return base_url + '?' + urllib.encode(query)
| |
|
| |
| mode = args.get('mode', None)
| |
|
| |
| if mode is None:
| |
| url = build_url({'mode': 'folder', 'foldername': 'Folder One'})
| |
| li = xbmcgui.ListItem('Folder One', iconImage='DefaultFolder.png')
| |
| xbmcplugin.addDirectoryItem(handle=addon_id, url=url,
| |
| listitem=li, isFolder=True)
| |
|
| |
| url = build_url({'mode': 'folder', 'foldername': 'Folder Two'})
| |
| li = xbmcgui.ListItem('Folder Two', iconImage='DefaultFolder.png')
| |
| xbmcplugin.addDirectoryItem(handle=addon_id, url=url,
| |
| listitem=li, isFolder=True)
| |
|
| |
| xbmcplugin.endOfDirectory(addon_id)
| |
|
| |
| elif mode[0] == 'folder':
| |
| foldername = args['foldername'][0]
| |
| url = 'http://localhost/some_video.mkv'
| |
| li = xbmcgui.ListItem(foldername + ' Video', iconImage='DefaultVideo.png')
| |
| li.setInfo(type='video')
| |
| xbmcplugin.addDirectoryItem(handle=addon_id, url=url, listitem=li)
| |
| xbmcplugin.endOfDirectory(addon_id)
| |
| </source>
| |