Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions default.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# -*- coding: utf-8 -*-
#
import logging

import xbmcaddon

from resources.lib import kodilogging
from resources.lib.kodiUtilities import getSetting, setSetting
from resources.lib.service import traktService
from resources.lib.utilities import createError, checkIfNewVersion
from resources.lib.kodiUtilities import setSetting, getSetting
from resources.lib.utilities import checkIfNewVersion, createError

__addon__ = xbmcaddon.Addon('script.trakt')
__addonversion__ = __addon__.getAddonInfo('version')
__addonid__ = __addon__.getAddonInfo('id')
__addon__ = xbmcaddon.Addon("script.trakt")
__addonversion__ = __addon__.getAddonInfo("version")
__addonid__ = __addon__.getAddonInfo("id")
kodilogging.config()
logger = logging.getLogger(__name__)

logger.debug("Loading '%s' version '%s'" % (__addonid__, __addonversion__))
if checkIfNewVersion(str(getSetting('version')), str(__addonversion__)):
setSetting('version', __addonversion__)
logger.debug(f"Loading '{__addonid__}' version '{__addonversion__}'")
if checkIfNewVersion(str(getSetting("version")), str(__addonversion__)):
setSetting("version", __addonversion__)

try:
traktService().run()
except Exception as ex:
except Exception as ex: # noqa: BLE001 - preserve the existing broad failure boundary
message = createError(ex)
logger.fatal(message)

logger.debug("'%s' shutting down." % __addonid__)
logger.debug(f"'{__addonid__}' shutting down.")
7 changes: 5 additions & 2 deletions defaultscript.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# -*- coding: utf-8 -*-
import logging

import xbmcaddon

from resources.lib import script

logger = logging.getLogger(__name__)

__addon__ = xbmcaddon.Addon("script.trakt")


def Main():
script.run()

if __name__ == '__main__':

if __name__ == "__main__":
Main()
17 changes: 10 additions & 7 deletions resources/lib/deviceAuthDialog.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import xbmcgui
import logging
import time

import xbmcaddon
from resources.lib.kodiUtilities import notification, setSetting, getString
import logging
import xbmcgui

from resources.lib.kodiUtilities import getString, notification, setSetting

__addon__ = xbmcaddon.Addon("script.trakt")

Expand Down Expand Up @@ -32,7 +34,8 @@ def onInit(self) -> None:
authcode = self.getControl(AUTHCODE_LABEL)
warning = self.getControl(WARNING_LABEL)
instuction.setLabel(
getString(32159).format("[COLOR red]" + self.url + "[/COLOR]"))
getString(32159).format("[COLOR red]" + self.url + "[/COLOR]")
)
authcode.setLabel(self.code)
warning.setLabel(getString(32162))

Expand All @@ -47,15 +50,15 @@ def onFocus(self, control: xbmcgui.Control) -> None:
pass

def onClick(self, control: xbmcgui.Control) -> None:
logger.debug('onClick: %s' % (control))
logger.debug(f"onClick: {control}")

if control == LATER_BUTTON:
notification(getString(32157), getString(32150), 5000)
setSetting('last_reminder', str(int(time.time())))
setSetting("last_reminder", str(int(time.time())))

if control == NEVER_BUTTON:
notification(getString(32157), getString(32151), 5000)
setSetting('last_reminder', '-1')
setSetting("last_reminder", "-1")

if control in [LATER_BUTTON, NEVER_BUTTON]:
self.close()
2 changes: 0 additions & 2 deletions resources/lib/globals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
#
from typing import Any

traktapi: Any = None
102 changes: 55 additions & 47 deletions resources/lib/kodiUtilities.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# -*- coding: utf-8 -*-
#
import json
import logging
import re

import xbmc
import xbmcgui
import xbmcaddon
import json
import re
import logging
from typing import Tuple, List, Dict, Union, Optional
from resources.lib import utilities
import xbmcgui

from resources.lib import utilities

# read settings
__addon__ = xbmcaddon.Addon("script.trakt")
Expand All @@ -20,7 +17,10 @@


def notification(
header: str, message: str, time: int = 5000, icon: str = __addon__.getAddonInfo("icon")
header: str,
message: str,
time: int = 5000,
icon: str = __addon__.getAddonInfo("icon"),
) -> None:
xbmcgui.Dialog().notification(header, message, icon, time)

Expand All @@ -33,7 +33,7 @@ def getSetting(setting: str) -> str:
return __addon__.getSetting(setting).strip()


def setSetting(setting: str, value: Union[str, int, float, bool]) -> None:
def setSetting(setting: str, value: str | float | bool) -> None:
__addon__.setSetting(setting, str(value))


Expand All @@ -59,7 +59,7 @@ def getString(string_id: int) -> str:
return __addon__.getLocalizedString(string_id)


def kodiJsonRequest(params: Dict) -> Optional[Union[Dict, List]]:
def kodiJsonRequest(params: dict) -> dict | list | None:
data = json.dumps(params)
request = xbmc.executeJSONRPC(data)

