Skip to content

Commit

Permalink
add: option to exlude paths from scanning when searching for orphaned…
Browse files Browse the repository at this point in the history
… subtitle files
  • Loading branch information
kiziuk committed Feb 1, 2024
1 parent 65b7994 commit b9c329d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 29 deletions.
4 changes: 2 additions & 2 deletions addon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ v2.0.1 (2021-11-08)
- improve matching multiline texts
v2.1.0 (2022-11-30)
- remove handling Kodi's subtitle settings from within the plugin for better compatibility with Kodi 20 (Nexus)
v2.2.0 (2024-01-26)
v2.2.0 (2024-02-01)
- add option to hide progressbar during cleaning orphaned subtitles

- add possibility of ignoring paths during scanning for orphaned subtitles - requires manual editing of 'cleanexcl.def' file in addon's workdir
</news>
<platform>all</platform>
<license>GPL-3.0-or-later</license>
Expand Down
3 changes: 2 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
v2.2.0 (2024-01-26)
v2.2.0 (2024-02-01)
- add option to hide progressbar during cleaning orphaned subtitles
- add possibility of ignoring paths during scanning for orphaned subtitles - requires manual editing of 'cleanexcl.def' file in addon's workdir
v2.1.0 (2022-11-30)
- remove handling Kodi's subtitle settings from within the plugin for better compatibility with Kodi 20 (Nexus)
v2.0.1 (2021-11-08)
Expand Down
2 changes: 2 additions & 0 deletions resources/lib/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
tempdeffilename = os.path.join(__addonworkdir__, 'tempdef.def')
# file that will be used during subtitle processing
deffilename = None
# file holding excluded paths for cleaning process
cleaningexclusionsfilename = os.path.join(__addonworkdir__, 'cleanexcl.def')

# list of input file extensions
# extensions in lowercase with leading dot
Expand Down
112 changes: 86 additions & 26 deletions resources/lib/smangler.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,22 @@ def GetIsoCode(lang):
return outlang


def TruncateComment(line):
"""Returns line without any comment"""

# truncate any comment at the end of line
# https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation
pos = line.find("#")
# if there is no comment, pos==-1
if pos >= 0:
# take only part before comment
line = line[:pos]
# remove whitespaces at the beginning and end
line = line.strip()

return line


# parse a list of definitions from file
# load only a particular section
def GetDefinitions(section):
Expand All @@ -395,15 +411,7 @@ def GetDefinitions(section):
with open(globals.deffilename, "rt", encoding='utf-8') as f:
thissection = False
for line in f:
# truncate any comment at the end of line
# https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation
pos = line.find("#")
# if there is no comment, pos==-1
if pos >= 0:
# take only part before comment
line = line[:pos]
# remove whitespaces at the beginning and end
line = line.strip()
line = TruncateComment(line)

# patterns for finding sections
thissectionpattern = "\[" + section + "\]" # matches: [SeCtIoNnAmE]
Expand Down Expand Up @@ -1244,6 +1252,50 @@ def UpdateDefFile():
# Log("Can not remove temporary definitions file: " + globals.tempdeffilename, xbmc.LOGERROR)


def LoadExcludedPaths(file):
"""Loads paths from file and returns it as a list"""

excludeList = []
if os.path.isfile(file):
# open file
with open(file, "rt", encoding='utf-8') as f:
for line in f:
line = TruncateComment(line)
if line:
excludeList.append(line)

Log("Excluded paths file loaded: {filename}".format(filename=file), xbmc.LOGINFO)

if excludeList:
for entry in excludeList:
Log(" {line}".format(line=entry), xbmc.LOGDEBUG)
else:
Log(" --empty list--", xbmc.LOGDEBUG)

else:
# create empty file with comment
with open(file, "w", encoding='utf-8') as f:
f.write('# This file holds paths that should be excluded from processing\n')
f.write('# when cleaning old subtitles files is performed\n')
f.write('# Provide one path per line\n')

Log("Excluded paths file for cleaning process does not exist: {filename}. Creating an empty one.".format(filename=file), xbmc.LOGINFO)

return excludeList


def isIgnored(dir, ignoredlist):
"""Checks if the directory is within one of the ignored paths"""
if ignoredlist:
Log("Checking if path should be ignored: {path}".format(path=dir),xbmc.LOGDEBUG)
for ignored_path in ignoredlist:
if dir.startswith(ignored_path):
Log(" path matches: {match}".format(match=ignored_path), xbmc.LOGDEBUG)
return True

return False


# walk through video sources and remove any subtitle files that do not acompany its own video any more
# also remove '.noautosubs' files
def RemoveOldSubs():
Expand All @@ -1267,6 +1319,9 @@ def RemoveOldSubs():
# record start time
ClearStartTime = time.time()

# load list containing paths to ignore
ignorePathsList = LoadExcludedPaths(globals.cleaningexclusionsfilename)

# create background dialog
# http://mirrors.kodi.tv/docs/python-docs/13.0-gotham/xbmcgui.html#DialogProgressBG
if not globals.setting_HideOrphanedSubsCleaningProgress:
Expand Down Expand Up @@ -1309,23 +1364,28 @@ def RemoveOldSubs():
while len(directories) > 0:
# take one element from directories list and process it
directory = directories.pop()
dirs, files = xbmcvfs.listdir(directory)
# add every subdir to the list for checking
for subdir in dirs:
Log("Adding subpath: " + os.path.join(directory, subdir), xbmc.LOGDEBUG)
directories.append(os.path.join(directory, subdir))
# check every file in the current subdir and add it to appropriate list
for thisfile in files:
fullfilepath = os.path.join(directory, thisfile)
_filebase, fileext = os.path.splitext(fullfilepath)
if fileext in globals.VideoExtList:
# this file is video - add to video list
Log("Adding to video list: " + fullfilepath, xbmc.LOGDEBUG)
videofiles.append(fullfilepath)
elif fileext in extRemovalList:
# this file is subs related - add to subs list
Log("Adding to subs list: " + fullfilepath, xbmc.LOGDEBUG)
subfiles.append(fullfilepath)
# if the directory is not within any of the dirs to be ignored
if not isIgnored(directory, ignorePathsList):
dirs, files = xbmcvfs.listdir(directory)
# add every subdir to the list for checking
for subdir in dirs:
Log("Adding subpath: " + os.path.join(directory, subdir), xbmc.LOGDEBUG)
directories.append(os.path.join(directory, subdir))
# check every file in the current subdir and add it to appropriate list
for thisfile in files:
fullfilepath = os.path.join(directory, thisfile)
_filebase, fileext = os.path.splitext(fullfilepath)
if fileext in globals.VideoExtList:
# this file is video - add to video list
Log("Adding to video list: " + fullfilepath, xbmc.LOGDEBUG)
videofiles.append(fullfilepath)
elif fileext in extRemovalList:
# this file is subs related - add to subs list
Log("Adding to subs list: " + fullfilepath, xbmc.LOGDEBUG)
subfiles.append(fullfilepath)

else:
Log("Ignoring path: {path}".format(path=directory), xbmc.LOGINFO)

# process custom subtitle path if it is set in Kodi configuration
custompath = xbmcvfs.translatePath("special://subtitles") # path to non-standard dir with subtitles
Expand Down

0 comments on commit b9c329d

Please sign in to comment.