Skip to content

Commit

Permalink
Fixed 'play from beginning' context menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
quarckster committed Nov 4, 2018
1 parent 057c34a commit 44f9ec1
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 74 deletions.
89 changes: 54 additions & 35 deletions resources/lib/addonworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import xbmcgui
import xbmcplugin
from addonutils import (get_internal_link, get_mlink, nav_internal_link, notice, request, route,
ROUTES, trailer_link, video_info)
ROUTES, trailer_link, video_info as extract_video_info)
from authwindow import auth
from client import KinoPubClient
from data import __settings__, __plugin__
Expand Down Expand Up @@ -43,24 +43,31 @@ def show_items(items, add_indexes=False):
title = "{}. {}".format(index, title) if add_indexes else title
li = ExtendedListItem(
title,
art={"poster": item["posters"]["big"]},
poster=item["posters"]["big"],
properties={"id": item["id"]},
addContextMenuItems=True
)
if "in_watchlist" in item:
li.setProperty("in_watchlist", str(int(item["in_watchlist"])))
extra_info = {"trailer": trailer_link(item), "mediatype": mediatype_map[item["type"]]}
video_info = extract_video_info(item, {
"trailer": trailer_link(item),
"mediatype": mediatype_map[item["type"]]
})
# If not serials or multiseries movie, create playable item
if item["type"] not in ["serial", "docuserial", "tvshow"] and not item["subtype"]:
watching_info = KinoPubClient("watching").get(
data={"id": item["id"]})["item"]["videos"][0]
extra_info.update(watching_info)
video_info.update({
"time": watching_info["time"],
"duration": watching_info["duration"],
"playcount": watching_info["status"],
})
link = get_internal_link(
"play",
id=item["id"],
title=title,
info=json.dumps(video_info(item, extra_info)),
art=item["posters"]["big"]
video_info=json.dumps(video_info),
poster=item["posters"]["big"]
)
li.setProperty("isPlayable", "true")
li.setResumeTime(watching_info["time"], watching_info["duration"])
Expand All @@ -71,7 +78,7 @@ def show_items(items, add_indexes=False):
else:
link = get_internal_link("view_seasons", id=item["id"])
isdir = True
li.setInfo("video", video_info(item, extra_info))
li.setInfo("video", video_info)
xbmcplugin.addDirectoryItem(request.handle, link, li, isdir)


