def redirect_search(self, search_term, extras): """ Checks if searh term returns valid results before adding to saved searches. Then directly farward the results to kodi. :param str search_term: The serch term used to search for results. :param dict extras: Extra parameters that will be farwarded on to the callback function. :return: List if valid search results """ self.category = search_term.title() callback_params = extras.copy() callback_params["search_query"] = search_term # We switch selector to redirected callback to allow next page to work properly route = callback_params.pop("route") dispatcher.selector = route # Fetch search results from callback func = dispatcher.get_route().function listitems = func(self, **callback_params) # Check that we have valid listitems valid_listitems = validate_listitems(listitems) # Add the search term to database and return the list of results if valid_listitems: if search_term not in self.search_db: # pragma: no branch self.search_db.append(search_term) self.search_db.flush() return valid_listitems else: # Return False to indicate failure return False
def next_page(cls, *args, **kwargs): """ Constructor for adding link to "Next Page" of content. By default the current running "callback" will be called with all of the parameters that are given here. You can specify which "callback" will be called by setting a keyword only argument called 'callback'. :param args: "Positional" arguments that will be passed to the callback. :param kwargs: "Keyword" arguments that will be passed to the callback. :example: >>> item = Listitem() >>> item.next_page(url="http://example.com/videos?page2") """ # Current running callback callback = kwargs.pop( "callback") if "callback" in kwargs else dispatcher.get_route( ).callback # Add support params to callback params kwargs[ "_updatelisting_"] = True if u"_nextpagecount_" in dispatcher.params else False kwargs["_title_"] = dispatcher.params.get(u"_title_", u"") kwargs["_nextpagecount_"] = dispatcher.params.get( u"_nextpagecount_", 1) + 1 # Create listitem instance item = cls() label = u"%s %i" % (Script.localize(NEXT_PAGE), kwargs["_nextpagecount_"]) item.info["plot"] = Script.localize(NEXT_PAGE_PLOT) item.label = bold(label) item.art.global_thumb("next.png") item.set_callback(callback, *args, **kwargs) return item
def search(cls, callback, *args, **kwargs): """ Constructor to add "saved search" support to add-on. This will first link to a "sub" folder that lists all saved "search terms". From here, "search terms" can be created or removed. When a selection is made, the "callback" function that was given will be executed with all parameters forwarded on. Except with one extra parameter, ``search_query``, which is the "search term" that was selected. :param Callback callback: Function that will be called when the "listitem" is activated. :param args: "Positional" arguments that will be passed to the callback. :param kwargs: "Keyword" arguments that will be passed to the callback. """ if hasattr(callback, "route"): route = callback.route elif isinstance(callback, CallbackRef): route = callback else: route = dispatcher.get_route(callback) kwargs["first_load"] = True kwargs["_route"] = route.path item = cls() item.label = bold(Script.localize(SEARCH)) item.art.global_thumb("search.png") item.info["plot"] = Script.localize(SEARCH_PLOT) item.set_callback(Route.ref("/codequick/search:saved_searches"), *args, **kwargs) return item
def next_page(cls, *args, **kwargs): """ Constructor for adding link to "Next Page" of content. The current running "callback" will be called with all of the parameters that are given here. You can also specify which "callback" will be called by setting a keywork only argument called 'callback'. :param args: "Positional" arguments that will be passed to the callback. :param kwargs: "Keyword" arguments that will be passed to the callback. :example: >>> item = Listitem() >>> item.next_page(url="http://example.com/videos?page2") """ # Current running callback callback = dispatcher.get_route().callback callback = kwargs.pop("callback", callback) # Add support params to callback params kwargs["_updatelisting_"] = True if u"_nextpagecount_" in dispatcher.params else False kwargs["_title_"] = dispatcher.params.get(u"_title_", u"") kwargs["_nextpagecount_"] = dispatcher.params.get(u"_nextpagecount_", 1) + 1 # Create listitem instance item = cls() label = u"%s %i" % (Script.localize(NEXT_PAGE), kwargs["_nextpagecount_"]) item.info["plot"] = Script.localize(NEXT_PAGE_PLOT) item.label = bold(label) item.art.global_thumb("next.png") item.set_callback(callback, *args, **kwargs) return item
def related(self, callback, *args, **kwargs): """ Convenient method to add a "Related Videos" context menu item. All this really does is to call "context.container" and sets "label" for you. :param callback: The function that will be called when menu item is activated. :param args: [opt] "Positional" arguments that will be passed to the callback. :param kwargs: [opt] "Keyword" arguments that will be passed to the callback. """ # Add '_updatelisting_ = True' to callback params if called from the same callback as is given here if callback.route == dispatcher.get_route(): kwargs["_updatelisting_"] = True related_videos_text = Script.localize(RELATED_VIDEOS) kwargs["_title_"] = related_videos_text self.container(callback, related_videos_text, *args, **kwargs)
def related(self, callback, *args, **kwargs): """ Convenient method to add a "Related Videos" context menu item. All this really does is to call "context.container" and sets "label" for you. :param callback: The function that will be called when menu item is activated. :param args: [opt] "Positional" arguments that will be passed to the callback. :param kwargs: [opt] "Keyword" arguments that will be passed to the callback. """ # Add '_updatelisting_ = True' to callback params if called from the same callback as is given here if callback.route == dispatcher.get_route(): kwargs["_updatelisting_"] = True related_videos_text = Script.localize(RELATED_VIDEOS) kwargs["_title_"] = related_videos_text self.container(callback, related_videos_text, *args, **kwargs)
def list_terms(plugin, searchdb, extras): """ List all saved searches. :param Route plugin: Tools related to Route callbacks. :param Search searchdb: Search DB :param dict extras: Extra parameters that will be farwarded on to the context.container. :returns: A generator of listitems. :rtype: :class:`types.GeneratorType` """ # Add listitem for adding new search terms search_item = Listitem() search_item.label = u"[B]%s[/B]" % plugin.localize(SEARCH) search_item.set_callback(saved_searches, search=True, **extras) search_item.art.global_thumb("search_new.png") yield search_item # Set the callback function to the route that was given callback_params = extras.copy() route = callback_params.pop("_route") callback = dispatcher.get_route(route).callback # Prefetch the localized string for the context menu lable str_remove = plugin.localize(REMOVE) # List all saved searches for search_term in searchdb: item = Listitem() item.label = search_term.title() # Creatre Context Menu item for removing search term item.context.container(saved_searches, str_remove, remove_entry=search_term, **extras) # Update params with full url and set the callback item.params.update(callback_params, search_query=search_term) item.set_callback(callback) yield item
def set_callback(self, callback, *args, **kwargs): """ Set the "callback" function for this listitem. The "callback" parameter can be any of the following: * :class:`codequick.Script<codequick.script.Script>` callback. * :class:`codequick.Route<codequick.route.Route>` callback. * :class:`codequick.Resolver<codequick.resolver.Resolver>` callback. * A callback reference object :func:`Script.ref<codequick.script.Script.ref>`. :param callback: The "callback" function or reference object. :param args: "Positional" arguments that will be passed to the callback. :param kwargs: "Keyword" arguments that will be passed to the callback. """ if hasattr(callback, "route"): callback = callback.route elif not isinstance(callback, CallbackRef): # We don't have a plugin / http path, # So we should then have a callback path if "://" not in callback: msg = "passing callback path to 'set_callback' is deprecated, " \ "use callback reference 'Route.ref' instead" logger.warning("DeprecationWarning: " + msg) callback = dispatcher.get_route(callback) else: msg = "passing a playable / plugin path to 'set_callback' is deprecated, use 'set_path' instead" logger.warning("DeprecationWarning: " + msg) is_folder = kwargs.pop("is_folder", False) is_playable = kwargs.pop("is_playable", not is_folder) self.set_path(callback, is_folder, is_playable) return self.params.update(kwargs) self._is_playable = callback.is_playable self._is_folder = callback.is_folder self._path = callback self._args = args