Skip to content

Commit

Permalink
add exclude anime feature (#120)
Browse files Browse the repository at this point in the history
* add exclude anime feature
  • Loading branch information
Limeone authored Sep 7, 2020
1 parent 7b9a414 commit 200080e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/resources/lib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
def render_pagination(pagination):
"""Add "next page" button"""
if pagination and (int(pagination["current"]) + 1 <= int(pagination["total"])):
page = int(pagination["current"]) + 1
kwargs = {"page": int(pagination["current"]) + 1}
if plugin.settings.exclude_anime == "true" and "start_from" in pagination:
kwargs["start_from"] = pagination["start_from"]
img = plugin.routing.build_icon_path("next_page")
li = plugin.list_item("[COLOR FFFFF000]Вперёд[/COLOR]", iconImage=img, thumbnailImage=img)
url = plugin.routing.add_kwargs_to_url(page=page)
url = plugin.routing.add_kwargs_to_url(**kwargs)
xbmcplugin.addDirectoryItem(plugin.handle, url, li, True)
xbmcplugin.endOfDirectory(plugin.handle)

Expand Down Expand Up @@ -124,11 +126,14 @@ def items(content_type, heading):
else:
data = {"type": None if content_type == "all" else content_type.rstrip("s")}
data.update(plugin.kwargs)
exclude_anime = plugin.settings.exclude_anime == "true"
if heading == "sort":
data.update(plugin.sorting_params)
response = plugin.items.get("items", data)
response = plugin.items.get("items", data=data, exclude_anime=exclude_anime)
else:
response = plugin.items.get("items/{}".format(heading), data)
response = plugin.items.get(
"items/{}".format(heading), data=data, exclude_anime=exclude_anime
)
render_items(response.items, content_type)
render_pagination(response.pagination)

Expand Down
51 changes: 49 additions & 2 deletions src/resources/lib/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ def content_type_map(self):
def __init__(self, plugin):
self.plugin = plugin

def get(self, endpoint, data=None):
resp = self.plugin.client(endpoint).get(data=data)
def get(self, endpoint, data=None, exclude_anime=False):
if exclude_anime:
resp = self._get_anime_exluded(endpoint, data=data)
else:
resp = self.plugin.client(endpoint).get(data=data)
items = [self.instantiate(item=item, index=i) for i, item in enumerate(resp["items"], 1)]
return Response(items, resp.get("pagination"))

Expand Down Expand Up @@ -78,6 +81,50 @@ def get_playable(self, item, season_index=None, index=None):
else:
return item

def _get_anime_exluded(self, endpoint, data=None, collection=None):
# init items collection
collection = collection or {"items": []}

# exclude start_from from request data
start_from = int(data.pop("start_from", 0))

resp = self.plugin.client(endpoint).get(data=data)

new_items = resp["items"]
pagination = resp["pagination"]
page_size = int(pagination["perpage"])
collection["pagination"] = pagination

# filter items list from anime items
non_anime_items = list(
filter(lambda x: all(i["id"] != 25 for i in x["genres"]), new_items[start_from:])
)

# if not enough items continue with next API page
if len(non_anime_items) + len(collection["items"]) < page_size:
collection["items"].extend(non_anime_items)

if int(pagination["current"]) + 1 < int(pagination["total"]):
data.update({"page": pagination["current"] + 1, "start_from": 0})
collection = self._get_anime_exluded(endpoint, data, collection)
else:
# exlude extra items from filtered items
count_items_to_extend = page_size - len(collection["items"])
items = non_anime_items[:count_items_to_extend]
last_item_id = items[-1]["id"]
last_item_index = next(
(index for (index, d) in enumerate(new_items) if d["id"] == last_item_id), None
)
collection["items"].extend(items)
collection["pagination"]["current"] = (
pagination["current"] - 1
) # start from current API page
collection["pagination"]["start_from"] = (
last_item_index + 1
) # do not include last item to next page

return collection


class ItemEntity(object):
def __init__(self, parent, item, index=None):
Expand Down
1 change: 1 addition & 0 deletions src/resources/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<setting id="stream_type" type="select" label="Тип стриминга" values="http|hls|hls2|hls4" enable="eq(1,false)" default="hls"/>
<setting id="ask_quality" type="bool" label="Задавать вопрос о качестве видео перед воспроизведением" default="false"/>
<setting id="mark_advert" type="bool" label="Отмечать видео с рекламой (!)" default="false"/>
<setting id="exclude_anime" type="bool" label="Исключать аниме" default="false"/>
<setting type="lsep" label="Требуется InputStream Adaptive"/>
<setting id="inputstream_adaptive_enabled" type="bool" label="Использовать inputstream adaptive" default="false" enable="System.HasAddon(inputstream.adaptive)"/>
<setting id="inputstream_helper_install" type="action" label="Установить inputstream helper" action="RunPlugin(plugin://$ID/inputstream_helper_install)" enable="System.HasAddon(inputstream.adaptive)"/>
Expand Down

0 comments on commit 200080e

Please sign in to comment.