Updated code using best practices, added setInfo for list items

This commit is contained in:
Roman Miroshnychenko (Work) 2015-08-31 14:59:06 +03:00
parent ff1b88cf39
commit a433b569c4
2 changed files with 49 additions and 15 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<addon id="plugin.video.example" <addon id="plugin.video.example"
version="1.0.0" version="1.1.0"
name="Example Kodi Video Plugin" name="Example Kodi Video Plugin"
provider-name="Roman_V_M"> provider-name="Roman_V_M">
<requires> <requires>

62
main.py
View File

@ -20,33 +20,42 @@ __handle__ = int(sys.argv[1])
# from some web-site or online service. # from some web-site or online service.
VIDEOS = {'Animals': [{'name': 'Crab', VIDEOS = {'Animals': [{'name': 'Crab',
'thumb': 'http://www.vidsplay.com/vids/crab.jpg', 'thumb': 'http://www.vidsplay.com/vids/crab.jpg',
'video': 'http://www.vidsplay.com/vids/crab.mp4'}, 'video': 'http://www.vidsplay.com/vids/crab.mp4',
'genre': 'Animals'},
{'name': 'Alligator', {'name': 'Alligator',
'thumb': 'http://www.vidsplay.com/vids/alligator.jpg', 'thumb': 'http://www.vidsplay.com/vids/alligator.jpg',
'video': 'http://www.vidsplay.com/vids/alligator.mp4'}, 'video': 'http://www.vidsplay.com/vids/alligator.mp4',
'genre': 'Animals'},
{'name': 'Turtle', {'name': 'Turtle',
'thumb': 'http://www.vidsplay.com/vids/turtle.jpg', 'thumb': 'http://www.vidsplay.com/vids/turtle.jpg',
'video': 'http://www.vidsplay.com/vids/turtle.mp4'} 'video': 'http://www.vidsplay.com/vids/turtle.mp4',
'genre': 'Animals'}
], ],
'Cars': [{'name': 'Postal Truck', 'Cars': [{'name': 'Postal Truck',
'thumb': 'http://www.vidsplay.com/vids/us_postal.jpg', 'thumb': 'http://www.vidsplay.com/vids/us_postal.jpg',
'video': 'http://www.vidsplay.com/vids/us_postal.mp4'}, 'video': 'http://www.vidsplay.com/vids/us_postal.mp4',
'genre': 'Cars'},
{'name': 'Traffic', {'name': 'Traffic',
'thumb': 'http://www.vidsplay.com/vids/traffic1.jpg', 'thumb': 'http://www.vidsplay.com/vids/traffic1.jpg',
'video': 'http://www.vidsplay.com/vids/traffic1.avi'}, 'video': 'http://www.vidsplay.com/vids/traffic1.avi',
'genre': 'Cars'},
{'name': 'Traffic Arrows', {'name': 'Traffic Arrows',
'thumb': 'http://www.vidsplay.com/vids/traffic_arrows.jpg', 'thumb': 'http://www.vidsplay.com/vids/traffic_arrows.jpg',
'video': 'http://www.vidsplay.com/vids/traffic_arrows.mp4'} 'video': 'http://www.vidsplay.com/vids/traffic_arrows.mp4',
'genre': 'Cars'}
], ],
'Food': [{'name': 'Chicken', 'Food': [{'name': 'Chicken',
'thumb': 'http://www.vidsplay.com/vids/chicken.jpg', 'thumb': 'http://www.vidsplay.com/vids/chicken.jpg',
'video': 'http://www.vidsplay.com/vids/bbqchicken.mp4'}, 'video': 'http://www.vidsplay.com/vids/bbqchicken.mp4',
'genre': 'Food'},
{'name': 'Hamburger', {'name': 'Hamburger',
'thumb': 'http://www.vidsplay.com/vids/hamburger.jpg', 'thumb': 'http://www.vidsplay.com/vids/hamburger.jpg',
'video': 'http://www.vidsplay.com/vids/hamburger.mp4'}, 'video': 'http://www.vidsplay.com/vids/hamburger.mp4',
'genre': 'Food'},
{'name': 'Pizza', {'name': 'Pizza',
'thumb': 'http://www.vidsplay.com/vids/pizza.jpg', 'thumb': 'http://www.vidsplay.com/vids/pizza.jpg',
'video': 'http://www.vidsplay.com/vids/pizza.mp4'} 'video': 'http://www.vidsplay.com/vids/pizza.mp4',
'genre': 'Food'}
]} ]}
@ -79,6 +88,8 @@ def list_categories():
""" """
# Get video categories # Get video categories
categories = get_categories() categories = get_categories()
# Create a list for our items.
listing = []
# Iterate through categories # Iterate through categories
for category in categories: for category in categories:
# Create a list item with a text label and a thumbnail image. # Create a list item with a text label and a thumbnail image.
@ -86,12 +97,23 @@ def list_categories():
# Set a fanart image for the list item. # Set a fanart image for the list item.
# Here we use the same image as the thumbnail for simplicity's sake. # Here we use the same image as the thumbnail for simplicity's sake.
list_item.setProperty('fanart_image', VIDEOS[category][0]['thumb']) list_item.setProperty('fanart_image', VIDEOS[category][0]['thumb'])
# Set additional info for the list item.
# Here we use a category name for both properties for for simplicity's sake.
# setInfo allows to set various information for an item.
# For available properties see the following link:
# http://mirrors.xbmc.org/docs/python-docs/15.x-isengard/xbmcgui.html#ListItem-setInfo
list_item.setInfo('video', {'title': category, 'genre': category})
# Create a URL for the plugin recursive callback. # Create a URL for the plugin recursive callback.
# Example: plugin://plugin.video.example/?action=listing&category=Animals # Example: plugin://plugin.video.example/?action=listing&category=Animals
url = '{0}?action=listing&category={1}'.format(__url__, category) url = '{0}?action=listing&category={1}'.format(__url__, category)
# Add the list item to a virtual Kodi folder. # is_folder = True means that this item opens a sub-list of lower level items.
# isFolder=True means that this item opens a sub-list of lower level items. is_folder = True
xbmcplugin.addDirectoryItem(__handle__, url, list_item, isFolder=True) # Add our item to the listing as a 3-element tuple.
listing.append((url, list_item, is_folder))
# Add our listing to Kodi.
# Large lists and/or slower systems benefit from adding all items at once via addDirectoryItems
# instead of adding one by ove via addDirectoryItem.
xbmcplugin.addDirectoryItems(__handle__, listing, len(listing))
# Add a sort method for the virtual folder items (alphabetically, ignore articles) # Add a sort method for the virtual folder items (alphabetically, ignore articles)
xbmcplugin.addSortMethod(__handle__, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE) xbmcplugin.addSortMethod(__handle__, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
# Finish creating a virtual folder. # Finish creating a virtual folder.
@ -106,6 +128,8 @@ def list_videos(category):
""" """
# Get the list of videos in the category. # Get the list of videos in the category.
videos = get_videos(category) videos = get_videos(category)
# Create a list for our items.
listing = []
# Iterate through videos. # Iterate through videos.
for video in videos: for video in videos:
# Create a list item with a text label and a thumbnail image. # Create a list item with a text label and a thumbnail image.
@ -113,6 +137,8 @@ def list_videos(category):
# Set a fanart image for the list item. # Set a fanart image for the list item.
# Here we use the same image as the thumbnail for simplicity's sake. # Here we use the same image as the thumbnail for simplicity's sake.
list_item.setProperty('fanart_image', video['thumb']) list_item.setProperty('fanart_image', video['thumb'])
# Set additional info for the list item.
list_item.setInfo('video', {'title': video['name'], 'genre': video['genre']})
# Set 'IsPlayable' property to 'true'. # Set 'IsPlayable' property to 'true'.
# This is mandatory for playable items! # This is mandatory for playable items!
list_item.setProperty('IsPlayable', 'true') list_item.setProperty('IsPlayable', 'true')
@ -120,8 +146,16 @@ def list_videos(category):
# Example: plugin://plugin.video.example/?action=play&video=http://www.vidsplay.com/vids/crab.mp4 # Example: plugin://plugin.video.example/?action=play&video=http://www.vidsplay.com/vids/crab.mp4
url = '{0}?action=play&video={1}'.format(__url__, video['video']) url = '{0}?action=play&video={1}'.format(__url__, video['video'])
# Add the list item to a virtual Kodi folder. # Add the list item to a virtual Kodi folder.
# isFolder=False means that this item won't open any sub-list. # is_folder = False means that this item won't open any sub-list.
xbmcplugin.addDirectoryItem(__handle__, url, list_item, isFolder=False) is_folder = False
# Add our item to the listing as a 3-element tuple.
listing.append((url, list_item, is_folder))
# Add our listing to Kodi.
# Large lists and/or slower systems benefit from adding all items at once via addDirectoryItems
# instead of adding one by ove via addDirectoryItem.
xbmcplugin.addDirectoryItems(__handle__, listing, len(listing))
# Add a sort method for the virtual folder items (alphabetically, ignore articles)
xbmcplugin.addSortMethod(__handle__, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
# Finish creating a virtual folder. # Finish creating a virtual folder.
xbmcplugin.endOfDirectory(__handle__) xbmcplugin.endOfDirectory(__handle__)