-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathmain.py
More file actions
227 lines (199 loc) · 8.79 KB
/
main.py
File metadata and controls
227 lines (199 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Copyright (C) 2023, Roman V. M.
#
# 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 <https://www.gnu.org/licenses/>.
"""
Example video plugin that is compatible with Kodi 20.x "Nexus" and above
"""
import json
import sys
from pathlib import Path
from urllib.parse import urlencode, parse_qsl
import xbmcgui
import xbmcplugin
from xbmcaddon import Addon
from xbmcvfs import translatePath
# Get the plugin url in plugin:// notation.
URL = sys.argv[0]
# Get a plugin handle as an integer number.
HANDLE = int(sys.argv[1])
# Get the addon base path. Here we use pathlib.Path for convenient path handling
ADDON_PATH = Path(translatePath(Addon().getAddonInfo('path')))
ICONS_DIR = ADDON_PATH / 'resources' / 'images' / 'icons'
FANART_DIR = ADDON_PATH / 'resources' / 'images' / 'fanart'
# Public domain movies are from https://publicdomainmovie.net
# Here we use a hardcoded list of movies from a JSON file simply for demonstrating purposes
# In a "real life" plugin you will need to get info and links to video files/streams
# from some website or online service.
MOVIES_INFO_PATH = ADDON_PATH / 'movies.json'
def get_url(**kwargs):
"""
Create a URL for calling the plugin recursively from the given set of keyword arguments.
:param kwargs: "argument=value" pairs
:return: plugin call URL
:rtype: str
"""
return f'{URL}?{urlencode(kwargs)}'
def get_genres():
"""
Get the list of video genres
Here you can insert some code that retrieves
the list of video sections (in this case movie genres) from some site or API.
:return: The list of video genres
:rtype: list
"""
with MOVIES_INFO_PATH.open('r', encoding='utf-8') as fo:
return json.load(fo)
def get_videos(genre_index):
"""
Get the list of videofiles/streams.
Here you can insert some code that retrieves
the list of video streams in the given section from some site or API.
:param genre_index: genre index
:type genre_index: int
:return: the list of videos in the category
:rtype: list
"""
return get_genres()[genre_index]
def list_genres():
"""
Create the list of movie genres in the Kodi interface.
"""
# Set plugin category. It is displayed in some skins as the name
# of the current section.
xbmcplugin.setPluginCategory(HANDLE, 'Public Domain Movies')
# Set plugin content. It allows Kodi to select appropriate views
# for this type of content.
xbmcplugin.setContent(HANDLE, 'movies')
# Get movie genres
genres = get_genres()
# Iterate through genres
for index, genre_info in enumerate(genres):
# Create a list item with a text label.
list_item = xbmcgui.ListItem(label=genre_info['genre'])
# Set images for the list item.
# Convert Path objects to str because Kodi API accepts only str.
list_item.setArt({
'icon': str(ICONS_DIR / genre_info['icon']),
'fanart': str(FANART_DIR / genre_info['fanart']),
})
# Set additional info for the list item using its InfoTag.
# InfoTag allows to set various information for an item.
# For available properties and methods see the following link:
# https://codedocs.xyz/xbmc/xbmc/classXBMCAddon_1_1xbmc_1_1InfoTagVideo.html
# 'mediatype' is needed for a skin to display info for this ListItem correctly.
info_tag = list_item.getVideoInfoTag()
info_tag.setMediaType('video')
info_tag.setTitle(genre_info['genre'])
info_tag.setGenres([genre_info['genre']])
# Create a URL for a plugin recursive call.
# Example: plugin://plugin.video.example/?action=listing&genre_index=0
url = get_url(action='listing', genre_index=index)
# is_folder = True means that this item opens a sub-list of lower level items.
is_folder = True
# Add our item to the Kodi virtual folder listing.
xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)
# Add sort methods for the virtual folder items
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
# Finish creating a virtual folder.
xbmcplugin.endOfDirectory(HANDLE)
def list_videos(genre_index):
"""
Create the list of playable videos in the Kodi interface.
:param genre_index: the index of genre in the list of movie genres
:type genre_index: int
"""
genre_info = get_videos(genre_index)
# Set plugin category. It is displayed in some skins as the name
# of the current section.
xbmcplugin.setPluginCategory(HANDLE, genre_info['genre'])
# Set plugin content. It allows Kodi to select appropriate views
# for this type of content.
xbmcplugin.setContent(HANDLE, 'movies')
# Get the list of videos in the category.
videos = genre_info['movies']
# Iterate through videos.
for video in videos:
# Create a list item with a text label
list_item = xbmcgui.ListItem(label=video['title'])
# Set graphics (thumbnail, fanart, banner, poster, landscape etc.) for the list item.
# Here we use only poster for simplicity's sake.
# In a real-life plugin you may need to set multiple image types.
list_item.setArt({'poster': video['poster']})
# Set additional info for the list item via InfoTag.
# 'mediatype' is needed for skin to display info for this ListItem correctly.
info_tag = list_item.getVideoInfoTag()
info_tag.setMediaType('movie')
info_tag.setTitle(video['title'])
info_tag.setGenres([genre_info['genre']])
info_tag.setPlot(video['plot'])
info_tag.setYear(video['year'])
# Set 'IsPlayable' property to 'true'.
# This is mandatory for playable items!
list_item.setProperty('IsPlayable', 'true')
# Create a URL for a plugin recursive call.
# Example: plugin://plugin.video.example/?action=play&video=https%3A%2F%2Fia600702.us.archive.org%2F3%2Fitems%2Firon_mask%2Firon_mask_512kb.mp4
url = get_url(action='play', video=video['url'])
# Add the list item to a virtual Kodi folder.
# is_folder = False means that this item won't open any sub-list.
is_folder = False
# Add our item to the Kodi virtual folder listing.
xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)
# Add sort methods for the virtual folder items
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_VIDEO_YEAR)
# Finish creating a virtual folder.
xbmcplugin.endOfDirectory(HANDLE)
def play_video(path):
"""
Play a video by the provided path.
:param path: Fully-qualified video URL
:type path: str
"""
# Create a playable item with a path to play.
# offscreen=True means that the list item is not meant for displaying,
# only to pass info to the Kodi player
play_item = xbmcgui.ListItem(offscreen=True)
play_item.setPath(path)
# Pass the item to the Kodi player.
xbmcplugin.setResolvedUrl(HANDLE, True, listitem=play_item)
def router(paramstring):
"""
Router function that calls other functions
depending on the provided paramstring
:param paramstring: URL encoded plugin paramstring
:type paramstring: str
"""
# Parse a URL-encoded paramstring to the dictionary of
# {<parameter>: <value>} elements
params = dict(parse_qsl(paramstring))
# Check the parameters passed to the plugin
if not params:
# If the plugin is called from Kodi UI without any parameters,
# display the list of video categories
list_genres()
elif params['action'] == 'listing':
# Display the list of videos in a provided category.
list_videos(int(params['genre_index']))
elif params['action'] == 'play':
# Play a video from a provided URL.
play_video(params['video'])
else:
# If the provided paramstring does not contain a supported action
# we raise an exception. This helps to catch coding errors,
# e.g. typos in action names.
raise ValueError(f'Invalid paramstring: {paramstring}!')
if __name__ == '__main__':
# Call the router function and pass the plugin call parameters to it.
# We use string slicing to trim the leading '?' from the plugin call paramstring
router(sys.argv[2][1:])