Expand Down Expand Up @@ -191,8 +198,8 @@ def seasons(id):
watching_season = watching_info["seasons"][season["number"] - 1]
li = ExtendedListItem(
season_title,
info={"video": video_info(item, {"season": season["number"]})},
art={"poster": item["posters"]["big"]}
video_info=extract_video_info(item, {"season": season["number"]}),
poster=item["posters"]["big"]
)
if watching_season["status"] < 1 and not selectedSeason:
selectedSeason = True
Expand All @@ -216,7 +223,7 @@ def episodes(id):
episode_title = "e{:02d}".format(video["number"])
if video["title"]:
episode_title = "{} | {}".format(episode_title, video["title"].encode("utf-8"))
info = video_info(item, {
info = extract_video_info(item, {
"episode": video["number"],
"time": watching_episode["time"],
"duration": watching_episode["duration"],
Expand All @@ -226,8 +233,8 @@ def episodes(id):
li = ExtendedListItem(
episode_title,
thumbnailImage=video["thumbnail"],
info={"video": info},
art={"poster": item["posters"]["big"]},
video_info=info,
poster=item["posters"]["big"],
properties={"id": item["id"], "isPlayable": "true"},
addContextMenuItems=True
)
Expand All @@ -236,8 +243,8 @@ def episodes(id):
id=item["id"],
title=episode_title,
video_data=json.dumps(video),
info=json.dumps(info),
art=item["posters"]["big"]
video_info=json.dumps(info),
poster=item["posters"]["big"]
)
xbmcplugin.addDirectoryItem(request.handle, link, li, False)
xbmcplugin.endOfDirectory(request.handle)
Expand All @@ -257,7 +264,7 @@ def season_episodes(id, season_number):
episode_title = "s{:02d}e{:02d}".format(season_number, episode["number"])
if episode["title"]:
episode_title = "{} | {}".format(episode_title, episode["title"].encode("utf-8"))
info = video_info(item, {
info = extract_video_info(item, {
"season": season_number,
"episode": episode["number"],
"time": watching_episode["time"],
Expand All @@ -268,8 +275,8 @@ def season_episodes(id, season_number):
li = ExtendedListItem(
episode_title,
thumbnailImage=episode["thumbnail"],
art={"poster": item["posters"]["big"]},
info={"video": info},
poster=item["posters"]["big"],
video_info=info,
properties={"id": item["id"], "isPlayable": "true"},
addContextMenuItems=True
)
Expand All @@ -281,21 +288,21 @@ def season_episodes(id, season_number):
id=item["id"],
title=episode_title,
video_data=json.dumps(episode),
info=json.dumps(info),
art=item["posters"]["big"]
video_info=json.dumps(info),
poster=item["posters"]["big"]
)
xbmcplugin.addDirectoryItem(request.handle, link, li, False)
xbmcplugin.endOfDirectory(request.handle)


@route("/play")
def play(id, title, info, video_data=None, art=None):
def play(id, title, video_info, video_data=None, poster=None):
if not video_data:
response = KinoPubClient("items/{}".format(id)).get()
video_data = response["item"]["videos"][0]
info = video_info(response["item"], json.loads(info))
video_info = extract_video_info(response["item"], json.loads(video_info))
video_data = json.loads(video_data) if isinstance(video_data, str) else video_data
info = json.loads(info) if isinstance(info, str) else info
video_info = json.loads(video_info) if isinstance(video_info, str) else video_info
if "files" not in video_data:
notice("Видео обновляется и временно не доступно!", "Видео в обработке", time=8000)
return
Expand All @@ -308,10 +315,16 @@ def play(id, title, info, video_data=None, art=None):
li = ExtendedListItem(
title,
path=url,
info={"video": info},
properties={"id": id},
art={"poster": art},
subtitles=[subtitle["url"] for subtitle in video_data["subtitles"]]
properties={
"id": id,
"play_duration": video_info["duration"],
"play_resumetime": video_info["time"],
"video_number": video_info.get("episode", 1),
"season_number": video_info.get("season", ""),
"playcount": video_info["playcount"]
},
poster=poster,
subtitles=[subtitle["url"] for subtitle in video_data["subtitles"]],
)
player = Player(list_item=li)
xbmcplugin.setResolvedUrl(request.handle, True, li)
Expand Down Expand Up @@ -388,9 +401,9 @@ def watching():
li = ExtendedListItem(
title,
str(item["new"]),
art={"poster": item["posters"]["big"]},
poster=item["posters"]["big"],
properties={"id": str(item["id"]), "in_watchlist": "1"},
info={"video": {"mediatype": mediatype_map[item["type"]]}},
video_info={"mediatype": mediatype_map[item["type"]]},
addContextMenuItems=True
)
link = get_internal_link("view_seasons", id=item["id"])
Expand All @@ -404,26 +417,32 @@ def watching_movies():
for item in KinoPubClient("watching/movies").get()["items"]:
li = ExtendedListItem(
item["title"].encode("utf-8"),
art={"poster": item["posters"]["big"]},
poster=item["posters"]["big"],
properties={"id": item["id"]},
info={"video": {"mediatype": mediatype_map[item["type"]]}},
video_info={"mediatype": mediatype_map[item["type"]]},
addContextMenuItems=True
)
if item["subtype"] == "multi":
link = get_internal_link("view_episodes", id=item["id"])
isdir = True
else:
response = KinoPubClient("watching").get(data={"id": item["id"]})
watching_info = response["item"]["videos"][0]
response = KinoPubClient("items/{}".format(item["id"])).get()
watching_info = KinoPubClient("watching").get(data={"id": item["id"]})["item"]["videos"]
watching_info = watching_info[0]
video_info = extract_video_info(response["item"], {
"time": watching_info["time"],
"duration": watching_info["duration"],
"playcount": watching_info["status"],
})
li.setInfo("video", video_info)
li.setProperty("isPlayable", "true")
li.setInfo("video", {"duration": watching_info["duration"]})
li.setResumeTime(watching_info["time"])
link = get_internal_link(
"play",
id=item["id"],
title=item["title"].encode("utf-8"),
art=item["posters"]["big"],
info=json.dumps(watching_info)
poster=item["posters"]["big"],
video_info=json.dumps(video_info)
)
isdir = False
xbmcplugin.addDirectoryItem(request.handle, link, li, isdir)
Expand Down
24 changes: 12 additions & 12 deletions resources/lib/listitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ def __new__(cls, name, label2="", iconImage="", thumbnailImage="", path="", **kw
return super(ExtendedListItem, cls).__new__(cls, name, label2, iconImage, thumbnailImage,
path)

def __init__(self, name, label2="", iconImage="", thumbnailImage="", path="", art=None,
info=None, properties=None, addContextMenuItems=False, subtitles=None):
def __init__(self, name, label2="", iconImage="", thumbnailImage="", path="", poster=None,
video_info=None, properties=None, addContextMenuItems=False, subtitles=None):
super(ExtendedListItem, self).__init__(name, label2, iconImage, thumbnailImage, path)
if info:
self.setInfos(**info)
self.setResumeTime(info.get("video", {}).get("time"))
if art:
self.setArt(art)
if properties:
self.setProperties(**properties)
if video_info:
self.setInfo("video", video_info)
self.setResumeTime(video_info.get("time"))
if poster:
self.setArt({"poster": poster})
if subtitles:
self.setSubtitles(subtitles)
if addContextMenuItems:
Expand Down Expand Up @@ -61,8 +61,12 @@ def _addBookmarksContextMenuItem(self, menu_items):
link = get_internal_link("edit_bookmarks", item_id=item_id)
menu_items.append((label, "Container.Update({})".format(link)))

def _addSeparatorContextMenuItem(self, menu_items):
length = min([len(item[0]) for item in menu_items])
menu_items.append(("─" * length, ""))

def addPredefinedContextMenuItems(self, items=None):
items = items or ["watched", "watchlist", "bookmarks"]
items = items or ["watched", "watchlist", "bookmarks", "separator"]
menu_items = []
for item in items:
getattr(self, "_add{}ContextMenuItem".format(item.capitalize()))(menu_items)
Expand All @@ -72,10 +76,6 @@ def setProperties(self, **properties):
for prop, value in properties.items():
self.setProperty(prop, str(value))

def setInfos(self, **info):
for info_type, info_value in info.items():
self.setInfo(info_type, info_value)

def setResumeTime(self, resumetime, totaltime=None):
totaltime = float(totaltime or self.getduration())
if (resumetime is not None and 100 * resumetime / totaltime <=
Expand Down
16 changes: 8 additions & 8 deletions resources/lib/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ def should_make_resume_point(self):

@property
def should_mark_as_watched(self):
return (100 * self.marktime / float(self.list_item.getduration()) >
return (100 * self.marktime / float(self.list_item.getProperty("play_duration")) >
get_adv_setting("video", "playcountminimumpercent"))

@property
def should_reset_resume_point(self):
return (
self.marktime < get_adv_setting("video", "ignoresecondsatstart") and
(float(self.list_item.getProperty("resumetime")) >
(float(self.list_item.getProperty("play_resumetime")) >
get_adv_setting("video", "ignoresecondsatstart"))
)

@property
def _base_data(self):
id = self.list_item.getProperty("id")
video_number = self.list_item.getVideoInfoTag().getEpisode()
season_number = self.list_item.getVideoInfoTag().getSeason()
if season_number != -1:
video_number = self.list_item.getProperty("video_number")
season_number = self.list_item.getProperty("season_number")
if season_number:
data = {"id": id, "season": season_number, "video": video_number}
else:
data = {"id": id, "video": 1 if video_number == -1 else video_number}
data = {"id": id, "video": video_number}
return data

def onPlayBackStopped(self):
Expand All @@ -53,7 +53,7 @@ def onPlayBackStopped(self):
if self.should_make_resume_point:
data["time"] = self.marktime
KinoPubClient("watching/marktime").get(data=data)
elif self.should_mark_as_watched and self.list_item.getVideoInfoTag().getPlayCount() < 1:
elif self.should_mark_as_watched and int(self.list_item.getProperty("playcount")) < 1:
data["status"] = 1
KinoPubClient("watching/toggle").get(data=data)
elif self.should_reset_resume_point:
Expand All @@ -65,7 +65,7 @@ def onPlayBackStopped(self):

def onPlayBackEnded(self):
self.is_playing = False
if self.list_item.getVideoInfoTag().getPlayCount() < 1:
if int(self.list_item.getProperty("playcount")) < 1:
data = self._base_data
data["status"] = 1
KinoPubClient("watching/toggle").get(data=data)
Expand Down
Loading

0 comments on commit 44f9ec1

Please sign in to comment.