Expand All @@ -70,7 +70,7 @@ def kodiJsonRequest(params: Dict) -> Optional[Union[Dict, List]]:
return response["result"]
return None
except KeyError:
logger.warn("[%s] %s" % (params["method"], response["error"]["message"]))
logger.warning("[{}] {}".format(params["method"], response["error"]["message"]))
return None


Expand Down Expand Up @@ -113,26 +113,31 @@ def checkExclusion(fullpath: str) -> bool:

# Path exclusions
ExcludePath = getSetting("ExcludePath")
if ExcludePath != "" and getSettingAsBool("ExcludePathOption"):
if fullpath.startswith(ExcludePath):
logger.debug(
"checkExclusion(): Video is from location, which is currently set as excluded path 1."
)
return True
if (
ExcludePath != ""
and getSettingAsBool("ExcludePathOption")
and fullpath.startswith(ExcludePath)
):
logger.debug(
"checkExclusion(): Video is from location, which is currently set as excluded path 1."
)
return True

found = False
for x in range(2, 13):
found |= utilities.checkExcludePath(
getSetting("ExcludePath%i" % x),
getSettingAsBool("ExcludePathOption%i" % x),
getSetting(f"ExcludePath{x:d}"),
getSettingAsBool(f"ExcludePathOption{x:d}"),
fullpath,
x,
)

return found


def kodiRpcToTraktMediaObject(type: str, data: Dict, mode: str = "collected") -> Optional[Dict]:
def kodiRpcToTraktMediaObject(
type: str, data: dict, mode: str = "collected"
) -> dict | None:
if type == "show":
if "uniqueid" in data:
data["ids"] = data.pop("uniqueid")
Expand Down Expand Up @@ -194,9 +199,12 @@ def kodiRpcToTraktMediaObject(type: str, data: Dict, mode: str = "collected") ->
episode["rating"] = (
data["userrating"] if "userrating" in data and data["userrating"] > 0 else 0
)
if mode == "watched" and episode["watched"]:
return episode
elif mode == "collected" and episode["collected"]:
if (
mode == "watched"
and episode["watched"]
or mode == "collected"
and episode["collected"]
):
return episode
else:
return
Expand Down Expand Up @@ -231,7 +239,7 @@ def kodiRpcToTraktMediaObject(type: str, data: Dict, mode: str = "collected") ->
return


def kodiRpcToTraktMediaObjects(data: Dict, mode: str = "collected") -> Optional[List]:
def kodiRpcToTraktMediaObjects(data: dict, mode: str = "collected") -> list | None:
if "tvshows" in data:
shows = data["tvshows"]

