jmooremcc

plugin.video.example main.py

Nov 24th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.36 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. # Module: default
  3. # Author: Roman V. M.
  4. # Created on: 28.11.2014
  5. # License: GPL v.3 https://www.gnu.org/copyleft/gpl.html
  6.  
  7. import sys
  8. from urllib.parse import urlencode, quote_plus, parse_qsl
  9. import xbmcgui
  10. import xbmcplugin
  11. import xbmc
  12.  
  13. # Get the plugin url in plugin:// notation.
  14. _url = sys.argv[0]
  15. # Get the plugin handle as an integer number.
  16. _handle = int(sys.argv[1])
  17.  
  18. # Free sample videos are provided by www.vidsplay.com
  19. # Here we use a fixed set of properties simply for demonstrating purposes
  20. # In a "real life" plugin you will need to get info and links to video files/streams
  21. # from some web-site or online service.
  22. VIDEOS = {'Animals': [{'name': 'Crab',
  23. 'thumb': 'http://www.vidsplay.com/wp-content/uploads/2017/04/crab-screenshot.jpg',
  24. 'video': 'http://www.vidsplay.com/wp-content/uploads/2017/04/crab.mp4',
  25. 'genre': 'Animals'},
  26. {'name': 'Alligator',
  27. 'thumb': 'http://www.vidsplay.com/wp-content/uploads/2017/04/alligator-screenshot.jpg',
  28. 'video': 'http://www.vidsplay.com/wp-content/uploads/2017/04/alligator.mp4',
  29. 'genre': 'Animals'},
  30. {'name': 'Turtle',
  31. 'thumb': 'http://www.vidsplay.com/wp-content/uploads/2017/04/turtle-screenshot.jpg',
  32. 'video': 'http://www.vidsplay.com/wp-content/uploads/2017/04/turtle.mp4',
  33. 'genre': 'Animals'}
  34. ],
  35. 'Cars': [{'name': 'Postal Truck',
  36. 'thumb': 'http://www.vidsplay.com/wp-content/uploads/2017/05/us_postal-screenshot.jpg',
  37. 'video': 'http://www.vidsplay.com/wp-content/uploads/2017/05/us_postal.mp4',
  38. 'genre': 'Cars'},
  39. {'name': 'Traffic',
  40. 'thumb': 'http://www.vidsplay.com/wp-content/uploads/2017/05/traffic1-screenshot.jpg',
  41. 'video': 'http://www.vidsplay.com/wp-content/uploads/2017/05/traffic1.mp4',
  42. 'genre': 'Cars'},
  43. {'name': 'Traffic Arrows',
  44. 'thumb': 'http://www.vidsplay.com/wp-content/uploads/2017/05/traffic_arrows-screenshot.jpg',
  45. 'video': 'http://www.vidsplay.com/wp-content/uploads/2017/05/traffic_arrows.mp4',
  46. 'genre': 'Cars'}
  47. ],
  48. 'Food': [{'name': 'Chicken',
  49. 'thumb': 'http://www.vidsplay.com/wp-content/uploads/2017/05/bbq_chicken-screenshot.jpg',
  50. 'video': 'http://www.vidsplay.com/wp-content/uploads/2017/05/bbqchicken.mp4',
  51. 'genre': 'Food'},
  52. {'name': 'Hamburger',
  53. 'thumb': 'http://www.vidsplay.com/wp-content/uploads/2017/05/hamburger-screenshot.jpg',
  54. 'video': 'http://www.vidsplay.com/wp-content/uploads/2017/05/hamburger.mp4',
  55. 'genre': 'Food'},
  56. {'name': 'Pizza',
  57. 'thumb': 'http://www.vidsplay.com/wp-content/uploads/2017/05/pizza-screenshot.jpg',
  58. 'video': 'http://www.vidsplay.com/wp-content/uploads/2017/05/pizza.mp4',
  59. 'genre': 'Food'}
  60. ]}
  61.  
  62.  
  63. def get_url(**kwargs):
  64. """
  65. Create a URL for calling the plugin recursively from the given set of keyword arguments.
  66.  
  67. :param kwargs: "argument=value" pairs
  68. :type kwargs: dict
  69. :return: plugin call URL
  70. :rtype: str
  71. """
  72. return '{0}?{1}'.format(_url, urlencode(kwargs))
  73.  
  74.  
  75. def get_categories():
  76. """
  77. Get the list of video categories.
  78.  
  79. Here you can insert some parsing code that retrieves
  80. the list of video categories (e.g. 'Movies', 'TV-shows', 'Documentaries' etc.)
  81. from some site or server.
  82.  
  83. .. note:: Consider using `generator functions <https://wiki.python.org/moin/Generators>`_
  84. instead of returning lists.
  85.  
  86. :return: The list of video categories
  87. :rtype: types.GeneratorType
  88. """
  89. return VIDEOS.keys()
  90.  
  91.  
  92. def get_videos(category):
  93. """
  94. Get the list of videofiles/streams.
  95.  
  96. Here you can insert some parsing code that retrieves
  97. the list of video streams in the given category from some site or server.
  98.  
  99. .. note:: Consider using `generators functions <https://wiki.python.org/moin/Generators>`_
  100. instead of returning lists.
  101.  
  102. :param category: Category name
  103. :type category: str
  104. :return: the list of videos in the category
  105. :rtype: list
  106. """
  107. return VIDEOS[category]
  108.  
  109.  
  110. def list_categories():
  111. """
  112. Create the list of video categories in the Kodi interface.
  113. """
  114. # Set plugin category. It is displayed in some skins as the name
  115. # of the current section.
  116. xbmcplugin.setPluginCategory(_handle, 'My Video Collection')
  117. # Set plugin content. It allows Kodi to select appropriate views
  118. # for this type of content.
  119. xbmcplugin.setContent(_handle, 'videos')
  120. # Get video categories
  121. categories = get_categories()
  122. # Iterate through categories
  123. for category in categories:
  124. # Create a list item with a text label and a thumbnail image.
  125. list_item = xbmcgui.ListItem(label=category)
  126. # Set graphics (thumbnail, fanart, banner, poster, landscape etc.) for the list item.
  127. # Here we use the same image for all items for simplicity's sake.
  128. # In a real-life plugin you need to set each image accordingly.
  129. list_item.setArt({'thumb': VIDEOS[category][0]['thumb'],
  130. 'icon': VIDEOS[category][0]['thumb'],
  131. 'fanart': VIDEOS[category][0]['thumb']})
  132. # Set additional info for the list item.
  133. # Here we use a category name for both properties for for simplicity's sake.
  134. # setInfo allows to set various information for an item.
  135. # For available properties see the following link:
  136. # https://codedocs.xyz/xbmc/xbmc/group__python__xbmcgui__listitem.html#ga0b71166869bda87ad744942888fb5f14
  137. # 'mediatype' is needed for a skin to display info for this ListItem correctly.
  138. list_item.setInfo('video', {'title': category,
  139. 'genre': category,
  140. 'mediatype': 'video'})
  141. # Create a URL for a plugin recursive call.
  142. # Example: plugin://plugin.video.example/?action=listing&category=Animals
  143. url = get_url(action='listing', category=category)
  144. # is_folder = True means that this item opens a sub-list of lower level items.
  145. is_folder = True
  146. # Add our item to the Kodi virtual folder listing.
  147. xbmcplugin.addDirectoryItem(_handle, url, list_item, is_folder)
  148. # Add a sort method for the virtual folder items (alphabetically, ignore articles)
  149. xbmcplugin.addSortMethod(_handle, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
  150. # Finish creating a virtual folder.
  151. xbmcplugin.endOfDirectory(_handle)
  152.  
  153.  
  154. def list_videos(category):
  155. """
  156. Create the list of playable videos in the Kodi interface.
  157.  
  158. :param category: Category name
  159. :type category: str
  160. """
  161. # Set plugin category. It is displayed in some skins as the name
  162. # of the current section.
  163. xbmcplugin.setPluginCategory(_handle, category)
  164. # Set plugin content. It allows Kodi to select appropriate views
  165. # for this type of content.
  166. xbmcplugin.setContent(_handle, 'videos')
  167. # Get the list of videos in the category.
  168. videos = get_videos(category)
  169. # Iterate through videos.
  170. # import web_pdb; web_pdb.set_trace()
  171. for video in videos:
  172. # Create a list item with a text label and a thumbnail image.
  173. list_item = xbmcgui.ListItem(label=video['name'])
  174. # Set additional info for the list item.
  175. # 'mediatype' is needed for skin to display info for this ListItem correctly.
  176. list_item.setInfo('video', {'title': video['name'],
  177. 'genre': video['genre'],
  178. 'mediatype': 'video'})
  179. # Set graphics (thumbnail, fanart, banner, poster, landscape etc.) for the list item.
  180. # Here we use the same image for all items for simplicity's sake.
  181. # In a real-life plugin you need to set each image accordingly.
  182. list_item.setArt({'thumb': video['thumb'], 'icon': video['thumb'], 'fanart': video['thumb']})
  183. # Set 'IsPlayable' property to 'true'.
  184. # This is mandatory for playable items!
  185. list_item.setProperty('IsPlayable', 'true')
  186. # Create a URL for a plugin recursive call.
  187. # Example: plugin://plugin.video.example/?action=play&video=http://www.vidsplay.com/wp-content/uploads/2017/04/crab.mp4
  188. url = get_url(action='play', video=video['video'])
  189. # Add the list item to a virtual Kodi folder.
  190. # is_folder = False means that this item won't open any sub-list.
  191. is_folder = False
  192.  
  193. # add context menu to edit video listItem
  194. xbmc.log("***Adding Context Menu Item")
  195. cmd = 'XBMC.RunPlugin({})'.format(get_url(action='edit',title=video['name']))
  196. menutitle = "Edit {} Video".format(video['name'])
  197. xbmc.log("menu=[({},{})]".format(menutitle, cmd))
  198. menu = []
  199. menu.append((menutitle, cmd),)
  200. list_item.addContextMenuItems(menu)
  201.  
  202. # Add our item to the Kodi virtual folder listing.
  203. xbmcplugin.addDirectoryItem(_handle, url, list_item, is_folder)
  204. # Add a sort method for the virtual folder items (alphabetically, ignore articles)
  205. xbmcplugin.addSortMethod(_handle, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
  206. # Finish creating a virtual folder.
  207. xbmcplugin.endOfDirectory(_handle)
  208.  
  209.  
  210. def play_video(path):
  211. """
  212. Play a video by the provided path.
  213.  
  214. :param path: Fully-qualified video URL
  215. :type path: str
  216. """
  217. # Create a playable item with a path to play.
  218. play_item = xbmcgui.ListItem(path=path)
  219. # Pass the item to the Kodi player.
  220. xbmcplugin.setResolvedUrl(_handle, True, listitem=play_item)
  221.  
  222. def edit_videotitle(videoname):
  223. #fake edit video title
  224. keyb = xbmc.Keyboard(videoname,"Edit Video Title")
  225. keyb.doModal()
  226. if (keyb.isConfirmed()):
  227. return keyb.getText()
  228.  
  229. def router(paramstring):
  230. """
  231. Router function that calls other functions
  232. depending on the provided paramstring
  233.  
  234. :param paramstring: URL encoded plugin paramstring
  235. :type paramstring: str
  236. """
  237. # Parse a URL-encoded paramstring to the dictionary of
  238. # {<parameter>: <value>} elements
  239. params = dict(parse_qsl(paramstring))
  240. xbmc.log('***params={}'.format(params))
  241. # Check the parameters passed to the plugin
  242. if params:
  243. if params['action'] == 'listing':
  244. # Display the list of videos in a provided category.
  245. list_videos(params['category'])
  246. elif params['action'] == 'play':
  247. # Play a video from a provided URL.
  248. play_video(params['video'])
  249. elif params['action'] == 'edit':
  250. # edit video title
  251. title=edit_videotitle(params['title'])
  252. if title is not None:
  253. win = xbmcgui.Window(xbmcgui.getCurrentWindowId())
  254. # curctl = win.getFocusId()
  255. # import web_pdb; web_pdb.set_trace()
  256. curctl = win.getFocus()
  257. print("Type of curctl is " + type(curctl).__name__)
  258.  
  259. # cursel = curctl.getSelectedItem()
  260. print("Type of cursel is " + type(cursel).__name__)
  261.  
  262. # label = cursel.getLabel()
  263. # xbmc.log("current item:{}".format(liz.getLabel()))
  264. else:
  265. # If the provided paramstring does not contain a supported action
  266. # we raise an exception. This helps to catch coding errors,
  267. # e.g. typos in action names.
  268. raise ValueError('Invalid paramstring: {0}!'.format(paramstring))
  269. else:
  270. # If the plugin is called from Kodi UI without any parameters,
  271. # display the list of video categories
  272. list_categories()
  273.  
  274.  
  275. if __name__ == '__main__':
  276. # Call the router function and pass the plugin call parameters to it.
  277. # We use string slicing to trim the leading '?' from the plugin call paramstring
  278. router(sys.argv[2][1:])
  279.  
Advertisement
Add Comment
Please, Sign In to add comment