initial commit
This commit is contained in:
112
addon.py
Normal file
112
addon.py
Normal file
@@ -0,0 +1,112 @@
|
||||
import sys
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
import xbmc
|
||||
import xbmcaddon
|
||||
import xbmcgui
|
||||
import xbmcplugin
|
||||
|
||||
ADDON = xbmcaddon.Addon()
|
||||
ADDON_ID = ADDON.getAddonInfo("id")
|
||||
ADDON_NAME = ADDON.getAddonInfo("name")
|
||||
HANDLE = int(sys.argv[1]) if len(sys.argv) > 1 else -1
|
||||
PARAMS_STR = sys.argv[2] if len(sys.argv) > 2 else ""
|
||||
|
||||
def run_trigger(n):
|
||||
if n == 0:
|
||||
return notify("Yay","One",success=True)
|
||||
elif n == 1:
|
||||
return notify("Yay","Two",success=True)
|
||||
else:
|
||||
return notify("Oh no","You fucked up",success=False)
|
||||
|
||||
|
||||
def get_trigger_config(n):
|
||||
if n == 0:
|
||||
return "One"
|
||||
elif n == 1:
|
||||
return "Two"
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def show_menu():
|
||||
"""
|
||||
Build a virtual directory listing with one item per configured trigger.
|
||||
Each item can be added to Favourites individually.
|
||||
"""
|
||||
xbmcplugin.setPluginCategory(HANDLE, ADDON_NAME)
|
||||
xbmcplugin.setContent(HANDLE, "files")
|
||||
|
||||
n = 0
|
||||
while True:
|
||||
cfg = get_trigger_config(n)
|
||||
|
||||
if cfg == None:
|
||||
break
|
||||
|
||||
run_url = f"plugin://{ADDON_ID}/?trigger={n}"
|
||||
label = cfg
|
||||
sublabel = "[not configured]"
|
||||
|
||||
li = xbmcgui.ListItem(label=label, label2=sublabel)
|
||||
li.setArt({"icon": "DefaultAddonProgram.png"})
|
||||
li.setInfo("video", {"title": label, "plot": sublabel})
|
||||
li.setProperty("IsPlayable", "false")
|
||||
|
||||
li.addContextMenuItems([
|
||||
(
|
||||
f"Add '{label}' to Favourites",
|
||||
f"RunScript(special://home/addons/{ADDON_ID}/addon.py,trigger={n})"
|
||||
)
|
||||
])
|
||||
|
||||
xbmcplugin.addDirectoryItem(
|
||||
handle=HANDLE,
|
||||
url=run_url,
|
||||
listitem=li,
|
||||
isFolder=False,
|
||||
)
|
||||
|
||||
n += 1
|
||||
|
||||
xbmcplugin.endOfDirectory(HANDLE)
|
||||
|
||||
def notify(title, message, success=True):
|
||||
"""Show a Kodi notification toast."""
|
||||
icon = xbmcgui.NOTIFICATION_INFO if success else xbmcgui.NOTIFICATION_ERROR
|
||||
xbmcgui.Dialog().notification(title, message, icon=icon, time=4000)
|
||||
|
||||
def get_params():
|
||||
"""Parse the query-string arguments Kodi passes as sys.argv[2]."""
|
||||
if PARAMS_STR.startswith("?"):
|
||||
return dict(urllib.parse.parse_qsl(PARAMS_STR[1:]))
|
||||
return {}
|
||||
|
||||
def main():
|
||||
params = get_params()
|
||||
|
||||
# Called with ?trigger=N (from the favorites menu)
|
||||
if "trigger" in params:
|
||||
try:
|
||||
n = int(params["trigger"])
|
||||
except ValueError:
|
||||
notify(ADDON_NAME, "Invalid trigger parameter", success=False)
|
||||
return
|
||||
if get_trigger_config(n) == None:
|
||||
notify(ADDON_NAME, f"Trigger {n} out of range", success=False)
|
||||
return
|
||||
run_trigger(n)
|
||||
return
|
||||
|
||||
# Called with ?action=settings
|
||||
if params.get("action") == "settings":
|
||||
ADDON.openSettings()
|
||||
return
|
||||
|
||||
# No param
|
||||
show_menu()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user