Expand All @@ -252,8 +260,8 @@ def kodiRpcToTraktMediaObjects(data: Dict, mode: str = "collected") -> Optional[
if episodeObject:
a_episodes[s_no].append(episodeObject)

for episode in a_episodes:
seasons.append({"number": episode, "episodes": a_episodes[episode]})
for episode, episodes in a_episodes.items():
seasons.append({"number": episode, "episodes": episodes})
return seasons

elif "movies" in data:
Expand All @@ -271,7 +279,7 @@ def kodiRpcToTraktMediaObjects(data: Dict, mode: str = "collected") -> Optional[
return


def getShowDetailsFromKodi(showID: int, fields: List) -> Optional[Dict]:
def getShowDetailsFromKodi(showID: int, fields: list) -> dict | None:
result = kodiJsonRequest(
{
"jsonrpc": "2.0",
Expand All @@ -280,7 +288,7 @@ def getShowDetailsFromKodi(showID: int, fields: List) -> Optional[Dict]:
"id": 1,
}
)
logger.debug("getShowDetailsFromKodi(): %s" % str(result))
logger.debug(f"getShowDetailsFromKodi(): {result!s}")

if not result:
logger.debug("getShowDetailsFromKodi(): Result from Kodi was empty.")
Expand All @@ -293,7 +301,7 @@ def getShowDetailsFromKodi(showID: int, fields: List) -> Optional[Dict]:
return None


def getSeasonDetailsFromKodi(seasonID: int, fields: List) -> Optional[Dict]:
def getSeasonDetailsFromKodi(seasonID: int, fields: list) -> dict | None:
result = kodiJsonRequest(
{
"jsonrpc": "2.0",
Expand All @@ -302,7 +310,7 @@ def getSeasonDetailsFromKodi(seasonID: int, fields: List) -> Optional[Dict]:
"id": 1,
}
)
logger.debug("getSeasonDetailsFromKodi(): %s" % str(result))
logger.debug(f"getSeasonDetailsFromKodi(): {result!s}")

if not result:
logger.debug("getSeasonDetailsFromKodi(): Result from Kodi was empty.")
Expand All @@ -318,7 +326,7 @@ def getSeasonDetailsFromKodi(seasonID: int, fields: List) -> Optional[Dict]:
# get a single episode from kodi given the id


def getEpisodeDetailsFromKodi(libraryId: int, fields: List) -> Optional[Dict]:
def getEpisodeDetailsFromKodi(libraryId: int, fields: list) -> dict | None:
result = kodiJsonRequest(
{
"jsonrpc": "2.0",
Expand All @@ -327,7 +335,7 @@ def getEpisodeDetailsFromKodi(libraryId: int, fields: List) -> Optional[Dict]:
"id": 1,
}
)
logger.debug("getEpisodeDetailsFromKodi(): %s" % str(result))
logger.debug(f"getEpisodeDetailsFromKodi(): {result!s}")

if not result:
logger.debug("getEpisodeDetailsFromKodi(): Result from Kodi was empty.")
Expand Down Expand Up @@ -359,7 +367,7 @@ def getEpisodeDetailsFromKodi(libraryId: int, fields: List) -> Optional[Dict]:
# get a single movie from kodi given the id


def getMovieDetailsFromKodi(libraryId: int, fields: List) -> Optional[Dict]:
def getMovieDetailsFromKodi(libraryId: int, fields: list) -> dict | None:
result = kodiJsonRequest(
{
"jsonrpc": "2.0",
Expand All @@ -368,7 +376,7 @@ def getMovieDetailsFromKodi(libraryId: int, fields: List) -> Optional[Dict]:
"id": 1,
}
)
logger.debug("getMovieDetailsFromKodi(): %s" % str(result))
logger.debug(f"getMovieDetailsFromKodi(): {result!s}")

if not result:
logger.debug("getMovieDetailsFromKodi(): Result from Kodi was empty.")
Expand All @@ -381,7 +389,7 @@ def getMovieDetailsFromKodi(libraryId: int, fields: List) -> Optional[Dict]:
return None


def checkAndConfigureProxy() -> Optional[str]:
def checkAndConfigureProxy() -> str | None:
proxyActive = kodiJsonRequest(
{
"jsonrpc": "2.0",
Expand Down Expand Up @@ -481,10 +489,10 @@ def checkAndConfigureProxy() -> Optional[str]:
return None


def getMediaType() -> Optional[str]:
def getMediaType() -> str | None:
listType = xbmc.getInfoLabel("ListItem.DBTYPE")

xbmc.log("list item type: %s" % listType, xbmc.LOGINFO)
xbmc.log(f"list item type: {listType}", xbmc.LOGINFO)

if listType == "movie":
return "movie"
Expand All @@ -498,7 +506,7 @@ def getMediaType() -> Optional[str]:
return None


def getInfoLabelDetails(result: Dict) -> Tuple[str, Dict]:
def getInfoLabelDetails(result: dict) -> tuple[str, dict]:
type = result["item"]["type"]
data = {"action": "started"}
# check type of item
Expand All @@ -520,8 +528,7 @@ def getInfoLabelDetails(result: Dict) -> Tuple[str, Dict]:
if video_ids:
data["video_ids"] = json.loads(video_ids)
logger.debug(
"getInfoLabelDetails info - ids: %s, showtitle: %s, Year: %s, Season: %s, Episode: %s"
% (video_ids, showtitle, year, season, episode)
f"getInfoLabelDetails info - ids: {video_ids}, showtitle: {showtitle}, Year: {year}, Season: {season}, Episode: {episode}"
)

if season >= 0 and episode > 0 and (showtitle or video_ids):
Expand All @@ -535,8 +542,9 @@ def getInfoLabelDetails(result: Dict) -> Tuple[str, Dict]:
if year.isdigit():
data["year"] = int(year)
logger.debug(
"getInfoLabelDetails - Playing a non-library 'episode' - %s - S%02dE%02d - %s."
% (data["showtitle"], data["season"], data["episode"], data["title"])
"getInfoLabelDetails - Playing a non-library 'episode' - {:s} - S{:02d}E{:02d} - {:s}.".format(
data["showtitle"], data["season"], data["episode"], data["title"]
)
)
elif (year or video_ids) and season < 0 and not title:
# we have a year or video_id and no season/showtitle info, enough for a movie
Expand All @@ -546,8 +554,9 @@ def getInfoLabelDetails(result: Dict) -> Tuple[str, Dict]:
data["year"] = int(year)
data["title"] = utilities.regex_year(showtitle)[0] or showtitle
logger.debug(
"getInfoLabelDetails - Playing a non-library 'movie' - %s (%s)."
% (data["title"], data.get("year", "NaN"))
"getInfoLabelDetails - Playing a non-library 'movie' - {} ({}).".format(
data["title"], data.get("year", "NaN")
)
)
elif showtitle or title:
title, season, episode = utilities.regex_tvshow(title)
Expand All @@ -558,8 +567,7 @@ def getInfoLabelDetails(result: Dict) -> Tuple[str, Dict]:
data["episode"] = episode
data["title"] = data["showtitle"] = title or showtitle
logger.debug(
"getInfoLabelDetails - Title: %s, showtitle: %s, season: %d, episode: %d"
% (title, showtitle, season, episode)
f"getInfoLabelDetails - Title: {title:s}, showtitle: {showtitle:s}, season: {season:d}, episode: {episode:d}"
)
else:
logger.debug(
Expand Down
Loading
Loading