Compare commits
90 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 824e2c5106 | |||
| 5ec1f31434 | |||
| 4f4a9a8048 | |||
| a456ae4fa7 | |||
| b3b0ab225b | |||
| f4aa5d2bf1 | |||
| 8cc7ab5775 | |||
| 6d4a07db2e | |||
| a0d924c3b0 | |||
| c201bf3ef3 | |||
| 8d45a46ee2 | |||
| 6a5a9b33c2 | |||
| 6d237b1781 | |||
| 46b40bf2f0 | |||
| 546c258c82 | |||
| f6031e9b9c | |||
| b6480f9e32 | |||
| b830aba31c | |||
| c6b0c95aa4 | |||
| 129f58c059 | |||
| c10242b388 | |||
| 5c0a430d84 | |||
| 382afa52e9 | |||
| 8fd5191685 | |||
| ce67d74980 | |||
| 0e95e67d7e | |||
| 26e7a572d4 | |||
| 0d3d27c343 | |||
| 97764cbac8 | |||
| 883d9b60ee | |||
| 24f6a8e1f2 | |||
| fa366f2789 | |||
| 2bbe7d15eb | |||
| c5e3dda387 | |||
| 0184c41c8e | |||
| 0c8b0c1dd9 | |||
| 71e5c74b77 | |||
| 21ab566cff | |||
| 20e475cfb7 | |||
| febf592db6 | |||
| fe94358f0c | |||
| 0cb560b856 | |||
| faa0bb7550 | |||
| 1d7df79465 | |||
| 72f2a4fc86 | |||
| 8434eb4ff4 | |||
| ba4280ee4e | |||
| 34f34cef4d | |||
| 30f21d71c8 | |||
| 592d264b19 | |||
| 9d55dca0e1 | |||
| da4111904c | |||
| a4b9358f14 | |||
| 122c6527d4 | |||
| 844b76e116 | |||
| f262009349 | |||
| bc1a4ceb42 | |||
| a8ba984064 | |||
| fda6dab572 | |||
| 4cdb777840 | |||
| f94d9595a8 | |||
| 5d38bd26a2 | |||
| 9239261c5a | |||
| e3aed706fb | |||
| 89d87c6356 | |||
| a0cfe0b6fd | |||
| 476c311e01 | |||
| bb10b8fffa | |||
| 4a8fa4a838 | |||
| 624b844454 | |||
| 027f1f4045 | |||
| 28d66dc162 | |||
| 3995e732f6 | |||
| 60f553707a | |||
| c37e2ceaab | |||
| abd7922700 | |||
| c47389426e | |||
| 6b5c7bd14b | |||
| cb072c2aa6 | |||
| 533649c791 | |||
| 3105f2e8ae | |||
| 8160bc98fd | |||
| 8a1b615fe9 | |||
| 3f3bb2d830 | |||
| ae4871f6dd | |||
| f46da7b12f | |||
| b3f5bdd58d | |||
| ca8ecd297b | |||
| d954d25a73 | |||
| bda261b495 |
@@ -1,3 +1,14 @@
|
||||
1.3.31.513
|
||||
|
||||
- core: add option to only download one language again (and skip the addition of .lang to the subtitle filename) (default: off); fixes #126
|
||||
- core: add option to always encode saved subtitles to UTF-8 (default: on); fixes #128
|
||||
- core: add fallback encoding detection using bs4.UnicodeDammit; hopefully fixes #101
|
||||
- core: update libraries: chardet, beautifulsoup, six
|
||||
- menu/core: check Plex libraries for permission problems on plugin start and report them in the channel menu (option, default: on); fixes #143
|
||||
- menu: while a manual refresh takes place, add a refresh button to the top of the SZ menu for convenience
|
||||
- menu: move the "add/remove X to ignore list" menu item to the bottom of the list on item detail
|
||||
|
||||
|
||||
1.3.27.491
|
||||
|
||||
- menu/core: make Sub-Zero channel menu optional (setting: "Enable Sub-Zero channel (disabling doesn't affect the subtitle features)?")
|
||||
|
||||
@@ -14,24 +14,30 @@ for key, value in getattr(module, "__builtins__").iteritems():
|
||||
globals()[key] = value
|
||||
|
||||
import logger
|
||||
import logging
|
||||
|
||||
# temporarily add the console handler and set it to DEBUG to catch errors upon imports
|
||||
Core.log.addHandler(logger.console_handler)
|
||||
Core.log.setLevel(logging.DEBUG)
|
||||
|
||||
sys.modules["logger"] = logger
|
||||
|
||||
from subzero import intent
|
||||
import subliminal
|
||||
import subliminal_patch
|
||||
import support
|
||||
|
||||
import interface
|
||||
|
||||
sys.modules["interface"] = interface
|
||||
|
||||
from subzero.constants import OS_PLEX_USERAGENT, PERSONAL_MEDIA_IDENTIFIER
|
||||
from subzero import intent
|
||||
from interface.menu import *
|
||||
from support.subtitlehelpers import getSubtitlesFromMetadata, force_utf8
|
||||
from support.storage import storeSubtitleInfo
|
||||
from support.config import config, IGNORE_FN
|
||||
from support.plex_media import convert_media_to_parts, get_media_item_ids, scan_parts
|
||||
from support.subtitlehelpers import get_subtitles_from_metadata, force_utf8
|
||||
from support.helpers import notify_executable
|
||||
from support.storage import store_subtitle_info, whack_missing_parts
|
||||
from support.items import is_ignored
|
||||
from support.config import config
|
||||
|
||||
|
||||
def Start():
|
||||
@@ -45,144 +51,26 @@ def Start():
|
||||
ValidatePrefs()
|
||||
Log.Debug(config.full_version)
|
||||
|
||||
if not config.plex_api_working:
|
||||
Log.Error(lib_unaccessible_error)
|
||||
return
|
||||
|
||||
if not config.permissions_ok:
|
||||
Log.Error("Insufficient permissions on library folders")
|
||||
Log.Error("Insufficient permissions on library folders:")
|
||||
for title, path in config.missing_permissions:
|
||||
Log.Error("Insufficient permissions on library %s, folder: %s" % (title, path))
|
||||
return
|
||||
|
||||
scheduler.run()
|
||||
|
||||
|
||||
def initSubliminalPatches():
|
||||
def init_subliminal_patches():
|
||||
# configure custom subtitle destination folders for scanning pre-existing subs
|
||||
dest_folder = config.subtitleDestinationFolder
|
||||
dest_folder = config.subtitle_destination_folder
|
||||
subliminal_patch.patch_video.CUSTOM_PATHS = [dest_folder] if dest_folder else []
|
||||
subliminal_patch.patch_provider_pool.DOWNLOAD_TRIES = int(Prefs['subtitles.try_downloads'])
|
||||
subliminal_patch.patch_providers.addic7ed.USE_BOOST = bool(Prefs['provider.addic7ed.boost'])
|
||||
|
||||
|
||||
def flattenToParts(media, kind="series"):
|
||||
"""
|
||||
iterates through media and returns the associated parts (videos)
|
||||
:param media:
|
||||
:param kind:
|
||||
:return:
|
||||
"""
|
||||
parts = []
|
||||
if kind == "series":
|
||||
for season in media.seasons:
|
||||
for episode in media.seasons[season].episodes:
|
||||
ep = media.seasons[season].episodes[episode]
|
||||
for item in media.seasons[season].episodes[episode].items:
|
||||
for part in item.parts:
|
||||
parts.append({"video": part, "type": "episode", "title": ep.title, "series": media.title, "id": ep.id})
|
||||
else:
|
||||
for item in media.items:
|
||||
for part in item.parts:
|
||||
parts.append({"video": part, "type": "movie", "title": media.title, "id": media.id})
|
||||
return parts
|
||||
|
||||
|
||||
def parseMediaToParts(media, kind="series"):
|
||||
"""
|
||||
returns a list of parts to be used later on; ignores folders with an existing "subzero.ignore" file
|
||||
:param media:
|
||||
:param kind:
|
||||
:return:
|
||||
"""
|
||||
parts = flattenToParts(media, kind=kind)
|
||||
if not Prefs["subtitles.ignore_fs"]:
|
||||
return parts
|
||||
|
||||
use_parts = []
|
||||
check_ignore_paths = [".", "../"]
|
||||
if kind == "series":
|
||||
check_ignore_paths.append("../../")
|
||||
|
||||
for part in parts:
|
||||
base_folder, fn = os.path.split(part["video"].file)
|
||||
|
||||
ignore = False
|
||||
for rel_path in check_ignore_paths:
|
||||
fld = os.path.abspath(os.path.join(base_folder, rel_path))
|
||||
for ifn in IGNORE_FN:
|
||||
if os.path.isfile(os.path.join(fld, ifn)):
|
||||
Log.Info(u'Ignoring "%s" because "%s" exists in "%s"', fn, ifn, fld)
|
||||
ignore = True
|
||||
break
|
||||
if ignore:
|
||||
break
|
||||
|
||||
if not ignore:
|
||||
use_parts.append(part)
|
||||
return use_parts
|
||||
|
||||
|
||||
def getFPS(streams):
|
||||
for stream in streams:
|
||||
# video
|
||||
if stream.type == 1:
|
||||
return stream.frameRate
|
||||
return "25.000"
|
||||
|
||||
|
||||
def scanParts(parts, kind="series"):
|
||||
"""
|
||||
receives a list of parts containing dictionaries returned by flattenToParts
|
||||
:param parts:
|
||||
:param kind: series or movies
|
||||
:return: dictionary of scanned videos of subliminal.video.scan_video
|
||||
"""
|
||||
ret = {}
|
||||
for part in parts:
|
||||
force_refresh = intent.get("force", part["id"])
|
||||
hints = {"expected_title": [part["title"]]}
|
||||
hints.update({"type": "episode", "expected_series": [part["series"]]} if kind == "series" else {"type": "movie"})
|
||||
part["video"].fps = getFPS(part["video"].streams)
|
||||
scanned_video = scanVideo(part["video"], ignore_all=force_refresh, hints=hints)
|
||||
if not scanned_video:
|
||||
continue
|
||||
|
||||
scanned_video.id = part["id"]
|
||||
ret[scanned_video] = part["video"]
|
||||
return ret
|
||||
|
||||
|
||||
def getItemIDs(media, kind="series"):
|
||||
ids = []
|
||||
if kind == "movies":
|
||||
ids.append(media.id)
|
||||
else:
|
||||
for season in media.seasons:
|
||||
for episode in media.seasons[season].episodes:
|
||||
ids.append(media.seasons[season].episodes[episode].id)
|
||||
|
||||
return ids
|
||||
|
||||
|
||||
def scanVideo(part, ignore_all=False, hints=None):
|
||||
embedded_subtitles = not ignore_all and Prefs['subtitles.scan.embedded']
|
||||
external_subtitles = not ignore_all and Prefs['subtitles.scan.external']
|
||||
|
||||
if ignore_all:
|
||||
Log.Debug("Force refresh intended.")
|
||||
|
||||
Log.Debug("Scanning video: %s, subtitles=%s, embedded_subtitles=%s" % (part.file, external_subtitles, embedded_subtitles))
|
||||
|
||||
try:
|
||||
return subliminal.video.scan_video(part.file, subtitles=external_subtitles, embedded_subtitles=embedded_subtitles, hints=hints or {},
|
||||
video_fps=part.fps)
|
||||
|
||||
except ValueError:
|
||||
Log.Warn("File could not be guessed by subliminal")
|
||||
|
||||
|
||||
def downloadBestSubtitles(video_part_map, min_score=0):
|
||||
def download_best_subtitles(video_part_map, min_score=0):
|
||||
hearing_impaired = Prefs['subtitles.search.hearingImpaired']
|
||||
languages = config.langList
|
||||
languages = config.lang_list
|
||||
if not languages:
|
||||
return
|
||||
|
||||
@@ -190,14 +78,22 @@ def downloadBestSubtitles(video_part_map, min_score=0):
|
||||
for video, part in video_part_map.iteritems():
|
||||
if not Prefs['subtitles.save.filesystem']:
|
||||
# scan for existing metadata subtitles
|
||||
meta_subs = getSubtitlesFromMetadata(part)
|
||||
meta_subs = get_subtitles_from_metadata(part)
|
||||
for language, subList in meta_subs.iteritems():
|
||||
if subList:
|
||||
video.subtitle_languages.add(language)
|
||||
Log.Debug("Found metadata subtitle %s for %s", language, video)
|
||||
|
||||
if not (languages - video.subtitle_languages):
|
||||
Log.Debug('All languages %r exist for %s', languages, video)
|
||||
missing_subs = (languages - video.subtitle_languages)
|
||||
|
||||
# all languages are found if we either really have subs for all languages or we only want to have exactly one language
|
||||
# and we've only found one (the case for a selected language, Prefs['subtitles.only_one'] (one found sub matches any language))
|
||||
found_one_which_is_enough = len(video.subtitle_languages) >= 1 and Prefs['subtitles.only_one']
|
||||
if not missing_subs or found_one_which_is_enough:
|
||||
if found_one_which_is_enough:
|
||||
Log.Debug('Only one language was requested, and we\'ve got a subtitle for %s', video)
|
||||
else:
|
||||
Log.Debug('All languages %r exist for %s', languages, video)
|
||||
continue
|
||||
missing_languages = True
|
||||
break
|
||||
@@ -206,24 +102,41 @@ def downloadBestSubtitles(video_part_map, min_score=0):
|
||||
Log.Debug("Download best subtitles using settings: min_score: %s, hearing_impaired: %s" % (min_score, hearing_impaired))
|
||||
|
||||
return subliminal.api.download_best_subtitles(video_part_map.keys(), languages, min_score, hearing_impaired, providers=config.providers,
|
||||
provider_configs=config.providerSettings)
|
||||
provider_configs=config.provider_settings)
|
||||
Log.Debug("All languages for all requested videos exist. Doing nothing.")
|
||||
|
||||
|
||||
def saveSubtitles(videos, subtitles):
|
||||
def save_subtitles(videos, subtitles):
|
||||
meta_fallback = False
|
||||
save_successful = False
|
||||
storage = "metadata"
|
||||
if Prefs['subtitles.save.filesystem']:
|
||||
Log.Debug("Using filesystem as subtitle storage")
|
||||
saveSubtitlesToFile(subtitles)
|
||||
storage = "filesystem"
|
||||
else:
|
||||
Log.Debug("Using metadata as subtitle storage")
|
||||
saveSubtitlesToMetadata(videos, subtitles)
|
||||
storage = "metadata"
|
||||
try:
|
||||
Log.Debug("Using filesystem as subtitle storage")
|
||||
save_subtitles_to_file(subtitles)
|
||||
except OSError:
|
||||
if Prefs["subtitles.save.metadata_fallback"]:
|
||||
meta_fallback = True
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
save_successful = True
|
||||
|
||||
storeSubtitleInfo(videos, subtitles, storage)
|
||||
if not Prefs['subtitles.save.filesystem'] or meta_fallback:
|
||||
if meta_fallback:
|
||||
Log.Debug("Using metadata as subtitle storage, because filesystem storage failed")
|
||||
else:
|
||||
Log.Debug("Using metadata as subtitle storage")
|
||||
save_successful = save_subtitles_to_metadata(videos, subtitles)
|
||||
|
||||
if save_successful and config.notify_executable:
|
||||
notify_executable(config.notify_executable, videos, subtitles, storage)
|
||||
|
||||
store_subtitle_info(videos, subtitles, storage)
|
||||
|
||||
|
||||
def saveSubtitlesToFile(subtitles):
|
||||
def save_subtitles_to_file(subtitles):
|
||||
fld_custom = Prefs["subtitles.save.subFolder.Custom"].strip() if bool(Prefs["subtitles.save.subFolder.Custom"]) else None
|
||||
|
||||
for video, video_subtitles in subtitles.items():
|
||||
@@ -246,22 +159,24 @@ def saveSubtitlesToFile(subtitles):
|
||||
os.makedirs(fld)
|
||||
subliminal.api.save_subtitles(video, video_subtitles, directory=fld, single=Prefs['subtitles.only_one'],
|
||||
encode_with=force_utf8 if Prefs['subtitles.enforce_encoding'] else None)
|
||||
return True
|
||||
|
||||
|
||||
def saveSubtitlesToMetadata(videos, subtitles):
|
||||
def save_subtitles_to_metadata(videos, subtitles):
|
||||
for video, video_subtitles in subtitles.items():
|
||||
mediaPart = videos[video]
|
||||
for subtitle in video_subtitles:
|
||||
content = force_utf8(subtitle.text) if Prefs['subtitles.enforce_encoding'] else subtitle.content
|
||||
mediaPart.subtitles[Locale.Language.Match(subtitle.language.alpha2)][subtitle.page_link] = Proxy.Media(content, ext="srt")
|
||||
return True
|
||||
|
||||
|
||||
def updateLocalMedia(metadata, media, media_type="movies"):
|
||||
def update_local_media(metadata, media, media_type="movies"):
|
||||
# Look for subtitles
|
||||
if media_type == "movies":
|
||||
for item in media.items:
|
||||
for part in item.parts:
|
||||
support.localmedia.findSubtitles(part)
|
||||
support.localmedia.find_subtitles(part)
|
||||
return
|
||||
|
||||
# Look for subtitles for each episode.
|
||||
@@ -274,7 +189,7 @@ def updateLocalMedia(metadata, media, media_type="movies"):
|
||||
|
||||
# Look for subtitles.
|
||||
for part in i.parts:
|
||||
support.localmedia.findSubtitles(part)
|
||||
support.localmedia.find_subtitles(part)
|
||||
else:
|
||||
pass
|
||||
|
||||
@@ -289,7 +204,7 @@ class SubZeroAgent(object):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SubZeroAgent, self).__init__(*args, **kwargs)
|
||||
self.agent_type = "movies" if isinstance(self, Agent.Movies) else "series"
|
||||
self.name = "Sub-Zero Subtitles (%s, %s)" % (self.agent_type_verbose, config.getVersion())
|
||||
self.name = "Sub-Zero Subtitles (%s, %s)" % (self.agent_type_verbose, config.get_version())
|
||||
|
||||
def search(self, results, media, lang):
|
||||
Log.Debug("Sub-Zero %s, %s search" % (config.version, self.agent_type))
|
||||
@@ -306,17 +221,32 @@ class SubZeroAgent(object):
|
||||
|
||||
item_ids = []
|
||||
try:
|
||||
initSubliminalPatches()
|
||||
parts = parseMediaToParts(media, kind=self.agent_type)
|
||||
init_subliminal_patches()
|
||||
parts = convert_media_to_parts(media, kind=self.agent_type)
|
||||
|
||||
# media ignored?
|
||||
use_any_parts = False
|
||||
for part in parts:
|
||||
if is_ignored(part["id"]):
|
||||
Log.Debug(u"Ignoring %s" % part)
|
||||
continue
|
||||
use_any_parts = True
|
||||
|
||||
if not use_any_parts:
|
||||
Log.Debug(u"Nothing to do.")
|
||||
return
|
||||
|
||||
use_score = Prefs[self.score_prefs_key]
|
||||
scanned_parts = scanParts(parts, kind=self.agent_type)
|
||||
subtitles = downloadBestSubtitles(scanned_parts, min_score=int(use_score))
|
||||
item_ids = getItemIDs(media, kind=self.agent_type)
|
||||
scanned_parts = scan_parts(parts, kind=self.agent_type)
|
||||
subtitles = download_best_subtitles(scanned_parts, min_score=int(use_score))
|
||||
item_ids = get_media_item_ids(media, kind=self.agent_type)
|
||||
|
||||
whack_missing_parts(scanned_parts)
|
||||
|
||||
if subtitles:
|
||||
saveSubtitles(scanned_parts, subtitles)
|
||||
save_subtitles(scanned_parts, subtitles)
|
||||
|
||||
updateLocalMedia(metadata, media, media_type=self.agent_type)
|
||||
update_local_media(metadata, media, media_type=self.agent_type)
|
||||
|
||||
finally:
|
||||
# update the menu state
|
||||
@@ -338,7 +268,7 @@ class SubZeroSubtitlesAgentMovies(SubZeroAgent, Agent.Movies):
|
||||
|
||||
|
||||
class SubZeroSubtitlesAgentTvShows(SubZeroAgent, Agent.TV_Shows):
|
||||
contributes_to = ['com.plexapp.agents.thetvdb', 'com.plexapp.agents.thetvdbdvdorder', 'com.plexapp.agents.xbmcnfotv',
|
||||
'com.plexapp.agents.hama']
|
||||
contributes_to = ['com.plexapp.agents.thetvdb', 'com.plexapp.agents.themoviedb',
|
||||
'com.plexapp.agents.thetvdbdvdorder', 'com.plexapp.agents.xbmcnfotv', 'com.plexapp.agents.hama']
|
||||
score_prefs_key = "subtitles.search.minimumTVScore"
|
||||
agent_type_verbose = "TV"
|
||||
|
||||
@@ -1,24 +1,28 @@
|
||||
# coding=utf-8
|
||||
import logging
|
||||
|
||||
import logger
|
||||
from menu_helpers import add_ignore_options, dig_tree, set_refresh_menu_state, should_display_ignore, enable_channel_wrapper
|
||||
|
||||
from menu_helpers import add_ignore_options, dig_tree, set_refresh_menu_state, \
|
||||
should_display_ignore, enable_channel_wrapper, default_thumb, debounce
|
||||
from subzero.constants import TITLE, ART, ICON, PREFIX, PLUGIN_IDENTIFIER, DEPENDENCY_MODULE_NAMES
|
||||
from support.auth import refresh_plex_token
|
||||
from support.background import scheduler
|
||||
from support.config import config
|
||||
from support.helpers import pad_title, timestamp
|
||||
from support.ignore import ignore_list
|
||||
from support.items import getOnDeckItems, refreshItem, getAllItems
|
||||
from support.items import getRecentItems, get_items_info
|
||||
from support.lib import Plex, lib_unaccessible_error
|
||||
from support.missing_subtitles import getAllMissing
|
||||
from support.storage import resetStorage, logStorage
|
||||
from support.items import get_item, get_on_deck_items, refresh_item, get_all_items, get_recent_items, get_items_info, get_item_thumb
|
||||
from support.lib import Plex
|
||||
from support.missing_subtitles import items_get_all_missing_subs
|
||||
from support.storage import reset_storage, log_storage, get_subtitle_info
|
||||
from support.plex_media import scan_parts
|
||||
|
||||
# init GUI
|
||||
ObjectContainer.art = R(ART)
|
||||
ObjectContainer.no_cache = True
|
||||
|
||||
# default thumb for DirectoryObjects
|
||||
DirectoryObject.thumb = default_thumb
|
||||
|
||||
|
||||
# noinspection PyUnboundLocalVariable
|
||||
route = enable_channel_wrapper(route)
|
||||
# noinspection PyUnboundLocalVariable
|
||||
@@ -33,22 +37,14 @@ def fatality(randomize=None, force_title=None, header=None, message=None, only_r
|
||||
"""
|
||||
title = force_title if force_title is not None else config.full_version
|
||||
oc = ObjectContainer(title1=title, title2=None, header=unicode(header) if header else header, message=message, no_history=no_history,
|
||||
replace_parent=replace_parent)
|
||||
replace_parent=replace_parent, no_cache=True)
|
||||
|
||||
if not config.plex_api_working:
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(fatality, randomize=timestamp()),
|
||||
title=pad_title("PMS API ERROR"),
|
||||
summary=lib_unaccessible_error
|
||||
))
|
||||
return oc
|
||||
|
||||
if not config.permissions_ok:
|
||||
if not config.permissions_ok and config.missing_permissions:
|
||||
for title, path in config.missing_permissions:
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(fatality, randomize=timestamp()),
|
||||
title=pad_title("Insufficient permissions"),
|
||||
summary="Insufficient permissions on library %s, folder: %s" % (title, path)
|
||||
summary="Insufficient permissions on library %s, folder: %s" % (title, path),
|
||||
))
|
||||
return oc
|
||||
|
||||
@@ -65,7 +61,7 @@ def fatality(randomize=None, force_title=None, header=None, message=None, only_r
|
||||
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(OnDeckMenu),
|
||||
title=pad_title("On Deck items"),
|
||||
title="On Deck items",
|
||||
summary="Shows the current on deck items and allows you to individually (force-) refresh their metadata/subtitles."
|
||||
))
|
||||
oc.add(DirectoryObject(
|
||||
@@ -92,7 +88,7 @@ def fatality(randomize=None, force_title=None, header=None, message=None, only_r
|
||||
str(task.last_run_time).split(".")[0])
|
||||
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(RefreshMissing),
|
||||
key=Callback(RefreshMissing, randomize=timestamp()),
|
||||
title="Search for missing subtitles (in recently-added items, max-age: %s)" % Prefs["scheduler.item_is_recent_age"],
|
||||
summary="Automatically run periodically by the scheduler, if configured. %s" % task_state
|
||||
))
|
||||
@@ -124,42 +120,71 @@ def fatality(randomize=None, force_title=None, header=None, message=None, only_r
|
||||
|
||||
@route(PREFIX + '/on_deck')
|
||||
def OnDeckMenu(message=None):
|
||||
return mergedItemsMenu(title="Items On Deck", base_title="Items On Deck", itemGetter=getOnDeckItems)
|
||||
"""
|
||||
displays the items on deck
|
||||
:param message:
|
||||
:return:
|
||||
"""
|
||||
return mergedItemsMenu(title="Items On Deck", base_title="Items On Deck", itemGetter=get_on_deck_items)
|
||||
|
||||
|
||||
@route(PREFIX + '/recent')
|
||||
def RecentlyAddedMenu(message=None):
|
||||
"""
|
||||
displays the recently added items with missing subtitles
|
||||
:param message:
|
||||
:return:
|
||||
"""
|
||||
return recentItemsMenu(title="Missing Subtitles", base_title="Missing Subtitles")
|
||||
|
||||
|
||||
def recentItemsMenu(title, base_title=None):
|
||||
oc = ObjectContainer(title2=title, no_cache=True, no_history=True)
|
||||
recent_items = getRecentItems()
|
||||
recent_items = get_recent_items()
|
||||
if recent_items:
|
||||
missing_items = getAllMissing(recent_items)
|
||||
missing_items = items_get_all_missing_subs(recent_items)
|
||||
if missing_items:
|
||||
for added_at, item_id, title in missing_items:
|
||||
for added_at, item_id, title, item in missing_items:
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(RefreshItemMenu, title=base_title + " > " + title, item_title=title, rating_key=item_id), title=title
|
||||
key=Callback(ItemDetailsMenu, title=base_title + " > " + title, item_title=title, rating_key=item_id),
|
||||
title=title,
|
||||
thumb=get_item_thumb(item) or default_thumb
|
||||
))
|
||||
|
||||
return oc
|
||||
|
||||
|
||||
def mergedItemsMenu(title, itemGetter, itemGetterKwArgs=None, base_title=None, *args, **kwargs):
|
||||
"""
|
||||
displays an item list of dynamic kinds of items
|
||||
:param title:
|
||||
:param itemGetter:
|
||||
:param itemGetterKwArgs:
|
||||
:param base_title:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return:
|
||||
"""
|
||||
oc = ObjectContainer(title2=title, no_cache=True, no_history=True)
|
||||
items = itemGetter(*args, **kwargs)
|
||||
|
||||
for kind, title, item_id, deeper, item in items:
|
||||
oc.add(DirectoryObject(
|
||||
title=title,
|
||||
key=Callback(RefreshItemMenu, title=base_title + " > " + title, item_title=title, rating_key=item_id)
|
||||
key=Callback(ItemDetailsMenu, title=base_title + " > " + title, item_title=title, rating_key=item_id),
|
||||
thumb=get_item_thumb(item) or default_thumb
|
||||
))
|
||||
|
||||
return oc
|
||||
|
||||
|
||||
def determine_section_display(kind, item):
|
||||
"""
|
||||
returns the menu function for a section based on the size of it (amount of items)
|
||||
:param kind:
|
||||
:param item:
|
||||
:return:
|
||||
"""
|
||||
if item.size > 200:
|
||||
return SectionFirstLetterMenu
|
||||
return SectionMenu
|
||||
@@ -167,13 +192,22 @@ def determine_section_display(kind, item):
|
||||
|
||||
@route(PREFIX + '/ignore/set/{kind}/{rating_key}/{todo}/sure={sure}', kind=str, rating_key=str, todo=str, sure=bool)
|
||||
def IgnoreMenu(kind, rating_key, title=None, sure=False, todo="not_set"):
|
||||
"""
|
||||
displays the ignore options for a menu
|
||||
:param kind:
|
||||
:param rating_key:
|
||||
:param title:
|
||||
:param sure:
|
||||
:param todo:
|
||||
:return:
|
||||
"""
|
||||
is_ignored = rating_key in ignore_list[kind]
|
||||
if not sure:
|
||||
oc = ObjectContainer(no_history=True, replace_parent=True, title1="%s %s %s %s the ignore list" % (
|
||||
"Add" if not is_ignored else "Remove", ignore_list.verbose(kind), title, "to" if not is_ignored else "from"), title2="Are you sure?")
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(IgnoreMenu, kind=kind, rating_key=rating_key, title=title, sure=True, todo="add" if not is_ignored else "remove"),
|
||||
title=pad_title("Are you sure?")
|
||||
title=pad_title("Are you sure?"),
|
||||
))
|
||||
return oc
|
||||
|
||||
@@ -208,7 +242,11 @@ def IgnoreMenu(kind, rating_key, title=None, sure=False, todo="not_set"):
|
||||
|
||||
@route(PREFIX + '/sections')
|
||||
def SectionsMenu():
|
||||
items = getAllItems("sections")
|
||||
"""
|
||||
displays the menu for all sections
|
||||
:return:
|
||||
"""
|
||||
items = get_all_items("sections")
|
||||
|
||||
return dig_tree(ObjectContainer(title2="Sections", no_cache=True, no_history=True), items, None,
|
||||
menu_determination_callback=determine_section_display, pass_kwargs={"base_title": "Sections"},
|
||||
@@ -217,7 +255,16 @@ def SectionsMenu():
|
||||
|
||||
@route(PREFIX + '/section', ignore_options=bool)
|
||||
def SectionMenu(rating_key, title=None, base_title=None, section_title=None, ignore_options=True):
|
||||
items = getAllItems(key="all", value=rating_key, base="library/sections")
|
||||
"""
|
||||
displays the contents of a section
|
||||
:param rating_key:
|
||||
:param title:
|
||||
:param base_title:
|
||||
:param section_title:
|
||||
:param ignore_options:
|
||||
:return:
|
||||
"""
|
||||
items = get_all_items(key="all", value=rating_key, base="library/sections")
|
||||
|
||||
kind, deeper = get_items_info(items)
|
||||
title = unicode(title)
|
||||
@@ -235,7 +282,15 @@ def SectionMenu(rating_key, title=None, base_title=None, section_title=None, ign
|
||||
|
||||
@route(PREFIX + '/section/firstLetter', deeper=bool)
|
||||
def SectionFirstLetterMenu(rating_key, title=None, base_title=None, section_title=None):
|
||||
items = getAllItems(key="first_character", value=rating_key, base="library/sections")
|
||||
"""
|
||||
displays the contents of a section indexed by its first char (A-Z, 0-9...)
|
||||
:param rating_key:
|
||||
:param title:
|
||||
:param base_title:
|
||||
:param section_title:
|
||||
:return:
|
||||
"""
|
||||
items = get_all_items(key="first_character", value=rating_key, base="library/sections")
|
||||
|
||||
kind, deeper = get_items_info(items)
|
||||
|
||||
@@ -257,7 +312,7 @@ def SectionFirstLetterMenu(rating_key, title=None, base_title=None, section_titl
|
||||
def FirstLetterMetadataMenu(rating_key, key, title=None, base_title=None, display_items=False, previous_item_type=None,
|
||||
previous_rating_key=None):
|
||||
"""
|
||||
|
||||
displays the contents of a section filtered by the first letter
|
||||
:param rating_key: actually is the section's key
|
||||
:param key: the firstLetter wanted
|
||||
:param title: the first letter, or #
|
||||
@@ -267,7 +322,7 @@ def FirstLetterMetadataMenu(rating_key, key, title=None, base_title=None, displa
|
||||
title = base_title + " > " + unicode(title)
|
||||
oc = ObjectContainer(title2=title, no_cache=True, no_history=True)
|
||||
|
||||
items = getAllItems(key="first_character", value=[rating_key, key], base="library/sections", flat=False)
|
||||
items = get_all_items(key="first_character", value=[rating_key, key], base="library/sections", flat=False)
|
||||
kind, deeper = get_items_info(items)
|
||||
dig_tree(oc, items, MetadataMenu,
|
||||
pass_kwargs={"base_title": title, "display_items": deeper, "previous_item_type": kind, "previous_rating_key": rating_key})
|
||||
@@ -276,21 +331,45 @@ def FirstLetterMetadataMenu(rating_key, key, title=None, base_title=None, displa
|
||||
|
||||
@route(PREFIX + '/section/contents', display_items=bool)
|
||||
def MetadataMenu(rating_key, title=None, base_title=None, display_items=False, previous_item_type=None, previous_rating_key=None):
|
||||
"""
|
||||
displays the contents of a section based on whether it has a deeper tree or not (movies->movie (item) list; series->series list)
|
||||
:param rating_key:
|
||||
:param title:
|
||||
:param base_title:
|
||||
:param display_items:
|
||||
:param previous_item_type:
|
||||
:param previous_rating_key:
|
||||
:return:
|
||||
"""
|
||||
title = unicode(title)
|
||||
item_title = title
|
||||
title = base_title + " > " + title
|
||||
oc = ObjectContainer(title2=title, no_cache=True, no_history=True)
|
||||
|
||||
if display_items:
|
||||
items = getAllItems(key="children", value=rating_key, base="library/metadata")
|
||||
items = get_all_items(key="children", value=rating_key, base="library/metadata")
|
||||
kind, deeper = get_items_info(items)
|
||||
dig_tree(oc, items, MetadataMenu,
|
||||
pass_kwargs={"base_title": title, "display_items": deeper, "previous_item_type": kind, "previous_rating_key": rating_key})
|
||||
# we don't know exactly where we are here, only add ignore option to series
|
||||
if should_display_ignore(items, previous=previous_item_type):
|
||||
add_ignore_options(oc, "series", title=item_title, rating_key=rating_key, callback_menu=IgnoreMenu)
|
||||
|
||||
# add refresh
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(RefreshItem, rating_key=rating_key, item_title=item_title, refresh_kind=kind, previous_rating_key=previous_rating_key,
|
||||
timeout=16000, randomize=timestamp()),
|
||||
title=u"Refresh: %s" % item_title,
|
||||
summary="Refreshes the item, possibly picking up new subtitles on disk"
|
||||
))
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(RefreshItem, rating_key=rating_key, item_title=item_title, force=True, refresh_kind=kind,
|
||||
previous_rating_key=previous_rating_key, timeout=16000),
|
||||
title=u"Force-Refresh: %s" % item_title,
|
||||
summary="Issues a forced refresh, ignoring known subtitles and searching for new ones"
|
||||
))
|
||||
else:
|
||||
return RefreshItemMenu(rating_key=rating_key, title=title, item_title=item_title)
|
||||
return ItemDetailsMenu(rating_key=rating_key, title=title, item_title=item_title)
|
||||
|
||||
return oc
|
||||
|
||||
@@ -306,18 +385,31 @@ def IgnoreListMenu():
|
||||
|
||||
|
||||
@route(PREFIX + '/item/{rating_key}/actions')
|
||||
def RefreshItemMenu(rating_key, title=None, base_title=None, item_title=None, came_from="/recent"):
|
||||
def ItemDetailsMenu(rating_key, title=None, base_title=None, item_title=None, randomize=None):
|
||||
"""
|
||||
displays the item details menu of an item that doesn't contain any deeper tree, such as a movie or an episode
|
||||
:param rating_key:
|
||||
:param title:
|
||||
:param base_title:
|
||||
:param item_title:
|
||||
:param randomize:
|
||||
:return:
|
||||
"""
|
||||
title = unicode(base_title) + " > " + unicode(title) if base_title else unicode(title)
|
||||
item = get_item(rating_key)
|
||||
|
||||
oc = ObjectContainer(title2=title, replace_parent=True)
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(RefreshItem, rating_key=rating_key, item_title=item_title),
|
||||
key=Callback(RefreshItem, rating_key=rating_key, item_title=item_title, randomize=timestamp()),
|
||||
title=u"Refresh: %s" % item_title,
|
||||
summary="Refreshes the item, possibly picking up new subtitles on disk"
|
||||
summary="Refreshes the item, possibly picking up new subtitles on disk",
|
||||
thumb=item.thumb or default_thumb
|
||||
))
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(RefreshItem, rating_key=rating_key, item_title=item_title, force=True),
|
||||
key=Callback(RefreshItem, rating_key=rating_key, item_title=item_title, force=True, randomize=timestamp()),
|
||||
title=u"Force-Refresh: %s" % item_title,
|
||||
summary="Issues a forced refresh, ignoring known subtitles and searching for new ones"
|
||||
summary="Issues a forced refresh, ignoring known subtitles and searching for new ones",
|
||||
thumb=item.thumb or default_thumb
|
||||
))
|
||||
add_ignore_options(oc, "videos", title=item_title, rating_key=rating_key, callback_menu=IgnoreMenu)
|
||||
|
||||
@@ -325,18 +417,26 @@ def RefreshItemMenu(rating_key, title=None, base_title=None, item_title=None, ca
|
||||
|
||||
|
||||
@route(PREFIX + '/item/{rating_key}')
|
||||
def RefreshItem(rating_key=None, came_from="/recent", item_title=None, force=False):
|
||||
@debounce
|
||||
def RefreshItem(rating_key=None, came_from="/recent", item_title=None, force=False, refresh_kind=None, previous_rating_key=None, timeout=8000, randomize=None, trigger=True):
|
||||
assert rating_key
|
||||
set_refresh_menu_state(u"Triggering %sRefresh for %s" % ("Force-" if force else "", item_title))
|
||||
Thread.Create(refreshItem, rating_key=rating_key, force=force)
|
||||
return fatality(randomize=timestamp(), header=u"%s of item %s triggered" % ("Refresh" if not force else "Forced-refresh", rating_key),
|
||||
replace_parent=True)
|
||||
header = " "
|
||||
if trigger:
|
||||
set_refresh_menu_state(u"Triggering %sRefresh for %s" % ("Force-" if force else "", item_title))
|
||||
Thread.Create(refresh_item, rating_key=rating_key, force=force, refresh_kind=refresh_kind, parent_rating_key=previous_rating_key,
|
||||
timeout=int(timeout))
|
||||
header = u"%s of item %s triggered" % ("Refresh" if not force else "Forced-refresh", rating_key)
|
||||
return fatality(randomize=timestamp(), header=header, replace_parent=True)
|
||||
|
||||
|
||||
@route(PREFIX + '/missing/refresh')
|
||||
def RefreshMissing(randomize=None):
|
||||
Thread.CreateTimer(1.0, lambda: scheduler.run_task("searchAllRecentlyAddedMissing"))
|
||||
return fatality(header="Refresh of recently added items with missing subtitles triggered", replace_parent=True)
|
||||
@debounce
|
||||
def RefreshMissing(randomize=None, trigger=True):
|
||||
header = " "
|
||||
if trigger:
|
||||
Thread.CreateTimer(1.0, lambda: scheduler.run_task("searchAllRecentlyAddedMissing"))
|
||||
header = "Refresh of recently added items with missing subtitles triggered"
|
||||
return fatality(header=header, replace_parent=True)
|
||||
|
||||
|
||||
@route(PREFIX + '/advanced')
|
||||
@@ -345,36 +445,32 @@ def AdvancedMenu(randomize=None, header=None, message=None):
|
||||
replace_parent=True, title2="Advanced")
|
||||
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(TriggerRestart),
|
||||
title=pad_title("Restart the plugin")
|
||||
))
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(RefreshToken, randomize=timestamp()),
|
||||
title=pad_title("Re-request the API token from plex.tv")
|
||||
key=Callback(TriggerRestart, randomize=timestamp()),
|
||||
title=pad_title("Restart the plugin"),
|
||||
))
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(LogStorage, key="tasks", randomize=timestamp()),
|
||||
title=pad_title("Log the plugin's scheduled tasks state storage")
|
||||
title=pad_title("Log the plugin's scheduled tasks state storage"),
|
||||
))
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(LogStorage, key="subs", randomize=timestamp()),
|
||||
title=pad_title("Log the plugin's internal subtitle information storage")
|
||||
title=pad_title("Log the plugin's internal subtitle information storage"),
|
||||
))
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(LogStorage, key="ignore", randomize=timestamp()),
|
||||
title=pad_title("Log the plugin's internal ignorelist storage")
|
||||
title=pad_title("Log the plugin's internal ignorelist storage"),
|
||||
))
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(ResetStorage, key="tasks", randomize=timestamp()),
|
||||
title=pad_title("Reset the plugin's scheduled tasks state storage")
|
||||
title=pad_title("Reset the plugin's scheduled tasks state storage"),
|
||||
))
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(ResetStorage, key="subs", randomize=timestamp()),
|
||||
title=pad_title("Reset the plugin's internal subtitle information storage")
|
||||
title=pad_title("Reset the plugin's internal subtitle information storage"),
|
||||
))
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(ResetStorage, key="ignore", randomize=timestamp()),
|
||||
title=pad_title("Reset the plugin's internal ignorelist storage")
|
||||
title=pad_title("Reset the plugin's internal ignorelist storage"),
|
||||
))
|
||||
return oc
|
||||
|
||||
@@ -414,7 +510,7 @@ def ValidatePrefs():
|
||||
Log.Debug("Stop logging to console")
|
||||
|
||||
Log.Debug("Setting log-level to %s", Prefs["log_level"])
|
||||
logger.registerLoggingHander(DEPENDENCY_MODULE_NAMES, level=Prefs["log_level"])
|
||||
logger.register_logging_handler(DEPENDENCY_MODULE_NAMES, level=Prefs["log_level"])
|
||||
Core.log.setLevel(logging.getLevelName(Prefs["log_level"]))
|
||||
|
||||
return
|
||||
@@ -425,11 +521,13 @@ def DispatchRestart():
|
||||
|
||||
|
||||
@route(PREFIX + '/advanced/restart/trigger')
|
||||
def TriggerRestart(randomize=None):
|
||||
set_refresh_menu_state("Restarting the plugin")
|
||||
DispatchRestart()
|
||||
@debounce
|
||||
def TriggerRestart(randomize=None, trigger=True):
|
||||
if trigger:
|
||||
set_refresh_menu_state("Restarting the plugin")
|
||||
DispatchRestart()
|
||||
return fatality(header="Restart triggered, please wait about 5 seconds", force_title=" ", only_refresh=True, replace_parent=True,
|
||||
no_history=True)
|
||||
no_history=True, randomize=timestamp())
|
||||
|
||||
|
||||
@route(PREFIX + '/advanced/restart/execute')
|
||||
@@ -443,11 +541,12 @@ def ResetStorage(key, randomize=None, sure=False):
|
||||
oc = ObjectContainer(no_history=True, title1="Reset subtitle storage", title2="Are you sure?")
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(ResetStorage, key=key, sure=True, randomize=timestamp()),
|
||||
title=pad_title("Are you really sure?")
|
||||
title=pad_title("Are you really sure?"),
|
||||
|
||||
))
|
||||
return oc
|
||||
|
||||
resetStorage(key)
|
||||
reset_storage(key)
|
||||
|
||||
if key == "tasks":
|
||||
# reinitialize the scheduler
|
||||
@@ -463,20 +562,9 @@ def ResetStorage(key, randomize=None, sure=False):
|
||||
|
||||
@route(PREFIX + '/storage/log')
|
||||
def LogStorage(key, randomize=None):
|
||||
logStorage(key)
|
||||
log_storage(key)
|
||||
return AdvancedMenu(
|
||||
randomize=timestamp(),
|
||||
header='Success',
|
||||
message='Information Storage (%s) logged' % key
|
||||
)
|
||||
|
||||
|
||||
@route(PREFIX + '/refresh_token')
|
||||
def RefreshToken(randomize=None):
|
||||
result = refresh_plex_token()
|
||||
if result:
|
||||
msg = "Token successfully refreshed."
|
||||
else:
|
||||
msg = "Couldn't refresh the token, please check your credentials"
|
||||
|
||||
return AdvancedMenu(header=msg)
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
# coding=utf-8
|
||||
import types
|
||||
|
||||
from support.items import get_kind
|
||||
from support.items import get_kind, get_item_thumb
|
||||
from subzero import intent
|
||||
from support.helpers import format_video
|
||||
from support.ignore import ignore_list
|
||||
from subzero.constants import ICON
|
||||
from subzero.func import debouncer
|
||||
|
||||
default_thumb = R(ICON)
|
||||
|
||||
|
||||
def should_display_ignore(items, previous=None):
|
||||
@@ -42,8 +46,11 @@ def add_ignore_options(oc, kind, callback_menu=None, title=None, rating_key=None
|
||||
)
|
||||
|
||||
|
||||
def dig_tree(oc, items, menu_callback, menu_determination_callback=None, force_rating_key=None, fill_args=None, pass_kwargs=None):
|
||||
def dig_tree(oc, items, menu_callback, menu_determination_callback=None, force_rating_key=None, fill_args=None, pass_kwargs=None,
|
||||
thumb=default_thumb):
|
||||
for kind, title, key, dig_deeper, item in items:
|
||||
thumb = get_item_thumb(item) or thumb
|
||||
|
||||
add_kwargs = {}
|
||||
if fill_args:
|
||||
add_kwargs = dict((name, getattr(item, k)) for k, name in fill_args.iteritems() if item and hasattr(item, k))
|
||||
@@ -53,7 +60,7 @@ def dig_tree(oc, items, menu_callback, menu_determination_callback=None, force_r
|
||||
oc.add(DirectoryObject(
|
||||
key=Callback(menu_callback or menu_determination_callback(kind, item), title=title, rating_key=force_rating_key or key,
|
||||
**add_kwargs),
|
||||
title=title
|
||||
title=title, thumb=thumb
|
||||
))
|
||||
return oc
|
||||
|
||||
@@ -113,3 +120,21 @@ def enable_channel_wrapper(func):
|
||||
return (func if Prefs["enable_channel"] or enforce_route else noop)(*args, **kwargs)
|
||||
|
||||
return wrap
|
||||
|
||||
|
||||
def debounce(func):
|
||||
"""
|
||||
prevent func from being called twice with the same arguments
|
||||
:param func:
|
||||
:return:
|
||||
"""
|
||||
def wrap(*args, **kwargs):
|
||||
if "randomize" in kwargs:
|
||||
if ([func] + list(args), kwargs) in debouncer:
|
||||
kwargs["trigger"] = False
|
||||
Log.Debug("not triggering %s twice with %s, %s" % (func, args, kwargs))
|
||||
else:
|
||||
debouncer.add([func] + list(args), kwargs)
|
||||
return func(*args, **kwargs)
|
||||
|
||||
return wrap
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import logging
|
||||
|
||||
|
||||
def registerLoggingHander(dependencies, level="ERROR"):
|
||||
plexHandler = PlexLoggerHandler()
|
||||
def register_logging_handler(dependencies, level="ERROR"):
|
||||
plex_handler = PlexLoggerHandler()
|
||||
for dependency in dependencies:
|
||||
Log.Debug("Registering LoggerHandler for dependency: %s" % dependency)
|
||||
log = logging.getLogger(dependency)
|
||||
@@ -13,7 +13,7 @@ def registerLoggingHander(dependencies, level="ERROR"):
|
||||
log.removeHandler(handler)
|
||||
|
||||
log.setLevel(level)
|
||||
log.addHandler(plexHandler)
|
||||
log.addHandler(plex_handler)
|
||||
|
||||
|
||||
class PlexLoggerHandler(logging.StreamHandler):
|
||||
|
||||
@@ -13,6 +13,9 @@ import lib
|
||||
|
||||
sys.modules["support.lib"] = lib
|
||||
|
||||
import plex_media
|
||||
sys.modules["support.plex_media"] = plex_media
|
||||
|
||||
import localmedia
|
||||
|
||||
sys.modules["subzero.localmedia"] = localmedia
|
||||
@@ -41,10 +44,6 @@ import storage
|
||||
|
||||
sys.modules["support.storage"] = storage
|
||||
|
||||
import auth
|
||||
|
||||
sys.modules["support.auth"] = auth
|
||||
|
||||
import ignore
|
||||
|
||||
sys.modules["support.ignore"] = ignore
|
||||
|
||||
@@ -3,12 +3,11 @@
|
||||
import os
|
||||
import re
|
||||
import inspect
|
||||
|
||||
from babelfish import Language
|
||||
from subzero.lib.io import FileIO
|
||||
from subzero.constants import PLUGIN_NAME
|
||||
from auth import refresh_plex_token
|
||||
from lib import configure_plex, Plex
|
||||
from subzero.lib.io import FileIO, get_viable_encoding
|
||||
from subzero.constants import PLUGIN_NAME, PLUGIN_IDENTIFIER, MOVIE, SHOW
|
||||
from lib import Plex
|
||||
from helpers import check_write_permissions
|
||||
|
||||
SUBTITLE_EXTS = ['utf', 'utf8', 'utf-8', 'srt', 'smi', 'rt', 'ssa', 'aqt', 'jss', 'ass', 'idx', 'sub', 'txt', 'psb']
|
||||
VIDEO_EXTS = ['3g2', '3gp', 'asf', 'asx', 'avc', 'avi', 'avs', 'bivx', 'bup', 'divx', 'dv', 'dvr-ms', 'evo', 'fli', 'flv',
|
||||
@@ -30,66 +29,70 @@ def int_or_default(s, default):
|
||||
|
||||
class Config(object):
|
||||
version = None
|
||||
langList = None
|
||||
subtitleDestinationFolder = None
|
||||
full_version = None
|
||||
lang_list = None
|
||||
subtitle_destination_folder = None
|
||||
providers = None
|
||||
providerSettings = None
|
||||
provider_settings = None
|
||||
max_recent_items_per_library = 200
|
||||
plex_api_working = False
|
||||
permissions_ok = False
|
||||
missing_permissions = None
|
||||
ignore_paths = None
|
||||
fs_encoding = None
|
||||
notify_executable = None
|
||||
sections = None
|
||||
enabled_sections = None
|
||||
|
||||
initialized = False
|
||||
|
||||
def initialize(self):
|
||||
self.version = self.getVersion()
|
||||
self.fs_encoding = get_viable_encoding()
|
||||
self.version = self.get_version()
|
||||
self.full_version = u"%s %s" % (PLUGIN_NAME, self.version)
|
||||
self.langList = self.getLangList()
|
||||
self.subtitleDestinationFolder = self.getSubtitleDestinationFolder()
|
||||
self.providers = self.getProviders()
|
||||
self.providerSettings = self.getProviderSettings()
|
||||
self.lang_list = self.get_lang_list()
|
||||
self.subtitle_destination_folder = self.get_subtitle_destination_folder()
|
||||
self.providers = self.get_providers()
|
||||
self.provider_settings = self.get_provider_settings()
|
||||
self.max_recent_items_per_library = int_or_default(Prefs["scheduler.max_recent_items_per_library"], 200)
|
||||
self.initialized = True
|
||||
configure_plex()
|
||||
self.plex_api_working = self.checkPlexAPI()
|
||||
self.sections = list(Plex["library"].sections())
|
||||
self.missing_permissions = []
|
||||
self.permissions_ok = self.checkPermissions()
|
||||
self.ignore_paths = self.parse_ignore_paths()
|
||||
self.permissions_ok = self.check_permissions()
|
||||
self.notify_executable = self.check_notify_executable()
|
||||
self.enabled_sections = self.check_enabled_sections()
|
||||
self.initialized = True
|
||||
|
||||
def checkPlexAPI(self):
|
||||
return bool(Plex["library"].sections())
|
||||
|
||||
def checkPermissions(self):
|
||||
def check_permissions(self):
|
||||
if not Prefs["subtitles.save.filesystem"] or not Prefs["check_permissions"]:
|
||||
return True
|
||||
|
||||
if not self.plex_api_working:
|
||||
return
|
||||
|
||||
use_ignore_fs = Prefs["subtitles.ignore_fs"]
|
||||
sections = Plex["library"].sections()
|
||||
all_permissions_ok = True
|
||||
for section in list(sections):
|
||||
for section in self.sections:
|
||||
title = section.title
|
||||
for location in section:
|
||||
path_str = location.path
|
||||
if isinstance(path_str, unicode):
|
||||
path_str = path_str.encode(self.fs_encoding)
|
||||
|
||||
if use_ignore_fs:
|
||||
ignore = False
|
||||
# check whether we've got an ignore file inside the section path
|
||||
for ifn in IGNORE_FN:
|
||||
if os.path.isfile(os.path.join(location.path, ifn)):
|
||||
ignore = True
|
||||
if ignore:
|
||||
if self.is_physically_ignored(path_str):
|
||||
continue
|
||||
|
||||
if self.is_path_ignored(path_str):
|
||||
# is the path in our ignored paths setting?
|
||||
continue
|
||||
|
||||
# section not ignored, check for write permissions
|
||||
if not os.access(location.path, os.W_OK | os.X_OK):
|
||||
if not check_write_permissions(path_str):
|
||||
# not enough permissions
|
||||
self.missing_permissions.append((title, location.path))
|
||||
all_permissions_ok = False
|
||||
|
||||
return all_permissions_ok
|
||||
|
||||
|
||||
def getVersion(self):
|
||||
def get_version(self):
|
||||
curDir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
|
||||
info_file_path = os.path.abspath(os.path.join(curDir, "..", "..", "Info.plist"))
|
||||
data = FileIO.read(info_file_path)
|
||||
@@ -97,13 +100,71 @@ class Config(object):
|
||||
if result:
|
||||
return result.group(1)
|
||||
|
||||
def getBlacklist(self, key):
|
||||
return map(lambda id: id.strip(), (Prefs[key] or "").split(","))
|
||||
def parse_ignore_paths(self):
|
||||
paths = Prefs["subtitles.ignore_paths"]
|
||||
if paths:
|
||||
try:
|
||||
return [path.strip() for path in paths.split(",")]
|
||||
except:
|
||||
Log.Error("Couldn't parse your ignore paths settings: %s" % paths)
|
||||
return []
|
||||
|
||||
def is_physically_ignored(self, folder):
|
||||
# check whether we've got an ignore file inside the path
|
||||
for ifn in IGNORE_FN:
|
||||
if os.path.isfile(os.path.join(folder, ifn)):
|
||||
Log.Info(u'Ignoring "%s" because "%s" exists', folder, ifn)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def is_path_ignored(self, fn):
|
||||
for path in self.ignore_paths:
|
||||
if fn.startswith(path):
|
||||
return True
|
||||
return False
|
||||
|
||||
def check_notify_executable(self):
|
||||
fn = Prefs["notify_executable"]
|
||||
if not fn:
|
||||
return
|
||||
|
||||
splitted_fn = fn.split()
|
||||
exe_fn = splitted_fn[0]
|
||||
arguments = [arg.strip() for arg in splitted_fn[1:]]
|
||||
|
||||
if os.path.isfile(exe_fn) and os.access(exe_fn, os.X_OK):
|
||||
return exe_fn, arguments
|
||||
Log.Error("Notify executable not existing or not executable: %s" % exe_fn)
|
||||
|
||||
def check_enabled_sections(self):
|
||||
enabled_for_primary_agents = []
|
||||
enabled_sections = {}
|
||||
|
||||
# find which agents we're enabled for
|
||||
for agent in Plex.agents():
|
||||
if not agent.primary:
|
||||
continue
|
||||
|
||||
for t in list(agent.media_types):
|
||||
if t.media_type in (MOVIE, SHOW):
|
||||
related_agents = Plex.primary_agent(agent.identifier, t.media_type)
|
||||
for a in related_agents:
|
||||
if a.identifier == PLUGIN_IDENTIFIER and a.enabled:
|
||||
enabled_for_primary_agents.append(agent.identifier)
|
||||
|
||||
# find the libraries that use them
|
||||
for library in self.sections:
|
||||
if library.agent in enabled_for_primary_agents:
|
||||
enabled_sections[library.key] = library
|
||||
|
||||
Log.Debug(u"I'm enabled for: %s" % [lib.title for key, lib in enabled_sections.iteritems()])
|
||||
return enabled_sections
|
||||
|
||||
# Prepare a list of languages we want subs for
|
||||
def getLangList(self):
|
||||
def get_lang_list(self):
|
||||
l = {Language.fromietf(Prefs["langPref1"])}
|
||||
langCustom = Prefs["langPrefCustom"].strip()
|
||||
lang_custom = Prefs["langPrefCustom"].strip()
|
||||
|
||||
if Prefs['subtitles.only_one']:
|
||||
return l
|
||||
@@ -114,8 +175,8 @@ class Config(object):
|
||||
if Prefs["langPref3"] != "None":
|
||||
l.update({Language.fromietf(Prefs["langPref3"])})
|
||||
|
||||
if len(langCustom) and langCustom != "None":
|
||||
for lang in langCustom.split(u","):
|
||||
if len(lang_custom) and lang_custom != "None":
|
||||
for lang in lang_custom.split(u","):
|
||||
lang = lang.strip()
|
||||
try:
|
||||
real_lang = Language.fromietf(lang)
|
||||
@@ -128,14 +189,14 @@ class Config(object):
|
||||
|
||||
return l
|
||||
|
||||
def getSubtitleDestinationFolder(self):
|
||||
def get_subtitle_destination_folder(self):
|
||||
if not Prefs["subtitles.save.filesystem"]:
|
||||
return
|
||||
|
||||
fld_custom = Prefs["subtitles.save.subFolder.Custom"].strip() if bool(Prefs["subtitles.save.subFolder.Custom"]) else None
|
||||
return fld_custom or (Prefs["subtitles.save.subFolder"] if Prefs["subtitles.save.subFolder"] != "current folder" else None)
|
||||
|
||||
def getProviders(self):
|
||||
def get_providers(self):
|
||||
providers = {'opensubtitles': Prefs['provider.opensubtitles.enabled'],
|
||||
#'thesubdb': Prefs['provider.thesubdb.enabled'],
|
||||
'podnapisi': Prefs['provider.podnapisi.enabled'],
|
||||
@@ -144,7 +205,7 @@ class Config(object):
|
||||
}
|
||||
return filter(lambda prov: providers[prov], providers)
|
||||
|
||||
def getProviderSettings(self):
|
||||
def get_provider_settings(self):
|
||||
provider_settings = {'addic7ed': {'username': Prefs['provider.addic7ed.username'],
|
||||
'password': Prefs['provider.addic7ed.password'],
|
||||
'use_random_agents': Prefs['provider.addic7ed.use_random_agents'],
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
# coding=utf-8
|
||||
|
||||
import os
|
||||
import traceback
|
||||
import unicodedata
|
||||
import datetime
|
||||
import urllib
|
||||
import time
|
||||
import re
|
||||
import platform
|
||||
import subprocess
|
||||
|
||||
# Unicode control characters can appear in ID3v2 tags but are not legal in XML.
|
||||
|
||||
RE_UNICODE_CONTROL = u'([\u0000-\u0008\u000b-\u000c\u000e-\u001f\ufffe-\uffff])' + \
|
||||
u'|' + \
|
||||
u'([%s-%s][^%s-%s])|([^%s-%s][%s-%s])|([%s-%s]$)|(^[%s-%s])' % \
|
||||
@@ -19,7 +21,7 @@ RE_UNICODE_CONTROL = u'([\u0000-\u0008\u000b-\u000c\u000e-\u001f\ufffe-\uffff])'
|
||||
|
||||
|
||||
# A platform independent way to split paths which might come in with different separators.
|
||||
def splitPath(str):
|
||||
def split_path(str):
|
||||
if str.find('\\') != -1:
|
||||
return str.split('\\')
|
||||
else:
|
||||
@@ -39,10 +41,11 @@ def unicodize(s):
|
||||
return filename
|
||||
|
||||
|
||||
def cleanFilename(filename):
|
||||
def clean_filename(filename):
|
||||
# this will remove any whitespace and punctuation chars and replace them with spaces, strip and return as lowercase
|
||||
return string.translate(filename.encode('utf-8'), string.maketrans(string.punctuation + string.whitespace,
|
||||
' ' * len(string.punctuation + string.whitespace))).strip().lower()
|
||||
' ' * len(
|
||||
string.punctuation + string.whitespace))).strip().lower()
|
||||
|
||||
|
||||
def is_recent(t):
|
||||
@@ -95,14 +98,16 @@ def format_item(item, kind, parent=None, parent_title=None, section_title=None,
|
||||
:return:
|
||||
"""
|
||||
return format_video(kind, item.title,
|
||||
section_title=(section_title or (parent.section.title if parent and getattr(parent, "section") else None)),
|
||||
section_title=(
|
||||
section_title or (parent.section.title if parent and getattr(parent, "section") else None)),
|
||||
parent_title=(parent_title or (parent.show.title if parent else None)),
|
||||
season=parent.index if parent else None,
|
||||
episode=item.index if kind == "show" else None,
|
||||
add_section_title=add_section_title)
|
||||
|
||||
|
||||
def format_video(kind, title, section_title=None, parent_title=None, season=None, episode=None, add_section_title=False):
|
||||
def format_video(kind, title, section_title=None, parent_title=None, season=None, episode=None,
|
||||
add_section_title=False):
|
||||
section_add = ""
|
||||
if add_section_title:
|
||||
section_add = ("%s: " % section_title) if section_title else ""
|
||||
@@ -134,10 +139,66 @@ def query_plex(url, args):
|
||||
:return:
|
||||
"""
|
||||
use_args = args.copy()
|
||||
if "token" in Dict and Dict["token"]:
|
||||
use_args["X-Plex-Token"] = Dict["token"]
|
||||
|
||||
computed_args = "&".join(["%s=%s" % (key, String.Quote(value)) for key, value in use_args.iteritems()])
|
||||
|
||||
return HTTP.Request(url + ("?%s" % computed_args) if computed_args else "", immediate=True)
|
||||
|
||||
|
||||
def check_write_permissions(path):
|
||||
if platform.system() == "Windows":
|
||||
# physical access check
|
||||
check_path = os.path.join(os.path.realpath(path), ".sz_perm_chk")
|
||||
try:
|
||||
if os.path.exists(check_path):
|
||||
os.rmdir(check_path)
|
||||
os.mkdir(check_path)
|
||||
os.rmdir(check_path)
|
||||
return True
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
else:
|
||||
# os.access check
|
||||
return os.access(path, os.W_OK | os.X_OK)
|
||||
return False
|
||||
|
||||
|
||||
def get_item_hints(title, kind, series=None):
|
||||
hints = {"expected_title": [title]}
|
||||
hints.update({"type": "episode", "expected_series": [series]} if kind == "series" else {"type": "movie"})
|
||||
return hints
|
||||
|
||||
|
||||
def notify_executable(exe_info, videos, subtitles, storage):
|
||||
variables = (
|
||||
"subtitle_language", "subtitle_path", "subtitle_filename", "provider", "score", "storage", "series_id",
|
||||
"series", "title", "section", "filename", "path", "folder", "season_id", "type", "id", "season"
|
||||
)
|
||||
exe, arguments = exe_info
|
||||
for video, video_subtitles in subtitles.items():
|
||||
for subtitle in video_subtitles:
|
||||
lang = Locale.Language.Match(subtitle.language.alpha2)
|
||||
data = video.plexapi_metadata.copy()
|
||||
data.update({
|
||||
"subtitle_language": lang,
|
||||
"provider": subtitle.provider_name,
|
||||
"score": subtitle.score,
|
||||
"storage": storage,
|
||||
"subtitle_path": subtitle.storage_path,
|
||||
"subtitle_filename": os.path.basename(subtitle.storage_path)
|
||||
})
|
||||
|
||||
# fill missing data with None
|
||||
prepared_data = dict((v, data.get(v)) for v in variables)
|
||||
|
||||
prepared_arguments = [arg % prepared_data for arg in arguments]
|
||||
|
||||
Log.Debug(u"Calling %s with arguments: %s" % (exe, prepared_arguments))
|
||||
try:
|
||||
output = subprocess.check_output([exe] + prepared_arguments, stderr=subprocess.STDOUT)
|
||||
except subprocess.CalledProcessError:
|
||||
Log.Error(u"Calling %s failed: %s" % (exe, traceback.format_exc()))
|
||||
else:
|
||||
Log.Debug(u"Process output: %s" % output)
|
||||
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
import logging
|
||||
import re
|
||||
import types
|
||||
import os
|
||||
from ignore import ignore_list
|
||||
from helpers import is_recent, format_item, query_plex
|
||||
from subzero import intent
|
||||
from lib import Plex
|
||||
from config import config
|
||||
from config import config, IGNORE_FN
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -16,6 +17,27 @@ MI_KIND, MI_TITLE, MI_KEY, MI_DEEPER, MI_ITEM = 0, 1, 2, 3, 4
|
||||
container_size_re = re.compile(ur'totalSize="(\d+)"')
|
||||
|
||||
|
||||
def get_item(key):
|
||||
item_id = int(key)
|
||||
item_container = Plex["library"].metadata(item_id)
|
||||
|
||||
item = list(item_container)[0]
|
||||
return item
|
||||
|
||||
|
||||
def get_item_kind(item):
|
||||
return type(item).__name__
|
||||
|
||||
|
||||
def get_item_thumb(item):
|
||||
kind = get_item_kind(item)
|
||||
if kind == "Episode":
|
||||
return item.show.thumb
|
||||
elif kind == "Section":
|
||||
return item.art
|
||||
return item.thumb
|
||||
|
||||
|
||||
def get_items_info(items):
|
||||
return items[0][MI_KIND], items[0][MI_DEEPER]
|
||||
|
||||
@@ -24,7 +46,7 @@ def get_kind(items):
|
||||
return items[0][MI_KIND]
|
||||
|
||||
|
||||
def getSectionSize(key):
|
||||
def get_section_size(key):
|
||||
"""
|
||||
quick query to determine the section size
|
||||
:param key:
|
||||
@@ -44,7 +66,7 @@ def getSectionSize(key):
|
||||
return size
|
||||
|
||||
|
||||
def getItems(key="recently_added", base="library", value=None, flat=False, add_section_title=False):
|
||||
def get_items(key="recently_added", base="library", value=None, flat=False, add_section_title=False):
|
||||
"""
|
||||
try to handle all return types plex throws at us and return a generalized item tuple
|
||||
"""
|
||||
@@ -66,6 +88,17 @@ def getItems(key="recently_added", base="library", value=None, flat=False, add_s
|
||||
else:
|
||||
kind = item.type
|
||||
|
||||
# only return items for our enabled sections
|
||||
section_key = None
|
||||
if kind == "section":
|
||||
section_key = item.key
|
||||
else:
|
||||
if hasattr(item, "section_key"):
|
||||
section_key = getattr(item, "section_key")
|
||||
|
||||
if section_key and section_key not in config.enabled_sections:
|
||||
continue
|
||||
|
||||
if kind == "season":
|
||||
# fixme: i think this case is unused now
|
||||
if flat:
|
||||
@@ -81,8 +114,9 @@ def getItems(key="recently_added", base="library", value=None, flat=False, add_s
|
||||
items.append(("directory", item.title, item.key, True, item))
|
||||
|
||||
elif kind == "section":
|
||||
item.size = getSectionSize(item.key)
|
||||
items.append(("section", item.title, int(item.key), True, item))
|
||||
if item.type in ['movie', 'show']:
|
||||
item.size = get_section_size(item.key)
|
||||
items.append(("section", item.title, int(item.key), True, item))
|
||||
|
||||
elif kind == "episode":
|
||||
items.append(
|
||||
@@ -101,12 +135,12 @@ def getItems(key="recently_added", base="library", value=None, flat=False, add_s
|
||||
return items
|
||||
|
||||
|
||||
def getRecentlyAddedItems():
|
||||
items = getItems(key="recently_added")
|
||||
def get_recently_added_items():
|
||||
items = get_items(key="recently_added")
|
||||
return filter(lambda x: is_recent(x[MI_ITEM].added_at), items)
|
||||
|
||||
|
||||
def getRecentItems():
|
||||
def get_recent_items():
|
||||
"""
|
||||
actually get the recent items, not limited like /library/recentlyAdded
|
||||
:return:
|
||||
@@ -128,7 +162,9 @@ def getRecentItems():
|
||||
recent = []
|
||||
|
||||
for section in Plex["library"].sections():
|
||||
if section.type not in ("movie", "show") or section.key in ignore_list.sections:
|
||||
if section.type not in ("movie", "show") \
|
||||
or section.key not in config.enabled_sections \
|
||||
or section.key in ignore_list.sections:
|
||||
Log.Debug(u"Skipping section: %s" % section.title)
|
||||
continue
|
||||
|
||||
@@ -155,17 +191,69 @@ def getRecentItems():
|
||||
return recent
|
||||
|
||||
|
||||
def getOnDeckItems():
|
||||
return getItems(key="on_deck", add_section_title=True)
|
||||
def get_on_deck_items():
|
||||
return get_items(key="on_deck", add_section_title=True)
|
||||
|
||||
|
||||
def getAllItems(key, base="library", value=None, flat=False):
|
||||
return getItems(key, base=base, value=value, flat=flat)
|
||||
def get_all_items(key, base="library", value=None, flat=False):
|
||||
return get_items(key, base=base, value=value, flat=flat)
|
||||
|
||||
|
||||
def refreshItem(rating_key, force=False, timeout=8000):
|
||||
def is_ignored(rating_key, item=None):
|
||||
"""
|
||||
check whether an item, its show/season/section is in the soft or the hard ignore list
|
||||
:param rating_key:
|
||||
:param item:
|
||||
:return:
|
||||
"""
|
||||
# item in soft ignore list
|
||||
if rating_key in ignore_list["videos"]:
|
||||
Log.Debug("Item %s is in the soft ignore list" % rating_key)
|
||||
return True
|
||||
|
||||
item = item or get_item(rating_key)
|
||||
kind = get_item_kind(item)
|
||||
|
||||
# show in soft ignore list
|
||||
if kind == "Episode" and item.show.rating_key in ignore_list["series"]:
|
||||
Log.Debug("Item %s's show is in the soft ignore list" % rating_key)
|
||||
return True
|
||||
|
||||
# section in soft ignore list
|
||||
if item.section.key in ignore_list["sections"]:
|
||||
Log.Debug("Item %s's section is in the soft ignore list" % rating_key)
|
||||
return True
|
||||
|
||||
# physical/path ignore
|
||||
if Prefs["subtitles.ignore_fs"] or config.ignore_paths:
|
||||
# normally check current item folder and the library
|
||||
check_ignore_paths = [".", "../"]
|
||||
if kind == "Episode":
|
||||
# series/episode, we've got a season folder here, also
|
||||
check_ignore_paths.append("../../")
|
||||
|
||||
for part in item.media.parts:
|
||||
if config.ignore_paths and config.is_path_ignored(part.file):
|
||||
Log.Debug("Item %s's path is manually ignored" % rating_key)
|
||||
return True
|
||||
|
||||
if Prefs["subtitles.ignore_fs"]:
|
||||
for sub_path in check_ignore_paths:
|
||||
if config.is_physically_ignored(os.path.abspath(os.path.join(os.path.dirname(part.file), sub_path))):
|
||||
Log.Debug("An ignore file exists in either the items or its parent folders")
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def refresh_item(rating_key, force=False, timeout=8000, refresh_kind=None, parent_rating_key=None):
|
||||
# timeout actually is the time for which the intent will be valid
|
||||
if force:
|
||||
intent.set("force", rating_key, timeout=timeout)
|
||||
|
||||
if refresh_kind == "episode":
|
||||
# season refresh
|
||||
rating_key = parent_rating_key
|
||||
|
||||
Log.Info("%s item %s", "Refreshing" if not force else "Forced-refreshing", rating_key)
|
||||
Plex["library/metadata"].refresh(rating_key)
|
||||
|
||||
@@ -1,18 +1,37 @@
|
||||
# coding=utf-8
|
||||
|
||||
from plex import Plex
|
||||
from auth import refresh_plex_token
|
||||
import plex
|
||||
from subzero.lib.httpfake import PlexPyNativeResponseProxy
|
||||
|
||||
|
||||
def configure_plex():
|
||||
# this may be the only viable usage of global :O (correct me if i'm wrong)
|
||||
global Plex
|
||||
if not "token" in Dict or not (Prefs["plex_username"] and Prefs["plex_password"]):
|
||||
refresh_plex_token()
|
||||
class PlexPyNativeRequestProxy(object):
|
||||
"""
|
||||
A really dumb object that tries to mimic requests.Request in an incomplete way, so that plex.Plex
|
||||
uses native plex HTTPRequests instead of the better requests.Request class.
|
||||
|
||||
# initialize Plex api
|
||||
Plex.configuration.defaults.authentication(Dict["token"] if "token" in Dict else None)
|
||||
This allows us to operate freely on 127.0.0.1's PMS.
|
||||
|
||||
To be used in conjunction with subzero.lib.httpfake.PlexPyNativeResponseProxy
|
||||
"""
|
||||
url = None
|
||||
data = None
|
||||
headers = None
|
||||
method = None
|
||||
|
||||
def prepare(self):
|
||||
return self
|
||||
|
||||
def send(self):
|
||||
# fixme: add self.data to HTTP.Request
|
||||
data = None
|
||||
status_code = 200
|
||||
try:
|
||||
data = HTTP.Request(self.url, headers=self.headers, immediate=True, method=self.method)
|
||||
except Ex.HTTPError as e:
|
||||
status_code = e.code
|
||||
return PlexPyNativeResponseProxy(data, status_code, self)
|
||||
|
||||
|
||||
lib_unaccessible_error = "\n\n\n!!!!!!!!!!!!!! ATTENTION !!!!!!!!!!!!! \nCan't access your Plex Media Servers' API.\nAre you using Plex Home?"" \
|
||||
""Please configure your Plex.tv credentials! Advanced features disabled!\n\n\n"
|
||||
plex.request.Request = PlexPyNativeRequestProxy
|
||||
|
||||
Plex = plex.Plex
|
||||
|
||||
@@ -5,10 +5,10 @@ import config
|
||||
import helpers
|
||||
import subtitlehelpers
|
||||
|
||||
from subzero.lib.io import getViableEncoding
|
||||
from config import config as sz_config
|
||||
|
||||
|
||||
def findSubtitles(part):
|
||||
def find_subtitles(part):
|
||||
lang_sub_map = {}
|
||||
part_filename = helpers.unicodize(part.file)
|
||||
part_basename = os.path.splitext(os.path.basename(part_filename))[0]
|
||||
@@ -54,14 +54,14 @@ def findSubtitles(part):
|
||||
total_media_files = 0
|
||||
for path in paths:
|
||||
path = helpers.unicodize(path)
|
||||
for file_path_listing in os.listdir(path):
|
||||
for file_path_listing in os.listdir(path.encode(sz_config.fs_encoding)):
|
||||
|
||||
# When using os.listdir with a unicode path, it will always return a string using the
|
||||
# NFD form. However, we internally are using the form NFC and therefore need to convert
|
||||
# it to allow correct regex / comparisons to be performed.
|
||||
#
|
||||
file_path_listing = helpers.unicodize(file_path_listing)
|
||||
if os.path.isfile(os.path.join(path, file_path_listing).encode(getViableEncoding())):
|
||||
if os.path.isfile(os.path.join(path, file_path_listing).encode(sz_config.fs_encoding)):
|
||||
file_paths[file_path_listing.lower()] = os.path.join(path, file_path_listing)
|
||||
|
||||
# If we've found an actual media file, we should record it.
|
||||
@@ -90,7 +90,7 @@ def findSubtitles(part):
|
||||
if total_media_files > 1 and not filename_matches_part:
|
||||
continue
|
||||
|
||||
subtitle_helper = subtitlehelpers.SubtitleHelpers(file_path)
|
||||
subtitle_helper = subtitlehelpers.subtitle_helpers(file_path)
|
||||
if subtitle_helper != None:
|
||||
local_lang_map = subtitle_helper.process_subtitles(part)
|
||||
for new_language, subtitles in local_lang_map.items():
|
||||
@@ -104,7 +104,7 @@ def findSubtitles(part):
|
||||
|
||||
# add known metadata subs to our sub list
|
||||
if not use_filesystem:
|
||||
for language, sub_list in subtitlehelpers.getSubtitlesFromMetadata(part).iteritems():
|
||||
for language, sub_list in subtitlehelpers.get_subtitles_from_metadata(part).iteritems():
|
||||
if sub_list:
|
||||
if language not in lang_sub_map:
|
||||
lang_sub_map[language] = []
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
# coding=utf-8
|
||||
import traceback
|
||||
|
||||
from support.config import config
|
||||
from support.helpers import format_item
|
||||
from lib import Plex
|
||||
from support.items import get_item
|
||||
from support.lib import Plex
|
||||
|
||||
|
||||
def itemDiscoverMissing(rating_key, kind="show", added_at=None, section_title=None, internal=False, external=True, languages=()):
|
||||
def item_discover_missing_subs(rating_key, kind="show", added_at=None, section_title=None, internal=False, external=True, languages=()):
|
||||
existing_subs = {"internal": [], "external": [], "count": 0}
|
||||
|
||||
item_id = int(rating_key)
|
||||
item_container = Plex["library"].metadata(item_id)
|
||||
|
||||
item = list(item_container)[0]
|
||||
item = get_item(rating_key)
|
||||
|
||||
if kind == "show":
|
||||
item_title = format_item(item, kind, parent=item.season, section_title=section_title, parent_title=item.show.title)
|
||||
@@ -35,7 +35,7 @@ def itemDiscoverMissing(rating_key, kind="show", added_at=None, section_title=No
|
||||
if existing_subs["count"]:
|
||||
existing_flat = (existing_subs["internal"] if internal else []) + (existing_subs["external"] if external else [])
|
||||
languages_set = set(languages)
|
||||
if languages_set.issubset(existing_flat):
|
||||
if languages_set.issubset(existing_flat) or (len(existing_flat) >= 1 and Prefs['subtitles.only_one']):
|
||||
# all subs found
|
||||
Log.Info(u"All subtitles exist for '%s'", item_title)
|
||||
return
|
||||
@@ -44,19 +44,19 @@ def itemDiscoverMissing(rating_key, kind="show", added_at=None, section_title=No
|
||||
Log.Info(u"Subs still missing for '%s': %s", item_title, missing)
|
||||
|
||||
if missing:
|
||||
return added_at, item_id, item_title
|
||||
return added_at, item_id, item_title, item
|
||||
|
||||
|
||||
def getAllMissing(items):
|
||||
def items_get_all_missing_subs(items):
|
||||
missing = []
|
||||
for added_at, kind, section_title, key in items:
|
||||
try:
|
||||
state = itemDiscoverMissing(
|
||||
state = item_discover_missing_subs(
|
||||
key,
|
||||
kind=kind,
|
||||
added_at=added_at,
|
||||
section_title=section_title,
|
||||
languages=config.langList,
|
||||
languages=config.lang_list,
|
||||
internal=bool(Prefs["subtitles.scan.embedded"]),
|
||||
external=bool(Prefs["subtitles.scan.external"])
|
||||
)
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
# coding=utf-8
|
||||
|
||||
import os
|
||||
import subliminal
|
||||
import helpers
|
||||
|
||||
from items import get_item
|
||||
from subzero import intent
|
||||
|
||||
|
||||
def flatten_media(media, kind="series"):
|
||||
"""
|
||||
iterates through media and returns the associated parts (videos)
|
||||
:param media:
|
||||
:param kind:
|
||||
:return:
|
||||
"""
|
||||
parts = []
|
||||
|
||||
def get_metadata_dict(item, part, add):
|
||||
data = {
|
||||
"section": item.section.title,
|
||||
"path": part.file,
|
||||
"folder": os.path.dirname(part.file),
|
||||
"filename": os.path.basename(part.file)
|
||||
}
|
||||
data.update(add)
|
||||
return data
|
||||
|
||||
if kind == "series":
|
||||
for season in media.seasons:
|
||||
season_object = media.seasons[season]
|
||||
for episode in media.seasons[season].episodes:
|
||||
ep = media.seasons[season].episodes[episode]
|
||||
|
||||
# get plex item via API for additional metadata
|
||||
plex_episode = get_item(ep.id)
|
||||
|
||||
for item in media.seasons[season].episodes[episode].items:
|
||||
for part in item.parts:
|
||||
parts.append(
|
||||
get_metadata_dict(plex_episode, part,
|
||||
{"video": part, "type": "episode", "title": ep.title,
|
||||
"series": media.title, "id": ep.id,
|
||||
"series_id": media.id, "season_id": season_object.id,
|
||||
"season": plex_episode.season.index,
|
||||
})
|
||||
)
|
||||
else:
|
||||
plex_item = get_item(media.id)
|
||||
for item in media.items:
|
||||
for part in item.parts:
|
||||
parts.append(
|
||||
get_metadata_dict(plex_item, part, {"video": part, "type": "movie",
|
||||
"title": media.title, "id": media.id,
|
||||
"series_id": None,
|
||||
"season_id": None,
|
||||
"section": plex_item.section.title})
|
||||
)
|
||||
return parts
|
||||
|
||||
|
||||
IGNORE_FN = ("subzero.ignore", ".subzero.ignore", ".nosz")
|
||||
|
||||
|
||||
def convert_media_to_parts(media, kind="series"):
|
||||
"""
|
||||
returns a list of parts to be used later on; ignores folders with an existing "subzero.ignore" file
|
||||
:param media:
|
||||
:param kind:
|
||||
:return:
|
||||
"""
|
||||
return flatten_media(media, kind=kind)
|
||||
|
||||
|
||||
def get_stream_fps(streams):
|
||||
"""
|
||||
accepts a list of plex streams or a list of the plex api streams
|
||||
"""
|
||||
for stream in streams:
|
||||
# video
|
||||
stream_type = getattr(stream, "type", getattr(stream, "stream_type", None))
|
||||
if stream_type == 1:
|
||||
return getattr(stream, "frameRate", getattr(stream, "frame_rate", "25.000"))
|
||||
return "25.000"
|
||||
|
||||
|
||||
def get_media_item_ids(media, kind="series"):
|
||||
ids = []
|
||||
if kind == "movies":
|
||||
ids.append(media.id)
|
||||
else:
|
||||
for season in media.seasons:
|
||||
for episode in media.seasons[season].episodes:
|
||||
ids.append(media.seasons[season].episodes[episode].id)
|
||||
|
||||
return ids
|
||||
|
||||
|
||||
def scan_video(plex_video, ignore_all=False, hints=None):
|
||||
embedded_subtitles = not ignore_all and Prefs['subtitles.scan.embedded']
|
||||
external_subtitles = not ignore_all and Prefs['subtitles.scan.external']
|
||||
|
||||
if ignore_all:
|
||||
Log.Debug("Force refresh intended.")
|
||||
|
||||
Log.Debug("Scanning video: %s, subtitles=%s, embedded_subtitles=%s" % (plex_video.file, external_subtitles, embedded_subtitles))
|
||||
|
||||
try:
|
||||
return subliminal.video.scan_video(plex_video.file, subtitles=external_subtitles, embedded_subtitles=embedded_subtitles,
|
||||
hints=hints or {}, video_fps=plex_video.fps)
|
||||
|
||||
except ValueError:
|
||||
Log.Warn("File could not be guessed by subliminal")
|
||||
|
||||
|
||||
def scan_parts(parts, kind="series"):
|
||||
"""
|
||||
receives a list of parts containing dictionaries returned by flattenToParts
|
||||
:param parts:
|
||||
:param kind: series or movies
|
||||
:return: dictionary of subliminal.video.scan_video, key=subliminal scanned video, value=plex file part
|
||||
"""
|
||||
ret = {}
|
||||
for part in parts:
|
||||
force_refresh = intent.get("force", part["id"], part["series_id"], part["season_id"])
|
||||
|
||||
hints = helpers.get_item_hints(part["title"], kind, series=part["series"] if kind == "series" else None)
|
||||
part["video"].fps = get_stream_fps(part["video"].streams)
|
||||
scanned_video = scan_video(part["video"], ignore_all=force_refresh, hints=hints)
|
||||
if not scanned_video:
|
||||
continue
|
||||
|
||||
scanned_video.id = part["id"]
|
||||
part_metadata = part.copy()
|
||||
del part_metadata["video"]
|
||||
scanned_video.plexapi_metadata = part_metadata
|
||||
ret[scanned_video] = part["video"]
|
||||
return ret
|
||||
@@ -4,25 +4,61 @@ import datetime
|
||||
import pprint
|
||||
|
||||
|
||||
def storeSubtitleInfo(videos, subtitles, storage_type):
|
||||
def get_subtitle_info(rating_key):
|
||||
return Dict["subs"].get(rating_key)
|
||||
|
||||
|
||||
def whack_missing_parts(videos, existing_parts=None):
|
||||
"""
|
||||
cleans out our internal storage's video parts (parts may get updated/deleted/whatever)
|
||||
:param existing_parts: optional list of part ids known
|
||||
:param videos: videos to check for
|
||||
:return:
|
||||
"""
|
||||
# shortcut
|
||||
|
||||
if not existing_parts:
|
||||
existing_parts = []
|
||||
for part in videos.viewvalues():
|
||||
existing_parts.append(part.id)
|
||||
|
||||
whacked_parts = False
|
||||
for video in videos.keys():
|
||||
if video.id not in Dict["subs"]:
|
||||
continue
|
||||
|
||||
for part_id in Dict["subs"][video.id].keys():
|
||||
if part_id not in existing_parts:
|
||||
del Dict["subs"][video.id][part_id]
|
||||
Log.Info("Whacking part %s in internal storage of video %s", part_id, video.id)
|
||||
whacked_parts = True
|
||||
|
||||
if whacked_parts:
|
||||
Dict.Save()
|
||||
|
||||
|
||||
def store_subtitle_info(videos, subtitles, storage_type):
|
||||
"""
|
||||
stores information about downloaded subtitles in plex's Dict()
|
||||
"""
|
||||
if not "subs" in Dict:
|
||||
if "subs" not in Dict:
|
||||
Dict["subs"] = {}
|
||||
|
||||
storage = Dict["subs"]
|
||||
|
||||
existing_parts = []
|
||||
for video, video_subtitles in subtitles.items():
|
||||
part = videos[video]
|
||||
|
||||
if not video.id in storage:
|
||||
if video.id not in storage:
|
||||
storage[video.id] = {}
|
||||
|
||||
video_dict = storage[video.id]
|
||||
if not part.id in video_dict:
|
||||
if part.id not in video_dict:
|
||||
video_dict[part.id] = {}
|
||||
|
||||
existing_parts.append(part.id)
|
||||
|
||||
part_dict = video_dict[part.id]
|
||||
for subtitle in video_subtitles:
|
||||
lang = Locale.Language.Match(subtitle.language.alpha2)
|
||||
@@ -32,11 +68,14 @@ def storeSubtitleInfo(videos, subtitles, storage_type):
|
||||
sub_key = (subtitle.provider_name, subtitle.id)
|
||||
lang_dict[sub_key] = dict(score=subtitle.score, link=subtitle.page_link, storage=storage_type, hash=Hash.MD5(subtitle.content),
|
||||
date_added=datetime.datetime.now())
|
||||
lang_dict["current"] = sub_key
|
||||
|
||||
if existing_parts:
|
||||
whack_missing_parts(videos, existing_parts=existing_parts)
|
||||
Dict.Save()
|
||||
|
||||
|
||||
def resetStorage(key):
|
||||
def reset_storage(key):
|
||||
"""
|
||||
resets the Dict[key] storage, thanks to https://docs.google.com/document/d/1hhLjV1pI-TA5y91TiJq64BdgKwdLnFt4hWgeOqpz1NA/edit#
|
||||
We can't use the nice Plex interface for this, as it calls get multiple times before set
|
||||
@@ -48,6 +87,6 @@ def resetStorage(key):
|
||||
Dict.Save()
|
||||
|
||||
|
||||
def logStorage(key):
|
||||
def log_storage(key):
|
||||
if key in Dict:
|
||||
Log.Debug(pprint.pformat(Dict[key]))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# coding=utf-8
|
||||
|
||||
import re, unicodedata, os
|
||||
import re, os
|
||||
import config
|
||||
import helpers
|
||||
|
||||
@@ -12,7 +12,7 @@ class SubtitleHelper(object):
|
||||
self.filename = filename
|
||||
|
||||
|
||||
def SubtitleHelpers(filename):
|
||||
def subtitle_helpers(filename):
|
||||
filename = helpers.unicodize(filename)
|
||||
for cls in [VobSubSubtitleHelper, DefaultSubtitleHelper]:
|
||||
if cls.is_helper_for(filename):
|
||||
@@ -137,7 +137,7 @@ class DefaultSubtitleHelper(SubtitleHelper):
|
||||
return lang_sub_map
|
||||
|
||||
|
||||
def getSubtitlesFromMetadata(part):
|
||||
def get_subtitles_from_metadata(part):
|
||||
subs = {}
|
||||
for language in part.subtitles:
|
||||
subs[language] = []
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
import datetime
|
||||
import time
|
||||
|
||||
from missing_subtitles import getAllMissing, refresh_item
|
||||
from missing_subtitles import items_get_all_missing_subs, refresh_item
|
||||
from background import scheduler
|
||||
from support.items import getRecentItems
|
||||
from support.items import get_recent_items, is_ignored
|
||||
|
||||
|
||||
class Task(object):
|
||||
@@ -82,9 +82,9 @@ class SearchAllRecentlyAddedMissing(Task):
|
||||
|
||||
def prepare(self):
|
||||
self.items_done = []
|
||||
recent_items = getRecentItems()
|
||||
missing = getAllMissing(recent_items)
|
||||
ids = set([id for added_at, id, title in missing])
|
||||
recent_items = get_recent_items()
|
||||
missing = items_get_all_missing_subs(recent_items)
|
||||
ids = set([id for added_at, id, title, item in missing if not is_ignored(id, item=item)])
|
||||
self.items_searching = missing
|
||||
self.items_searching_ids = ids
|
||||
self.items_failed = []
|
||||
@@ -97,7 +97,7 @@ class SearchAllRecentlyAddedMissing(Task):
|
||||
missing_count = len(self.items_searching)
|
||||
items_done_count = 0
|
||||
|
||||
for added_at, item_id, title in self.items_searching:
|
||||
for added_at, item_id, title, item in self.items_searching:
|
||||
Log.Debug(u"Task: %s, triggering refresh for %s (%s)", self.name, title, item_id)
|
||||
refresh_item(item_id, title)
|
||||
search_started = datetime.datetime.now()
|
||||
|
||||
@@ -17,20 +17,6 @@
|
||||
],
|
||||
"default": "2"
|
||||
},
|
||||
{
|
||||
"id": "plex_username",
|
||||
"label": "Plex.tv Username (needed for Plex Home users)",
|
||||
"type": "text",
|
||||
"default": ""
|
||||
},
|
||||
{
|
||||
"id": "plex_password",
|
||||
"label": "Plex.tv Password",
|
||||
"type": "text",
|
||||
"option": "hidden",
|
||||
"default": "",
|
||||
"secure": "true"
|
||||
},
|
||||
{
|
||||
"id": "provider.addic7ed.username",
|
||||
"label": "Addic7ed Username",
|
||||
@@ -395,6 +381,12 @@
|
||||
"type": "text",
|
||||
"default": ""
|
||||
},
|
||||
{
|
||||
"id": "subtitles.save.metadata_fallback",
|
||||
"label": "Fall back to metadata storage if filesystem storage failed",
|
||||
"type": "bool",
|
||||
"default": "false"
|
||||
},
|
||||
{
|
||||
"id": "subtitles.language.ietf",
|
||||
"label": "Treat IETF language tags as ISO 639-1 (e.g. pt-BR = pt)",
|
||||
@@ -407,6 +399,18 @@
|
||||
"type": "bool",
|
||||
"default": "false"
|
||||
},
|
||||
{
|
||||
"id": "subtitles.ignore_paths",
|
||||
"label": "Ignore anything in the following paths (comma-separated)",
|
||||
"type": "text",
|
||||
"default": ""
|
||||
},
|
||||
{
|
||||
"id": "notify_executable",
|
||||
"label": "Call this executable upon successful subtitle download",
|
||||
"type": "text",
|
||||
"default": ""
|
||||
},
|
||||
{
|
||||
"id": "scheduler.tasks.searchAllRecentlyAddedMissing",
|
||||
"label": "Scheduler: Periodically search for recent items with missing subtitles",
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1.3.31.513</string>
|
||||
<string>1.3.33.522</string>
|
||||
<key>PlexFrameworkVersion</key>
|
||||
<string>2</string>
|
||||
<key>PlexPluginClass</key>
|
||||
@@ -32,7 +32,7 @@
|
||||
|
||||
<h1>Sub-Zero for Plex</h1><i>Subtitles done right</i>
|
||||
|
||||
Version 1.3.31.513
|
||||
Version 1.3.33.522
|
||||
|
||||
Originally based on @bramwalet's awesome <a href="https://github.com/bramwalet/Subliminal.bundle">Subliminal.bundle</a>
|
||||
|
||||
|
||||
@@ -73,17 +73,18 @@ class HttpClient(object):
|
||||
return response
|
||||
|
||||
# TODO retrying requests on 502, 503 errors?
|
||||
try:
|
||||
response = self.session.send(prepared)
|
||||
except socket.gaierror as e:
|
||||
code, _ = e
|
||||
|
||||
if code != 8:
|
||||
raise e
|
||||
|
||||
log.warn('Encountered socket.gaierror (code: 8)')
|
||||
|
||||
response = self._build().send(prepared)
|
||||
# try:
|
||||
# response = self.session.send(prepared)
|
||||
# except socket.gaierror as e:
|
||||
# code, _ = e
|
||||
#
|
||||
# if code != 8:
|
||||
# raise e
|
||||
#
|
||||
# log.warn('Encountered socket.gaierror (code: 8)')
|
||||
#
|
||||
# response = self._build().send(prepared)
|
||||
response = request.request.send()
|
||||
|
||||
# Store response in cache
|
||||
self._cache_store(prepared, response)
|
||||
|
||||
@@ -34,7 +34,7 @@ class LibraryInterface(Interface):
|
||||
'MediaContainer': ('MediaContainer', idict({
|
||||
'Video': {
|
||||
'movie': 'Movie',
|
||||
'episode': 'Episode'
|
||||
'episode': 'Episode'
|
||||
}
|
||||
}))
|
||||
}))
|
||||
|
||||
@@ -40,3 +40,20 @@ class RootInterface(Interface):
|
||||
'Server': 'Server'
|
||||
}))
|
||||
}))
|
||||
|
||||
def agents(self):
|
||||
response = self.http.get('system/agents')
|
||||
|
||||
return self.parse(response, idict({
|
||||
'MediaContainer': ('Container', idict({
|
||||
'Agent': 'Agent'
|
||||
}))
|
||||
}))
|
||||
|
||||
def primary_agent(self, guid, media_type):
|
||||
response = self.http.get('/system/agents/%s/config/%s' % (guid, media_type))
|
||||
return self.parse(response, idict({
|
||||
'MediaContainer': ('Container', idict({
|
||||
'Agent': 'Agent'
|
||||
}))
|
||||
}))
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
from plex.objects.core.base import Descriptor, Property
|
||||
|
||||
|
||||
class MediaType(Descriptor):
|
||||
name = Property
|
||||
media_type = Property("mediaType", type=int)
|
||||
|
||||
@classmethod
|
||||
def from_node(cls, client, node):
|
||||
items = []
|
||||
|
||||
for t in cls.helpers.findall(node, 'MediaType'):
|
||||
_, obj = MediaType.construct(client, t, child=True)
|
||||
|
||||
items.append(obj)
|
||||
|
||||
return [], items
|
||||
|
||||
|
||||
class Agent(Descriptor):
|
||||
name = Property
|
||||
enabled = Property(type=int)
|
||||
identifier = Property
|
||||
primary = Property(type=int)
|
||||
has_prefs = Property("hasPrefs", type=int)
|
||||
has_attribution = Property("hasAttribution", type=int)
|
||||
|
||||
media_types = Property(resolver=lambda: MediaType.from_node)
|
||||
|
||||
@@ -5,6 +5,9 @@ from plex.objects.library.section import Section
|
||||
class Metadata(Descriptor):
|
||||
section = Property(resolver=lambda: Metadata.construct_section)
|
||||
|
||||
# somehow section doesn't resolve on onDeck, add key manually
|
||||
section_key = Property('librarySectionID')
|
||||
|
||||
key = Property
|
||||
guid = Property
|
||||
rating_key = Property('ratingKey')
|
||||
@@ -32,3 +35,4 @@ class Metadata(Descriptor):
|
||||
}
|
||||
|
||||
return Section.construct(client, node, attribute_map, child=True)
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
from .ssafile import SSAFile
|
||||
from .ssaevent import SSAEvent
|
||||
from .ssastyle import SSAStyle
|
||||
from . import time, formats, cli
|
||||
from .exceptions import *
|
||||
from .common import Color, VERSION
|
||||
|
||||
#: Alias for :meth:`SSAFile.load()`.
|
||||
load = SSAFile.load
|
||||
|
||||
#: Alias for :meth:`pysubs2.time.make_time()`.
|
||||
make_time = time.make_time
|
||||
@@ -0,0 +1,7 @@
|
||||
import sys
|
||||
from .cli import Pysubs2CLI
|
||||
|
||||
if __name__ == "__main__":
|
||||
cli = Pysubs2CLI()
|
||||
rv = cli(sys.argv[1:])
|
||||
sys.exit(rv)
|
||||
@@ -0,0 +1,165 @@
|
||||
from __future__ import unicode_literals, print_function
|
||||
import argparse
|
||||
import codecs
|
||||
import os
|
||||
import re
|
||||
import os.path as op
|
||||
import io
|
||||
from io import open
|
||||
import sys
|
||||
from textwrap import dedent
|
||||
from .formats import get_file_extension
|
||||
from .time import make_time
|
||||
from .ssafile import SSAFile
|
||||
from .common import PY3, VERSION
|
||||
|
||||
|
||||
def positive_float(s):
|
||||
x = float(s)
|
||||
if not x > 0:
|
||||
raise argparse.ArgumentTypeError("%r is not a positive number" % s)
|
||||
return x
|
||||
|
||||
def character_encoding(s):
|
||||
try:
|
||||
codecs.lookup(s)
|
||||
return s
|
||||
except LookupError:
|
||||
raise argparse.ArgumentError
|
||||
|
||||
def time(s):
|
||||
d = {}
|
||||
for v, k in re.findall(r"(\d*\.?\d*)(ms|m|s|h)", s):
|
||||
d[k] = float(v)
|
||||
return make_time(**d)
|
||||
|
||||
|
||||
def change_ext(path, ext):
|
||||
base, _ = op.splitext(path)
|
||||
return base + ext
|
||||
|
||||
|
||||
class Pysubs2CLI(object):
|
||||
def __init__(self):
|
||||
parser = self.parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
prog="pysubs2",
|
||||
description=dedent("""
|
||||
The pysubs2 CLI for processing subtitle files.
|
||||
https://github.com/tkarabela/pysubs2
|
||||
"""),
|
||||
epilog=dedent("""
|
||||
usage examples:
|
||||
python -m pysubs2 --to srt *.ass
|
||||
python -m pysubs2 --to microdvd --fps 23.976 *.ass
|
||||
python -m pysubs2 --shift 0.3s *.srt
|
||||
python -m pysubs2 --shift 0.3s <my_file.srt >retimed_file.srt
|
||||
python -m pysubs2 --shift-back 0.3s --output-dir retimed *.srt
|
||||
python -m pysubs2 --transform-framerate 25 23.976 *.srt"""))
|
||||
|
||||
parser.add_argument("files", nargs="*", metavar="FILE",
|
||||
help="Input subtitle files. Can be in SubStation Alpha (*.ass, *.ssa), SubRip (*.srt) or "
|
||||
"MicroDVD (*.sub) formats. When no files are specified, pysubs2 will work as a pipe, "
|
||||
"reading from standard input and writing to standard output.")
|
||||
|
||||
parser.add_argument("-v", "--version", action="version", version="pysubs2 %s" % VERSION)
|
||||
|
||||
parser.add_argument("-f", "--from", choices=["ass", "ssa", "srt", "microdvd", "json"], dest="input_format",
|
||||
help="By default, subtitle format is detected from the file. This option can be used to "
|
||||
"skip autodetection and force specific format. Generally, it should never be needed.")
|
||||
parser.add_argument("-t", "--to", choices=["ass", "ssa", "srt", "microdvd", "json"], dest="output_format",
|
||||
help="Convert subtitle files to given format. By default, each file is saved in its "
|
||||
"original format.")
|
||||
parser.add_argument("--input-enc", metavar="ENCODING", default="iso-8859-1", type=character_encoding,
|
||||
help="Character encoding for input files. By default, ISO-8859-1 is used for both "
|
||||
"input and output, which should generally work (for 8-bit encodings).")
|
||||
parser.add_argument("--output-enc", metavar="ENCODING", type=character_encoding,
|
||||
help="Character encoding for output files. By default, it is the same as input encoding. "
|
||||
"If you wish to convert between encodings, make sure --input-enc is set correctly! "
|
||||
"Otherwise, your output files will probably be corrupted. It's a good idea to "
|
||||
"back up your files or use the -o option.")
|
||||
parser.add_argument("--fps", metavar="FPS", type=positive_float,
|
||||
help="This argument specifies framerate for MicroDVD files. By default, framerate "
|
||||
"is detected from the file. Use this when framerate specification is missing "
|
||||
"or to force different framerate.")
|
||||
parser.add_argument("-o", "--output-dir", metavar="DIR",
|
||||
help="Use this to save all files to given directory. By default, every file is saved to its parent directory, "
|
||||
"ie. unless it's being saved in different subtitle format (and thus with different file extension), "
|
||||
"it overwrites the original file.")
|
||||
|
||||
group = parser.add_mutually_exclusive_group()
|
||||
|
||||
group.add_argument("--shift", metavar="TIME", type=time,
|
||||
help="Delay all subtitles by given time amount. Time is specified like this: '1m30s', '0.5s', ...")
|
||||
group.add_argument("--shift-back", metavar="TIME", type=time,
|
||||
help="The opposite of --shift (subtitles will appear sooner).")
|
||||
group.add_argument("--transform-framerate", nargs=2, metavar=("FPS1", "FPS2"), type=positive_float,
|
||||
help="Multiply all timestamps by FPS1/FPS2 ratio.")
|
||||
|
||||
def __call__(self, argv):
|
||||
try:
|
||||
self.main(argv)
|
||||
except KeyboardInterrupt:
|
||||
exit("\nAborted by user.")
|
||||
|
||||
def main(self, argv):
|
||||
args = self.parser.parse_args(argv)
|
||||
errors = 0
|
||||
|
||||
if args.output_dir and not op.exists(args.output_dir):
|
||||
os.makedirs(args.output_dir)
|
||||
|
||||
if args.output_enc is None:
|
||||
args.output_enc = args.input_enc
|
||||
|
||||
if args.files:
|
||||
for path in args.files:
|
||||
if not op.exists(path):
|
||||
print("Skipping", path, "(does not exist)")
|
||||
errors += 1
|
||||
elif not op.isfile(path):
|
||||
print("Skipping", path, "(not a file)")
|
||||
errors += 1
|
||||
else:
|
||||
with open(path, encoding=args.input_enc) as infile:
|
||||
subs = SSAFile.from_file(infile, args.input_format, args.fps)
|
||||
|
||||
self.process(subs, args)
|
||||
|
||||
if args.output_format is None:
|
||||
outpath = path
|
||||
output_format = subs.format
|
||||
else:
|
||||
ext = get_file_extension(args.output_format)
|
||||
outpath = change_ext(path, ext)
|
||||
output_format = args.output_format
|
||||
|
||||
if args.output_dir is not None:
|
||||
_, filename = op.split(outpath)
|
||||
outpath = op.join(args.output_dir, filename)
|
||||
|
||||
with open(outpath, "w", encoding=args.output_enc) as outfile:
|
||||
subs.to_file(outfile, output_format, args.fps)
|
||||
else:
|
||||
if PY3:
|
||||
infile = io.TextIOWrapper(sys.stdin.buffer, args.input_enc)
|
||||
outfile = io.TextIOWrapper(sys.stdout.buffer, args.output_enc)
|
||||
else:
|
||||
infile = io.TextIOWrapper(sys.stdin, args.input_enc)
|
||||
outfile = io.TextIOWrapper(sys.stdout, args.output_enc)
|
||||
|
||||
subs = SSAFile.from_file(infile, args.input_format, args.fps)
|
||||
self.process(subs, args)
|
||||
output_format = args.output_format or subs.format
|
||||
subs.to_file(outfile, output_format, args.fps)
|
||||
|
||||
return (0 if errors == 0 else 1)
|
||||
|
||||
@staticmethod
|
||||
def process(subs, args):
|
||||
if args.shift is not None:
|
||||
subs.shift(ms=args.shift)
|
||||
elif args.shift_back is not None:
|
||||
subs.shift(ms=-args.shift_back)
|
||||
elif args.transform_framerate is not None:
|
||||
in_fps, out_fps = args.transform_framerate
|
||||
subs.transform_framerate(in_fps, out_fps)
|
||||
@@ -0,0 +1,28 @@
|
||||
from collections import namedtuple
|
||||
import sys
|
||||
|
||||
_Color = namedtuple("Color", "r g b a")
|
||||
|
||||
class Color(_Color):
|
||||
"""
|
||||
(r, g, b, a) namedtuple for 8-bit RGB color with alpha channel.
|
||||
|
||||
All values are ints from 0 to 255.
|
||||
"""
|
||||
def __new__(cls, r, g, b, a=0):
|
||||
for value in r, g, b, a:
|
||||
if value not in range(256):
|
||||
raise ValueError("Color channels must have values 0-255")
|
||||
|
||||
return _Color.__new__(cls, r, g, b, a)
|
||||
|
||||
#: Version of the pysubs2 library.
|
||||
VERSION = "0.2.1"
|
||||
|
||||
|
||||
PY3 = sys.version_info.major == 3
|
||||
|
||||
if PY3:
|
||||
text_type = str
|
||||
else:
|
||||
text_type = unicode
|
||||
@@ -0,0 +1,14 @@
|
||||
class Pysubs2Error(Exception):
|
||||
"""Base class for pysubs2 exceptions."""
|
||||
|
||||
class UnknownFPSError(Pysubs2Error):
|
||||
"""Framerate was not specified and couldn't be inferred otherwise."""
|
||||
|
||||
class UnknownFileExtensionError(Pysubs2Error):
|
||||
"""File extension does not pertain to any known subtitle format."""
|
||||
|
||||
class UnknownFormatIdentifierError(Pysubs2Error):
|
||||
"""Unknown subtitle format identifier (ie. string like ``"srt"``)."""
|
||||
|
||||
class FormatAutodetectionError(Pysubs2Error):
|
||||
"""Subtitle format is ambiguous or unknown."""
|
||||
@@ -0,0 +1,76 @@
|
||||
class FormatBase(object):
|
||||
"""
|
||||
Base class for subtitle format implementations.
|
||||
|
||||
How to implement a new subtitle format:
|
||||
|
||||
1. Create a subclass of FormatBase and override the methods you want to support.
|
||||
2. Decide on a format identifier, like the ``"srt"`` or ``"microdvd"`` already used in the library.
|
||||
3. Add your identifier and class to :data:`pysubs2.formats.FORMAT_IDENTIFIER_TO_FORMAT_CLASS`.
|
||||
4. (optional) Add your file extension and class to :data:`pysubs2.formats.FILE_EXTENSION_TO_FORMAT_IDENTIFIER`.
|
||||
|
||||
After finishing these steps, you can call :meth:`SSAFile.load()` and :meth:`SSAFile.save()` with your
|
||||
format, including autodetection from content and file extension (if you provided these).
|
||||
|
||||
"""
|
||||
@classmethod
|
||||
def from_file(cls, subs, fp, format_, **kwargs):
|
||||
"""
|
||||
Load subtitle file into an empty SSAFile.
|
||||
|
||||
If the parser autodetects framerate, set it as ``subs.fps``.
|
||||
|
||||
Arguments:
|
||||
subs (SSAFile): An empty :class:`SSAFile`.
|
||||
fp (file object): Text file object, the subtitle file.
|
||||
format_ (str): Format identifier. Used when one format class
|
||||
implements multiple formats (see :class:`SubstationFormat`).
|
||||
kwargs: Extra options, eg. `fps`.
|
||||
|
||||
Returns:
|
||||
None
|
||||
|
||||
Raises:
|
||||
pysubs2.exceptions.UnknownFPSError: Framerate was not provided and cannot
|
||||
be detected.
|
||||
"""
|
||||
raise NotImplementedError("Parsing is not supported for this format")
|
||||
|
||||
@classmethod
|
||||
def to_file(cls, subs, fp, format_, **kwargs):
|
||||
"""
|
||||
Write SSAFile into a file.
|
||||
|
||||
If you need framerate and it is not passed in keyword arguments,
|
||||
use ``subs.fps``.
|
||||
|
||||
Arguments:
|
||||
subs (SSAFile): Subtitle file to write.
|
||||
fp (file object): Text file object used as output.
|
||||
format_ (str): Format identifier of desired output format.
|
||||
Used when one format class implements multiple formats
|
||||
(see :class:`SubstationFormat`).
|
||||
kwargs: Extra options, eg. `fps`.
|
||||
|
||||
Returns:
|
||||
None
|
||||
|
||||
Raises:
|
||||
pysubs2.exceptions.UnknownFPSError: Framerate was not provided and
|
||||
``subs.fps is None``.
|
||||
"""
|
||||
raise NotImplementedError("Writing is not supported for this format")
|
||||
|
||||
@classmethod
|
||||
def guess_format(self, text):
|
||||
"""
|
||||
Return format identifier of recognized format, or None.
|
||||
|
||||
Arguments:
|
||||
text (str): Content of subtitle file. When the file is long,
|
||||
this may be only its first few thousand characters.
|
||||
|
||||
Returns:
|
||||
format identifier (eg. ``"srt"``) or None (unknown format)
|
||||
"""
|
||||
return None
|
||||
@@ -0,0 +1,64 @@
|
||||
from .formatbase import FormatBase
|
||||
from .microdvd import MicroDVDFormat
|
||||
from .subrip import SubripFormat
|
||||
from .jsonformat import JSONFormat
|
||||
from .substation import SubstationFormat
|
||||
from .exceptions import *
|
||||
|
||||
#: Dict mapping file extensions to format identifiers.
|
||||
FILE_EXTENSION_TO_FORMAT_IDENTIFIER = {
|
||||
".srt": "srt",
|
||||
".ass": "ass",
|
||||
".ssa": "ssa",
|
||||
".sub": "microdvd",
|
||||
".json": "json"
|
||||
}
|
||||
|
||||
#: Dict mapping format identifiers to implementations (FormatBase subclasses).
|
||||
FORMAT_IDENTIFIER_TO_FORMAT_CLASS = {
|
||||
"srt": SubripFormat,
|
||||
"ass": SubstationFormat,
|
||||
"ssa": SubstationFormat,
|
||||
"microdvd": MicroDVDFormat,
|
||||
"json": JSONFormat
|
||||
}
|
||||
|
||||
def get_format_class(format_):
|
||||
"""Format identifier -> format class (ie. subclass of FormatBase)"""
|
||||
try:
|
||||
return FORMAT_IDENTIFIER_TO_FORMAT_CLASS[format_]
|
||||
except KeyError:
|
||||
raise UnknownFormatIdentifierError(format_)
|
||||
|
||||
def get_format_identifier(ext):
|
||||
"""File extension -> format identifier"""
|
||||
try:
|
||||
return FILE_EXTENSION_TO_FORMAT_IDENTIFIER[ext]
|
||||
except KeyError:
|
||||
raise UnknownFileExtensionError(ext)
|
||||
|
||||
def get_file_extension(format_):
|
||||
"""Format identifier -> file extension"""
|
||||
if format_ not in FORMAT_IDENTIFIER_TO_FORMAT_CLASS:
|
||||
raise UnknownFormatIdentifierError(format_)
|
||||
|
||||
for ext, f in FILE_EXTENSION_TO_FORMAT_IDENTIFIER.items():
|
||||
if f == format_:
|
||||
return ext
|
||||
|
||||
raise RuntimeError("No file extension for format %r" % format_)
|
||||
|
||||
def autodetect_format(content):
|
||||
"""Return format identifier for given fragment or raise FormatAutodetectionError."""
|
||||
formats = set()
|
||||
for impl in FORMAT_IDENTIFIER_TO_FORMAT_CLASS.values():
|
||||
guess = impl.guess_format(content)
|
||||
if guess is not None:
|
||||
formats.add(guess)
|
||||
|
||||
if len(formats) == 1:
|
||||
return formats.pop()
|
||||
elif not formats:
|
||||
raise FormatAutodetectionError("No suitable formats")
|
||||
else:
|
||||
raise FormatAutodetectionError("Multiple suitable formats (%r)" % formats)
|
||||
@@ -0,0 +1,46 @@
|
||||
from __future__ import unicode_literals, print_function
|
||||
|
||||
import json
|
||||
from .common import Color, PY3
|
||||
from .ssaevent import SSAEvent
|
||||
from .ssastyle import SSAStyle
|
||||
from .formatbase import FormatBase
|
||||
|
||||
|
||||
class JSONFormat(FormatBase):
|
||||
@classmethod
|
||||
def guess_format(cls, text):
|
||||
if text.startswith("{\""):
|
||||
return "json"
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, subs, fp, format_, **kwargs):
|
||||
data = json.load(fp)
|
||||
|
||||
subs.info.clear()
|
||||
subs.info.update(data["info"])
|
||||
|
||||
subs.styles.clear()
|
||||
for name, fields in data["styles"].items():
|
||||
subs.styles[name] = sty = SSAStyle()
|
||||
for k, v in fields.items():
|
||||
if "color" in k:
|
||||
setattr(sty, k, Color(*v))
|
||||
else:
|
||||
setattr(sty, k, v)
|
||||
|
||||
subs.events = [SSAEvent(**fields) for fields in data["events"]]
|
||||
|
||||
@classmethod
|
||||
def to_file(cls, subs, fp, format_, **kwargs):
|
||||
data = {
|
||||
"info": dict(**subs.info),
|
||||
"styles": {name: sty.as_dict() for name, sty in subs.styles.items()},
|
||||
"events": [ev.as_dict() for ev in subs.events]
|
||||
}
|
||||
|
||||
if PY3:
|
||||
json.dump(data, fp)
|
||||
else:
|
||||
text = json.dumps(data, fp)
|
||||
fp.write(unicode(text))
|
||||
@@ -0,0 +1,103 @@
|
||||
from __future__ import unicode_literals, print_function
|
||||
|
||||
from functools import partial
|
||||
import re
|
||||
from .common import text_type
|
||||
from .exceptions import UnknownFPSError
|
||||
from .ssaevent import SSAEvent
|
||||
from .ssastyle import SSAStyle
|
||||
from .formatbase import FormatBase
|
||||
from .substation import parse_tags
|
||||
from .time import ms_to_frames, frames_to_ms
|
||||
|
||||
#: Matches a MicroDVD line.
|
||||
MICRODVD_LINE = re.compile(r" *\{ *(\d+) *\} *\{ *(\d+) *\}(.+)")
|
||||
|
||||
|
||||
class MicroDVDFormat(FormatBase):
|
||||
@classmethod
|
||||
def guess_format(cls, text):
|
||||
if any(map(MICRODVD_LINE.match, text.splitlines())):
|
||||
return "microdvd"
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, subs, fp, format_, fps=None, **kwargs):
|
||||
for line in fp:
|
||||
match = MICRODVD_LINE.match(line)
|
||||
if not match:
|
||||
continue
|
||||
|
||||
fstart, fend, text = match.groups()
|
||||
fstart, fend = map(int, (fstart, fend))
|
||||
|
||||
if fps is None:
|
||||
# We don't know the framerate, but it is customary to include
|
||||
# it as text of the first subtitle. In that case, we skip
|
||||
# this auxiliary subtitle and proceed with reading.
|
||||
try:
|
||||
fps = float(text)
|
||||
subs.fps = fps
|
||||
continue
|
||||
except ValueError:
|
||||
raise UnknownFPSError("Framerate was not specified and "
|
||||
"cannot be read from "
|
||||
"the MicroDVD file.")
|
||||
|
||||
start, end = map(partial(frames_to_ms, fps=fps), (fstart, fend))
|
||||
|
||||
def prepare_text(text):
|
||||
text = text.replace("|", r"\N")
|
||||
|
||||
def style_replacer(match):
|
||||
tags = [c for c in "biu" if c in match.group(0)]
|
||||
return "{%s}" % "".join(r"\%s1" % c for c in tags)
|
||||
|
||||
text = re.sub(r"\{[Yy]:[^}]+\}", style_replacer, text)
|
||||
text = re.sub(r"\{[Ff]:([^}]+)\}", r"{\\fn\1}", text)
|
||||
text = re.sub(r"\{[Ss]:([^}]+)\}", r"{\\fs\1}", text)
|
||||
text = re.sub(r"\{P:(\d+),(\d+)\}", r"{\\pos(\1,\2)}", text)
|
||||
|
||||
return text.strip()
|
||||
|
||||
ev = SSAEvent(start=start, end=end, text=prepare_text(text))
|
||||
subs.append(ev)
|
||||
|
||||
@classmethod
|
||||
def to_file(cls, subs, fp, format_, fps=None, write_fps_declaration=True, **kwargs):
|
||||
if fps is None:
|
||||
fps = subs.fps
|
||||
|
||||
if fps is None:
|
||||
raise UnknownFPSError("Framerate must be specified when writing MicroDVD.")
|
||||
to_frames = partial(ms_to_frames, fps=fps)
|
||||
|
||||
def is_entirely_italic(line):
|
||||
style = subs.styles.get(line.style, SSAStyle.DEFAULT_STYLE)
|
||||
for fragment, sty in parse_tags(line.text, style, subs.styles):
|
||||
fragment = fragment.replace(r"\h", " ")
|
||||
fragment = fragment.replace(r"\n", "\n")
|
||||
fragment = fragment.replace(r"\N", "\n")
|
||||
if not sty.italic and fragment and not fragment.isspace():
|
||||
return False
|
||||
return True
|
||||
|
||||
# insert an artificial first line telling the framerate
|
||||
if write_fps_declaration:
|
||||
subs.insert(0, SSAEvent(start=0, end=0, text=text_type(fps)))
|
||||
|
||||
for line in (ev for ev in subs if not ev.is_comment):
|
||||
text = "|".join(line.plaintext.splitlines())
|
||||
if is_entirely_italic(line):
|
||||
text = "{Y:i}" + text
|
||||
|
||||
start, end = map(to_frames, (line.start, line.end))
|
||||
|
||||
# XXX warn on underflow?
|
||||
if start < 0: start = 0
|
||||
if end < 0: end = 0
|
||||
|
||||
print("{%d}{%d}%s" % (start, end, text), file=fp)
|
||||
|
||||
# remove the artificial framerate-telling line
|
||||
if write_fps_declaration:
|
||||
subs.pop(0)
|
||||
@@ -0,0 +1,153 @@
|
||||
from __future__ import unicode_literals
|
||||
import re
|
||||
from .time import ms_to_str, make_time
|
||||
from .common import PY3
|
||||
|
||||
|
||||
class SSAEvent(object):
|
||||
"""
|
||||
A SubStation Event, ie. one subtitle.
|
||||
|
||||
In SubStation, each subtitle consists of multiple "fields" like Start, End and Text.
|
||||
These are exposed as attributes (note that they are lowercase; see :attr:`SSAEvent.FIELDS` for a list).
|
||||
Additionaly, there are some convenience properties like :attr:`SSAEvent.plaintext` or :attr:`SSAEvent.duration`.
|
||||
|
||||
This class defines an ordering with respect to (start, end) timestamps.
|
||||
|
||||
.. tip :: Use :func:`pysubs2.make_time()` to get times in milliseconds.
|
||||
|
||||
Example::
|
||||
|
||||
>>> ev = SSAEvent(start=make_time(s=1), end=make_time(s=2.5), text="Hello World!")
|
||||
|
||||
"""
|
||||
OVERRIDE_SEQUENCE = re.compile(r"{[^}]*}")
|
||||
|
||||
#: All fields in SSAEvent.
|
||||
FIELDS = frozenset([
|
||||
"start", "end", "text", "marked", "layer", "style",
|
||||
"name", "marginl", "marginr", "marginv", "effect", "type"
|
||||
])
|
||||
|
||||
def __init__(self, **fields):
|
||||
self.start = 0 #: Subtitle start time (in milliseconds)
|
||||
self.end = 10000 #: Subtitle end time (in milliseconds)
|
||||
self.text = "" #: Text of subtitle (with SubStation override tags)
|
||||
self.marked = False #: (SSA only)
|
||||
self.layer = 0 #: Layer number, 0 is the lowest layer (ASS only)
|
||||
self.style = "Default" #: Style name
|
||||
self.name = "" #: Actor name
|
||||
self.marginl = 0 #: Left margin
|
||||
self.marginr = 0 #: Right margin
|
||||
self.marginv = 0 #: Vertical margin
|
||||
self.effect = "" #: Line effect
|
||||
self.type = "Dialogue" #: Line type (Dialogue/Comment)
|
||||
|
||||
for k, v in fields.items():
|
||||
if k in self.FIELDS:
|
||||
setattr(self, k, v)
|
||||
else:
|
||||
raise ValueError("SSAEvent has no field named %r" % k)
|
||||
|
||||
@property
|
||||
def duration(self):
|
||||
"""
|
||||
Subtitle duration in milliseconds (read/write property).
|
||||
|
||||
Writing to this property adjusts :attr:`SSAEvent.end`.
|
||||
Setting negative durations raises :exc:`ValueError`.
|
||||
"""
|
||||
return self.end - self.start
|
||||
|
||||
@duration.setter
|
||||
def duration(self, ms):
|
||||
if ms >= 0:
|
||||
self.end = self.start + ms
|
||||
else:
|
||||
raise ValueError("Subtitle duration cannot be negative")
|
||||
|
||||
@property
|
||||
def is_comment(self):
|
||||
"""
|
||||
When true, the subtitle is a comment, ie. not visible (read/write property).
|
||||
|
||||
Setting this property is equivalent to changing
|
||||
:attr:`SSAEvent.type` to ``"Dialogue"`` or ``"Comment"``.
|
||||
"""
|
||||
return self.type == "Comment"
|
||||
|
||||
@is_comment.setter
|
||||
def is_comment(self, value):
|
||||
if value:
|
||||
self.type = "Comment"
|
||||
else:
|
||||
self.type = "Dialogue"
|
||||
|
||||
@property
|
||||
def plaintext(self):
|
||||
"""
|
||||
Subtitle text as multi-line string with no tags (read/write property).
|
||||
|
||||
Writing to this property replaces :attr:`SSAEvent.text` with given plain
|
||||
text. Newlines are converted to ``\\N`` tags.
|
||||
"""
|
||||
text = self.text
|
||||
text = self.OVERRIDE_SEQUENCE.sub("", text)
|
||||
text = text.replace(r"\h", " ")
|
||||
text = text.replace(r"\n", "\n")
|
||||
text = text.replace(r"\N", "\n")
|
||||
return text
|
||||
|
||||
@plaintext.setter
|
||||
def plaintext(self, text):
|
||||
self.text = text.replace("\n", r"\N")
|
||||
|
||||
def shift(self, h=0, m=0, s=0, ms=0, frames=None, fps=None):
|
||||
"""
|
||||
Shift start and end times.
|
||||
|
||||
See :meth:`SSAFile.shift()` for full description.
|
||||
|
||||
"""
|
||||
delta = make_time(h=h, m=m, s=s, ms=ms, frames=frames, fps=fps)
|
||||
self.start += delta
|
||||
self.end += delta
|
||||
|
||||
def copy(self):
|
||||
"""Return a copy of the SSAEvent."""
|
||||
return SSAEvent(**self.as_dict())
|
||||
|
||||
def as_dict(self):
|
||||
return {field: getattr(self, field) for field in self.FIELDS}
|
||||
|
||||
def equals(self, other):
|
||||
"""Field-based equality for SSAEvents."""
|
||||
if isinstance(other, SSAEvent):
|
||||
return self.as_dict() == other.as_dict()
|
||||
else:
|
||||
raise TypeError("Cannot compare to non-SSAEvent object")
|
||||
|
||||
def __eq__(self, other):
|
||||
# XXX document this
|
||||
return self.start == other.start and self.end == other.end
|
||||
|
||||
def __ne__(self, other):
|
||||
return self.start != other.start or self.end != other.end
|
||||
|
||||
def __lt__(self, other):
|
||||
return (self.start, self.end) < (other.start, other.end)
|
||||
|
||||
def __le__(self, other):
|
||||
return (self.start, self.end) <= (other.start, other.end)
|
||||
|
||||
def __gt__(self, other):
|
||||
return (self.start, self.end) > (other.start, other.end)
|
||||
|
||||
def __ge__(self, other):
|
||||
return (self.start, self.end) >= (other.start, other.end)
|
||||
|
||||
def __repr__(self):
|
||||
s = "<SSAEvent type={self.type} start={start} end={end} text='{self.text}'>".format(
|
||||
self=self, start=ms_to_str(self.start), end=ms_to_str(self.end))
|
||||
if not PY3: s = s.encode("utf-8")
|
||||
return s
|
||||
@@ -0,0 +1,419 @@
|
||||
from __future__ import print_function, unicode_literals, division
|
||||
from collections import MutableSequence, OrderedDict
|
||||
import io
|
||||
from io import open
|
||||
from itertools import starmap, chain
|
||||
import os.path
|
||||
import logging
|
||||
from .formats import autodetect_format, get_format_class, get_format_identifier
|
||||
from .substation import is_valid_field_content
|
||||
from .ssaevent import SSAEvent
|
||||
from .ssastyle import SSAStyle
|
||||
from .time import make_time, ms_to_str
|
||||
from .common import PY3
|
||||
|
||||
|
||||
class SSAFile(MutableSequence):
|
||||
"""
|
||||
Subtitle file in SubStation Alpha format.
|
||||
|
||||
This class has a list-like interface which exposes :attr:`SSAFile.events`,
|
||||
list of subtitles in the file::
|
||||
|
||||
subs = SSAFile.load("subtitles.srt")
|
||||
|
||||
for line in subs:
|
||||
print(line.text)
|
||||
|
||||
subs.insert(0, SSAEvent(start=0, end=make_time(s=2.5), text="New first subtitle"))
|
||||
|
||||
del subs[0]
|
||||
|
||||
"""
|
||||
|
||||
DEFAULT_INFO = OrderedDict([
|
||||
("WrapStyle", "0"),
|
||||
("ScaledBorderAndShadow", "yes"),
|
||||
("Collisions", "Normal")])
|
||||
|
||||
def __init__(self):
|
||||
self.events = [] #: List of :class:`SSAEvent` instances, ie. individual subtitles.
|
||||
self.styles = OrderedDict([("Default", SSAStyle.DEFAULT_STYLE.copy())]) #: Dict of :class:`SSAStyle` instances.
|
||||
self.info = self.DEFAULT_INFO.copy() #: Dict with script metadata, ie. ``[Script Info]``.
|
||||
self.aegisub_project = OrderedDict() #: Dict with Aegisub project, ie. ``[Aegisub Project Garbage]``.
|
||||
self.fps = None #: Framerate used when reading the file, if applicable.
|
||||
self.format = None #: Format of source subtitle file, if applicable, eg. ``"srt"``.
|
||||
|
||||
# ------------------------------------------------------------------------
|
||||
# I/O methods
|
||||
# ------------------------------------------------------------------------
|
||||
|
||||
@classmethod
|
||||
def load(cls, path, encoding="utf-8", format_=None, fps=None, **kwargs):
|
||||
"""
|
||||
Load subtitle file from given path.
|
||||
|
||||
Arguments:
|
||||
path (str): Path to subtitle file.
|
||||
encoding (str): Character encoding of input file.
|
||||
Defaults to UTF-8, you may need to change this.
|
||||
format_ (str): Optional, forces use of specific parser
|
||||
(eg. `"srt"`, `"ass"`). Otherwise, format is detected
|
||||
automatically from file contents. This argument should
|
||||
be rarely needed.
|
||||
fps (float): Framerate for frame-based formats (MicroDVD),
|
||||
for other formats this argument is ignored. Framerate might
|
||||
be detected from the file, in which case you don't need
|
||||
to specify it here (when given, this argument overrides
|
||||
autodetection).
|
||||
kwargs: Extra options for the parser.
|
||||
|
||||
Returns:
|
||||
SSAFile
|
||||
|
||||
Raises:
|
||||
IOError
|
||||
UnicodeDecodeError
|
||||
pysubs2.exceptions.UnknownFPSError
|
||||
pysubs2.exceptions.UnknownFormatIdentifierError
|
||||
pysubs2.exceptions.FormatAutodetectionError
|
||||
|
||||
Note:
|
||||
pysubs2 may autodetect subtitle format and/or framerate. These
|
||||
values are set as :attr:`SSAFile.format` and :attr:`SSAFile.fps`
|
||||
attributes.
|
||||
|
||||
Example:
|
||||
>>> subs1 = pysubs2.load("subrip-subtitles.srt")
|
||||
>>> subs2 = pysubs2.load("microdvd-subtitles.sub", fps=23.976)
|
||||
|
||||
"""
|
||||
with open(path, encoding=encoding) as fp:
|
||||
return cls.from_file(fp, format_, fps=fps, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def from_string(cls, string, format_=None, fps=None, **kwargs):
|
||||
"""
|
||||
Load subtitle file from string.
|
||||
|
||||
See :meth:`SSAFile.load()` for full description.
|
||||
|
||||
Arguments:
|
||||
string (str): Subtitle file in a string. Note that the string
|
||||
must be Unicode (in Python 2).
|
||||
|
||||
Returns:
|
||||
SSAFile
|
||||
|
||||
Example:
|
||||
>>> text = '''
|
||||
... 1
|
||||
... 00:00:00,000 --> 00:00:05,000
|
||||
... An example SubRip file.
|
||||
... '''
|
||||
>>> subs = SSAFile.from_string(text)
|
||||
|
||||
"""
|
||||
fp = io.StringIO(string)
|
||||
return cls.from_file(fp, format_, fps=fps, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, fp, format_=None, fps=None, **kwargs):
|
||||
"""
|
||||
Read subtitle file from file object.
|
||||
|
||||
See :meth:`SSAFile.load()` for full description.
|
||||
|
||||
Note:
|
||||
This is a low-level method. Usually, one of :meth:`SSAFile.load()`
|
||||
or :meth:`SSAFile.from_string()` is preferable.
|
||||
|
||||
Arguments:
|
||||
fp (file object): A file object, ie. :class:`io.TextIOBase` instance.
|
||||
Note that the file must be opened in text mode (as opposed to binary).
|
||||
|
||||
Returns:
|
||||
SSAFile
|
||||
|
||||
"""
|
||||
if format_ is None:
|
||||
# Autodetect subtitle format, then read again using correct parser.
|
||||
# The file might be a pipe and we need to read it twice,
|
||||
# so just buffer everything.
|
||||
text = fp.read()
|
||||
fragment = text[:10000]
|
||||
format_ = autodetect_format(fragment)
|
||||
fp = io.StringIO(text)
|
||||
|
||||
impl = get_format_class(format_)
|
||||
subs = cls() # an empty subtitle file
|
||||
subs.format = format_
|
||||
subs.fps = fps
|
||||
impl.from_file(subs, fp, format_, fps=fps, **kwargs)
|
||||
return subs
|
||||
|
||||
def save(self, path, encoding="utf-8", format_=None, fps=None, **kwargs):
|
||||
"""
|
||||
Save subtitle file to given path.
|
||||
|
||||
Arguments:
|
||||
path (str): Path to subtitle file.
|
||||
encoding (str): Character encoding of output file.
|
||||
Defaults to UTF-8, which should be fine for most purposes.
|
||||
format_ (str): Optional, specifies desired subtitle format
|
||||
(eg. `"srt"`, `"ass"`). Otherwise, format is detected
|
||||
automatically from file extension. Thus, this argument
|
||||
is rarely needed.
|
||||
fps (float): Framerate for frame-based formats (MicroDVD),
|
||||
for other formats this argument is ignored. When omitted,
|
||||
:attr:`SSAFile.fps` value is used (ie. the framerate used
|
||||
for loading the file, if any). When the :class:`SSAFile`
|
||||
wasn't loaded from MicroDVD, or if you wish save it with
|
||||
different framerate, use this argument. See also
|
||||
:meth:`SSAFile.transform_framerate()` for fixing bad
|
||||
frame-based to time-based conversions.
|
||||
kwargs: Extra options for the writer.
|
||||
|
||||
Raises:
|
||||
IOError
|
||||
UnicodeEncodeError
|
||||
pysubs2.exceptions.UnknownFPSError
|
||||
pysubs2.exceptions.UnknownFormatIdentifierError
|
||||
pysubs2.exceptions.UnknownFileExtensionError
|
||||
|
||||
"""
|
||||
if format_ is None:
|
||||
ext = os.path.splitext(path)[1].lower()
|
||||
format_ = get_format_identifier(ext)
|
||||
|
||||
with open(path, "w", encoding=encoding) as fp:
|
||||
self.to_file(fp, format_, fps=fps, **kwargs)
|
||||
|
||||
def to_string(self, format_, fps=None, **kwargs):
|
||||
"""
|
||||
Get subtitle file as a string.
|
||||
|
||||
See :meth:`SSAFile.save()` for full description.
|
||||
|
||||
Returns:
|
||||
str
|
||||
|
||||
"""
|
||||
fp = io.StringIO()
|
||||
self.to_file(fp, format_, fps=fps, **kwargs)
|
||||
return fp.getvalue()
|
||||
|
||||
def to_file(self, fp, format_, fps=None, **kwargs):
|
||||
"""
|
||||
Write subtitle file to file object.
|
||||
|
||||
See :meth:`SSAFile.save()` for full description.
|
||||
|
||||
Note:
|
||||
This is a low-level method. Usually, one of :meth:`SSAFile.save()`
|
||||
or :meth:`SSAFile.to_string()` is preferable.
|
||||
|
||||
Arguments:
|
||||
fp (file object): A file object, ie. :class:`io.TextIOBase` instance.
|
||||
Note that the file must be opened in text mode (as opposed to binary).
|
||||
|
||||
"""
|
||||
impl = get_format_class(format_)
|
||||
impl.to_file(self, fp, format_, fps=fps, **kwargs)
|
||||
|
||||
# ------------------------------------------------------------------------
|
||||
# Retiming subtitles
|
||||
# ------------------------------------------------------------------------
|
||||
|
||||
def shift(self, h=0, m=0, s=0, ms=0, frames=None, fps=None):
|
||||
"""
|
||||
Shift all subtitles by constant time amount.
|
||||
|
||||
Shift may be time-based (the default) or frame-based. In the latter
|
||||
case, specify both frames and fps. h, m, s, ms will be ignored.
|
||||
|
||||
Arguments:
|
||||
h, m, s, ms: Integer or float values, may be positive or negative.
|
||||
frames (int): When specified, must be an integer number of frames.
|
||||
May be positive or negative. fps must be also specified.
|
||||
fps (float): When specified, must be a positive number.
|
||||
|
||||
Raises:
|
||||
ValueError: Invalid fps or missing number of frames.
|
||||
|
||||
"""
|
||||
delta = make_time(h=h, m=m, s=s, ms=ms, frames=frames, fps=fps)
|
||||
for line in self:
|
||||
line.start += delta
|
||||
line.end += delta
|
||||
|
||||
def transform_framerate(self, in_fps, out_fps):
|
||||
"""
|
||||
Rescale all timestamps by ratio of in_fps/out_fps.
|
||||
|
||||
Can be used to fix files converted from frame-based to time-based
|
||||
with wrongly assumed framerate.
|
||||
|
||||
Arguments:
|
||||
in_fps (float)
|
||||
out_fps (float)
|
||||
|
||||
Raises:
|
||||
ValueError: Non-positive framerate given.
|
||||
|
||||
"""
|
||||
if in_fps <= 0 or out_fps <= 0:
|
||||
raise ValueError("Framerates must be positive, cannot transform %f -> %f" % (in_fps, out_fps))
|
||||
|
||||
ratio = in_fps / out_fps
|
||||
for line in self:
|
||||
line.start = int(round(line.start * ratio))
|
||||
line.end = int(round(line.end * ratio))
|
||||
|
||||
# ------------------------------------------------------------------------
|
||||
# Working with styles
|
||||
# ------------------------------------------------------------------------
|
||||
|
||||
def rename_style(self, old_name, new_name):
|
||||
"""
|
||||
Rename a style, including references to it.
|
||||
|
||||
Arguments:
|
||||
old_name (str): Style to be renamed.
|
||||
new_name (str): New name for the style (must be unused).
|
||||
|
||||
Raises:
|
||||
KeyError: No style named old_name.
|
||||
ValueError: new_name is not a legal name (cannot use commas)
|
||||
or new_name is taken.
|
||||
|
||||
"""
|
||||
if old_name not in self.styles:
|
||||
raise KeyError("Style %r not found" % old_name)
|
||||
if new_name in self.styles:
|
||||
raise ValueError("There is already a style called %r" % new_name)
|
||||
if not is_valid_field_content(new_name):
|
||||
raise ValueError("%r is not a valid name" % new_name)
|
||||
|
||||
self.styles[new_name] = self.styles[old_name]
|
||||
del self.styles[old_name]
|
||||
|
||||
for line in self:
|
||||
# XXX also handle \r override tag
|
||||
if line.style == old_name:
|
||||
line.style = new_name
|
||||
|
||||
def import_styles(self, subs, overwrite=True):
|
||||
"""
|
||||
Merge in styles from other SSAFile.
|
||||
|
||||
Arguments:
|
||||
subs (SSAFile): Subtitle file imported from.
|
||||
overwrite (bool): On name conflict, use style from the other file
|
||||
(default: True).
|
||||
|
||||
"""
|
||||
if not isinstance(subs, SSAFile):
|
||||
raise TypeError("Must supply an SSAFile.")
|
||||
|
||||
for name, style in subs.styles.items():
|
||||
if name not in self.styles or overwrite:
|
||||
self.styles[name] = style
|
||||
|
||||
# ------------------------------------------------------------------------
|
||||
# Helper methods
|
||||
# ------------------------------------------------------------------------
|
||||
|
||||
def equals(self, other):
|
||||
"""
|
||||
Equality of two SSAFiles.
|
||||
|
||||
Compares :attr:`SSAFile.info`, :attr:`SSAFile.styles` and :attr:`SSAFile.events`.
|
||||
Order of entries in OrderedDicts does not matter. "ScriptType" key in info is
|
||||
considered an implementation detail and thus ignored.
|
||||
|
||||
Useful mostly in unit tests. Differences are logged at DEBUG level.
|
||||
|
||||
"""
|
||||
|
||||
if isinstance(other, SSAFile):
|
||||
for key in set(chain(self.info.keys(), other.info.keys())) - {"ScriptType"}:
|
||||
sv, ov = self.info.get(key), other.info.get(key)
|
||||
if sv is None:
|
||||
logging.debug("%r missing in self.info", key)
|
||||
return False
|
||||
elif ov is None:
|
||||
logging.debug("%r missing in other.info", key)
|
||||
return False
|
||||
elif sv != ov:
|
||||
logging.debug("info %r differs (self=%r, other=%r)", key, sv, ov)
|
||||
return False
|
||||
|
||||
for key in set(chain(self.styles.keys(), other.styles.keys())):
|
||||
sv, ov = self.styles.get(key), other.styles.get(key)
|
||||
if sv is None:
|
||||
logging.debug("%r missing in self.styles", key)
|
||||
return False
|
||||
elif ov is None:
|
||||
logging.debug("%r missing in other.styles", key)
|
||||
return False
|
||||
elif sv != ov:
|
||||
for k in sv.FIELDS:
|
||||
if getattr(sv, k) != getattr(ov, k): logging.debug("difference in field %r", k)
|
||||
logging.debug("style %r differs (self=%r, other=%r)", key, sv.as_dict(), ov.as_dict())
|
||||
return False
|
||||
|
||||
if len(self) != len(other):
|
||||
logging.debug("different # of subtitles (self=%d, other=%d)", len(self), len(other))
|
||||
return False
|
||||
|
||||
for i, (se, oe) in enumerate(zip(self.events, other.events)):
|
||||
if not se.equals(oe):
|
||||
for k in se.FIELDS:
|
||||
if getattr(se, k) != getattr(oe, k): logging.debug("difference in field %r", k)
|
||||
logging.debug("event %d differs (self=%r, other=%r)", i, se.as_dict(), oe.as_dict())
|
||||
return False
|
||||
|
||||
return True
|
||||
else:
|
||||
raise TypeError("Cannot compare to non-SSAFile object")
|
||||
|
||||
def __repr__(self):
|
||||
if self.events:
|
||||
max_time = max(ev.end for ev in self)
|
||||
s = "<SSAFile with %d events and %d styles, last timestamp %s>" % \
|
||||
(len(self), len(self.styles), ms_to_str(max_time))
|
||||
else:
|
||||
s = "<SSAFile with 0 events and %d styles>" % len(self.styles)
|
||||
|
||||
if not PY3: s = s.encode("utf-8")
|
||||
return s
|
||||
|
||||
# ------------------------------------------------------------------------
|
||||
# MutableSequence implementation + sort()
|
||||
# ------------------------------------------------------------------------
|
||||
|
||||
def sort(self):
|
||||
"""Sort subtitles time-wise, in-place."""
|
||||
self.events.sort()
|
||||
|
||||
def __getitem__(self, item):
|
||||
return self.events[item]
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if isinstance(value, SSAEvent):
|
||||
self.events[key] = value
|
||||
else:
|
||||
raise TypeError("SSAFile.events must contain only SSAEvent objects")
|
||||
|
||||
def __delitem__(self, key):
|
||||
del self.events[key]
|
||||
|
||||
def __len__(self):
|
||||
return len(self.events)
|
||||
|
||||
def insert(self, index, value):
|
||||
if isinstance(value, SSAEvent):
|
||||
self.events.insert(index, value)
|
||||
else:
|
||||
raise TypeError("SSAFile.events must contain only SSAEvent objects")
|
||||
@@ -0,0 +1,86 @@
|
||||
from __future__ import unicode_literals
|
||||
from .common import Color, PY3
|
||||
|
||||
|
||||
class SSAStyle(object):
|
||||
"""
|
||||
A SubStation Style.
|
||||
|
||||
In SubStation, each subtitle (:class:`SSAEvent`) is associated with a style which defines its font, color, etc.
|
||||
Like a subtitle event, a style also consists of "fields"; see :attr:`SSAStyle.FIELDS` for a list
|
||||
(note the spelling, which is different from SubStation proper).
|
||||
|
||||
Subtitles and styles are connected via an :class:`SSAFile` they belong to. :attr:`SSAEvent.style` is a string
|
||||
which is (or should be) a key in the :attr:`SSAFile.styles` dict. Note that style name is stored separately;
|
||||
a given :class:`SSAStyle` instance has no particular name itself.
|
||||
|
||||
This class defines equality (equality of all fields).
|
||||
|
||||
"""
|
||||
DEFAULT_STYLE = None
|
||||
|
||||
#: All fields in SSAStyle.
|
||||
FIELDS = frozenset([
|
||||
"fontname", "fontsize", "primarycolor", "secondarycolor",
|
||||
"tertiarycolor", "outlinecolor", "backcolor",
|
||||
"bold", "italic", "underline", "strikeout",
|
||||
"scalex", "scaley", "spacing", "angle", "borderstyle",
|
||||
"outline", "shadow", "alignment",
|
||||
"marginl", "marginr", "marginv", "alphalevel", "encoding"
|
||||
])
|
||||
|
||||
def __init__(self, **fields):
|
||||
self.fontname = "Arial" #: Font name
|
||||
self.fontsize = 20.0 #: Font size (in pixels)
|
||||
self.primarycolor = Color(255, 255, 255, 0) #: Primary color (:class:`pysubs2.Color` instance)
|
||||
self.secondarycolor = Color(255, 0, 0, 0) #: Secondary color (:class:`pysubs2.Color` instance)
|
||||
self.tertiarycolor = Color(0, 0, 0, 0) #: Tertiary color (:class:`pysubs2.Color` instance)
|
||||
self.outlinecolor = Color(0, 0, 0, 0) #: Outline color (:class:`pysubs2.Color` instance)
|
||||
self.backcolor = Color(0, 0, 0, 0) #: Back, ie. shadow color (:class:`pysubs2.Color` instance)
|
||||
self.bold = False #: Bold
|
||||
self.italic = False #: Italic
|
||||
self.underline = False #: Underline (ASS only)
|
||||
self.strikeout = False #: Strikeout (ASS only)
|
||||
self.scalex = 100.0 #: Horizontal scaling (ASS only)
|
||||
self.scaley = 100.0 #: Vertical scaling (ASS only)
|
||||
self.spacing = 0.0 #: Letter spacing (ASS only)
|
||||
self.angle = 0.0 #: Rotation (ASS only)
|
||||
self.borderstyle = 1 #: Border style
|
||||
self.outline = 2.0 #: Outline width (in pixels)
|
||||
self.shadow = 2.0 #: Shadow depth (in pixels)
|
||||
self.alignment = 2 #: Numpad-style alignment, eg. 7 is "top left" (that is, ASS alignment semantics)
|
||||
self.marginl = 10 #: Left margin (in pixels)
|
||||
self.marginr = 10 #: Right margin (in pixels)
|
||||
self.marginv = 10 #: Vertical margin (in pixels)
|
||||
self.alphalevel = 0 #: Old, unused SSA-only field
|
||||
self.encoding = 1 #: Charset
|
||||
|
||||
for k, v in fields.items():
|
||||
if k in self.FIELDS:
|
||||
setattr(self, k, v)
|
||||
else:
|
||||
raise ValueError("SSAStyle has no field named %r" % k)
|
||||
|
||||
def copy(self):
|
||||
return SSAStyle(**self.as_dict())
|
||||
|
||||
def as_dict(self):
|
||||
return {field: getattr(self, field) for field in self.FIELDS}
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.as_dict() == other.as_dict()
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
def __repr__(self):
|
||||
s = "<SSAStyle "
|
||||
s += "%rpx " % self.fontsize
|
||||
if self.bold: s += "bold "
|
||||
if self.italic: s += "italic "
|
||||
s += "'%s'>" % self.fontname
|
||||
if not PY3: s = s.encode("utf-8")
|
||||
return s
|
||||
|
||||
|
||||
SSAStyle.DEFAULT_STYLE = SSAStyle()
|
||||
@@ -0,0 +1,88 @@
|
||||
from __future__ import print_function, unicode_literals
|
||||
|
||||
import re
|
||||
from .formatbase import FormatBase
|
||||
from .ssaevent import SSAEvent
|
||||
from .ssastyle import SSAStyle
|
||||
from .substation import parse_tags
|
||||
from .time import ms_to_times, make_time, TIMESTAMP, timestamp_to_ms
|
||||
|
||||
#: Largest timestamp allowed in SubRip, ie. 99:59:59,999.
|
||||
MAX_REPRESENTABLE_TIME = make_time(h=100) - 1
|
||||
|
||||
def ms_to_timestamp(ms):
|
||||
"""Convert ms to 'HH:MM:SS,mmm'"""
|
||||
# XXX throw on overflow/underflow?
|
||||
if ms < 0: ms = 0
|
||||
if ms > MAX_REPRESENTABLE_TIME: ms = MAX_REPRESENTABLE_TIME
|
||||
h, m, s, ms = ms_to_times(ms)
|
||||
return "%02d:%02d:%02d,%03d" % (h, m, s, ms)
|
||||
|
||||
|
||||
class SubripFormat(FormatBase):
|
||||
@classmethod
|
||||
def guess_format(cls, text):
|
||||
if "[Script Info]" in text or "[V4+ Styles]" in text:
|
||||
# disambiguation vs. SSA/ASS
|
||||
return None
|
||||
|
||||
for line in text.splitlines():
|
||||
if len(TIMESTAMP.findall(line)) == 2:
|
||||
return "srt"
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, subs, fp, format_, **kwargs):
|
||||
timestamps = [] # (start, end)
|
||||
following_lines = [] # contains lists of lines following each timestamp
|
||||
|
||||
for line in fp:
|
||||
stamps = TIMESTAMP.findall(line)
|
||||
if len(stamps) == 2: # timestamp line
|
||||
start, end = map(timestamp_to_ms, stamps)
|
||||
timestamps.append((start, end))
|
||||
following_lines.append([])
|
||||
else:
|
||||
if timestamps:
|
||||
following_lines[-1].append(line)
|
||||
|
||||
def prepare_text(lines):
|
||||
s = "".join(lines).strip()
|
||||
s = re.sub(r"\n+ *\d+ *$", "", s) # strip number of next subtitle
|
||||
s = re.sub(r"< *i *>", r"{\i1}", s)
|
||||
s = re.sub(r"< */ *i *>", r"{\i0}", s)
|
||||
s = re.sub(r"< *s *>", r"{\s1}", s)
|
||||
s = re.sub(r"< */ *s *>", r"{\s0}", s)
|
||||
s = re.sub(r"< *u *>", "{\\u1}", s) # not r" for Python 2.7 compat, triggers unicodeescape
|
||||
s = re.sub(r"< */ *u *>", "{\\u0}", s)
|
||||
s = re.sub(r"< */? *[a-zA-Z][^>]*>", "", s) # strip other HTML tags
|
||||
s = re.sub(r"\n", r"\N", s) # convert newlines
|
||||
return s
|
||||
|
||||
subs.events = [SSAEvent(start=start, end=end, text=prepare_text(lines))
|
||||
for (start, end), lines in zip(timestamps, following_lines)]
|
||||
|
||||
@classmethod
|
||||
def to_file(cls, subs, fp, format_, **kwargs):
|
||||
def prepare_text(text, style):
|
||||
body = []
|
||||
for fragment, sty in parse_tags(text, style, subs.styles):
|
||||
fragment = fragment.replace(r"\h", " ")
|
||||
fragment = fragment.replace(r"\n", "\n")
|
||||
fragment = fragment.replace(r"\N", "\n")
|
||||
if sty.italic: fragment = "<i>%s</i>" % fragment
|
||||
if sty.underline: fragment = "<u>%s</u>" % fragment
|
||||
if sty.strikeout: fragment = "<s>%s</s>" % fragment
|
||||
body.append(fragment)
|
||||
|
||||
return re.sub("\n+", "\n", "".join(body).strip())
|
||||
|
||||
visible_lines = (line for line in subs if not line.is_comment)
|
||||
|
||||
for i, line in enumerate(visible_lines, 1):
|
||||
start = ms_to_timestamp(line.start)
|
||||
end = ms_to_timestamp(line.end)
|
||||
text = prepare_text(line.text, subs.styles.get(line.style, SSAStyle.DEFAULT_STYLE))
|
||||
|
||||
print("%d" % i, file=fp) # Python 2.7 compat
|
||||
print(start, "-->", end, file=fp)
|
||||
print(text, end="\n\n", file=fp)
|
||||
@@ -0,0 +1,255 @@
|
||||
from __future__ import print_function, division, unicode_literals
|
||||
import re
|
||||
from numbers import Number
|
||||
from .formatbase import FormatBase
|
||||
from .ssaevent import SSAEvent
|
||||
from .ssastyle import SSAStyle
|
||||
from .common import text_type, Color
|
||||
from .time import make_time, ms_to_times, timestamp_to_ms, TIMESTAMP
|
||||
|
||||
SSA_ALIGNMENT = (1, 2, 3, 9, 10, 11, 5, 6, 7)
|
||||
|
||||
def ass_to_ssa_alignment(i):
|
||||
return SSA_ALIGNMENT[i-1]
|
||||
|
||||
def ssa_to_ass_alignment(i):
|
||||
return SSA_ALIGNMENT.index(i) + 1
|
||||
|
||||
SECTION_HEADING = re.compile(r"^.{,3}\[[^\]]+\]") # allow for UTF-8 BOM, which is 3 bytes
|
||||
|
||||
STYLE_FORMAT_LINE = {
|
||||
"ass": "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic,"
|
||||
" Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment,"
|
||||
" MarginL, MarginR, MarginV, Encoding",
|
||||
"ssa": "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic,"
|
||||
" BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding"
|
||||
}
|
||||
|
||||
STYLE_FIELDS = {
|
||||
"ass": ["fontname", "fontsize", "primarycolor", "secondarycolor", "outlinecolor", "backcolor", "bold", "italic",
|
||||
"underline", "strikeout", "scalex", "scaley", "spacing", "angle", "borderstyle", "outline", "shadow",
|
||||
"alignment", "marginl", "marginr", "marginv", "encoding"],
|
||||
"ssa": ["fontname", "fontsize", "primarycolor", "secondarycolor", "tertiarycolor", "backcolor", "bold", "italic",
|
||||
"borderstyle", "outline", "shadow", "alignment", "marginl", "marginr", "marginv", "alphalevel", "encoding"]
|
||||
}
|
||||
|
||||
EVENT_FORMAT_LINE = {
|
||||
"ass": "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text",
|
||||
"ssa": "Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"
|
||||
}
|
||||
|
||||
EVENT_FIELDS = {
|
||||
"ass": ["layer", "start", "end", "style", "name", "marginl", "marginr", "marginv", "effect", "text"],
|
||||
"ssa": ["marked", "start", "end", "style", "name", "marginl", "marginr", "marginv", "effect", "text"]
|
||||
}
|
||||
|
||||
#: Largest timestamp allowed in SubStation, ie. 9:59:59.99.
|
||||
MAX_REPRESENTABLE_TIME = make_time(h=10) - 10
|
||||
|
||||
def ms_to_timestamp(ms):
|
||||
"""Convert ms to 'H:MM:SS.cc'"""
|
||||
# XXX throw on overflow/underflow?
|
||||
if ms < 0: ms = 0
|
||||
if ms > MAX_REPRESENTABLE_TIME: ms = MAX_REPRESENTABLE_TIME
|
||||
h, m, s, ms = ms_to_times(ms)
|
||||
return "%01d:%02d:%02d.%02d" % (h, m, s, ms//10)
|
||||
|
||||
def color_to_ass_rgba(c):
|
||||
return "&H%08X" % ((c.a << 24) | (c.b << 16) | (c.g << 8) | c.r)
|
||||
|
||||
def color_to_ssa_rgb(c):
|
||||
return "%d" % ((c.b << 16) | (c.g << 8) | c.r)
|
||||
|
||||
def ass_rgba_to_color(s):
|
||||
x = int(s[2:], base=16)
|
||||
r = x & 0xff
|
||||
g = (x >> 8) & 0xff
|
||||
b = (x >> 16) & 0xff
|
||||
a = (x >> 24) & 0xff
|
||||
return Color(r, g, b, a)
|
||||
|
||||
def ssa_rgb_to_color(s):
|
||||
x = int(s)
|
||||
r = x & 0xff
|
||||
g = (x >> 8) & 0xff
|
||||
b = (x >> 16) & 0xff
|
||||
return Color(r, g, b)
|
||||
|
||||
def is_valid_field_content(s):
|
||||
"""
|
||||
Returns True if string s can be stored in a SubStation field.
|
||||
|
||||
Fields are written in CSV-like manner, thus commas and/or newlines
|
||||
are not acceptable in the string.
|
||||
|
||||
"""
|
||||
return "\n" not in s and "," not in s
|
||||
|
||||
|
||||
def parse_tags(text, style=SSAStyle.DEFAULT_STYLE, styles={}):
|
||||
"""
|
||||
Split text into fragments with computed SSAStyles.
|
||||
|
||||
Returns list of tuples (fragment, style), where fragment is a part of text
|
||||
between two brace-delimited override sequences, and style is the computed
|
||||
styling of the fragment, ie. the original style modified by all override
|
||||
sequences before the fragment.
|
||||
|
||||
Newline and non-breakable space overrides are left as-is.
|
||||
|
||||
Supported override tags:
|
||||
|
||||
- i, b, u, s
|
||||
- r (with or without style name)
|
||||
|
||||
"""
|
||||
|
||||
fragments = SSAEvent.OVERRIDE_SEQUENCE.split(text)
|
||||
if len(fragments) == 1:
|
||||
return [(text, style)]
|
||||
|
||||
def apply_overrides(all_overrides):
|
||||
s = style.copy()
|
||||
for tag in re.findall(r"\\[ibus][10]|\\r[a-zA-Z_0-9 ]*", all_overrides):
|
||||
if tag == r"\r":
|
||||
s = style.copy() # reset to original line style
|
||||
elif tag.startswith(r"\r"):
|
||||
name = tag[2:]
|
||||
if name in styles:
|
||||
s = styles[name].copy() # reset to named style
|
||||
else:
|
||||
if "i" in tag: s.italic = "1" in tag
|
||||
elif "b" in tag: s.bold = "1" in tag
|
||||
elif "u" in tag: s.underline = "1" in tag
|
||||
elif "s" in tag: s.strikeout = "1" in tag
|
||||
return s
|
||||
|
||||
overrides = SSAEvent.OVERRIDE_SEQUENCE.findall(text)
|
||||
overrides_prefix_sum = ["".join(overrides[:i]) for i in range(len(overrides) + 1)]
|
||||
computed_styles = map(apply_overrides, overrides_prefix_sum)
|
||||
return list(zip(fragments, computed_styles))
|
||||
|
||||
|
||||
NOTICE = "Script generated by pysubs2\nhttps://pypi.python.org/pypi/pysubs2"
|
||||
|
||||
class SubstationFormat(FormatBase):
|
||||
@classmethod
|
||||
def guess_format(cls, text):
|
||||
if "V4+ Styles" in text:
|
||||
return "ass"
|
||||
elif "V4 Styles" in text:
|
||||
return "ssa"
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, subs, fp, format_, **kwargs):
|
||||
|
||||
def string_to_field(f, v):
|
||||
if f in {"start", "end"}:
|
||||
return timestamp_to_ms(TIMESTAMP.match(v).groups())
|
||||
elif "color" in f:
|
||||
if format_ == "ass":
|
||||
return ass_rgba_to_color(v)
|
||||
else:
|
||||
return ssa_rgb_to_color(v)
|
||||
elif f in {"bold", "underline", "italic", "strikeout"}:
|
||||
return v == "-1"
|
||||
elif f in {"borderstyle", "encoding", "marginl", "marginr", "marginv", "layer", "alphalevel"}:
|
||||
return int(v)
|
||||
elif f in {"fontsize", "scalex", "scaley", "spacing", "angle", "outline", "shadow"}:
|
||||
return float(v)
|
||||
elif f == "marked":
|
||||
return v.endswith("1")
|
||||
elif f == "alignment":
|
||||
i = int(v)
|
||||
if format_ == "ass":
|
||||
return i
|
||||
else:
|
||||
return ssa_to_ass_alignment(i)
|
||||
else:
|
||||
return v
|
||||
|
||||
subs.info.clear()
|
||||
subs.aegisub_project.clear()
|
||||
subs.styles.clear()
|
||||
|
||||
inside_info_section = False
|
||||
inside_aegisub_section = False
|
||||
|
||||
for line in fp:
|
||||
line = line.strip()
|
||||
|
||||
if SECTION_HEADING.match(line):
|
||||
inside_info_section = "Info" in line
|
||||
inside_aegisub_section = "Aegisub" in line
|
||||
elif inside_info_section or inside_aegisub_section:
|
||||
if line.startswith(";"): continue # skip comments
|
||||
try:
|
||||
k, v = line.split(": ", 1)
|
||||
if inside_info_section:
|
||||
subs.info[k] = v
|
||||
elif inside_aegisub_section:
|
||||
subs.aegisub_project[k] = v
|
||||
except ValueError:
|
||||
pass
|
||||
elif line.startswith("Style:"):
|
||||
_, rest = line.split(": ", 1)
|
||||
buf = rest.strip().split(",")
|
||||
name, raw_fields = buf[0], buf[1:] # splat workaround for Python 2.7
|
||||
field_dict = {f: string_to_field(f, v) for f, v in zip(STYLE_FIELDS[format_], raw_fields)}
|
||||
sty = SSAStyle(**field_dict)
|
||||
subs.styles[name] = sty
|
||||
elif line.startswith("Dialogue:") or line.startswith("Comment:"):
|
||||
ev_type, rest = line.split(": ", 1)
|
||||
raw_fields = rest.strip().split(",", len(EVENT_FIELDS[format_])-1)
|
||||
field_dict = {f: string_to_field(f, v) for f, v in zip(EVENT_FIELDS[format_], raw_fields)}
|
||||
field_dict["type"] = ev_type
|
||||
ev = SSAEvent(**field_dict)
|
||||
subs.events.append(ev)
|
||||
|
||||
|
||||
@classmethod
|
||||
def to_file(cls, subs, fp, format_, header_notice=NOTICE, **kwargs):
|
||||
print("[Script Info]", file=fp)
|
||||
for line in header_notice.splitlines(False):
|
||||
print(";", line, file=fp)
|
||||
|
||||
subs.info["ScriptType"] = "v4.00+" if format_ == "ass" else "v4.00"
|
||||
for k, v in subs.info.items():
|
||||
print(k, v, sep=": ", file=fp)
|
||||
|
||||
if subs.aegisub_project:
|
||||
print("\n[Aegisub Project Garbage]", file=fp)
|
||||
for k, v in subs.aegisub_project.items():
|
||||
print(k, v, sep=": ", file=fp)
|
||||
|
||||
def field_to_string(f, v):
|
||||
if f in {"start", "end"}:
|
||||
return ms_to_timestamp(v)
|
||||
elif f == "marked":
|
||||
return "Marked=%d" % v
|
||||
elif f == "alignment" and format_ == "ssa":
|
||||
return text_type(ass_to_ssa_alignment(v))
|
||||
elif isinstance(v, bool):
|
||||
return "-1" if v else "0"
|
||||
elif isinstance(v, (text_type, Number)):
|
||||
return text_type(v)
|
||||
elif isinstance(v, Color):
|
||||
if format_ == "ass":
|
||||
return color_to_ass_rgba(v)
|
||||
else:
|
||||
return color_to_ssa_rgb(v)
|
||||
else:
|
||||
raise TypeError("Unexpected type when writing a SubStation field")
|
||||
|
||||
print("\n[V4+ Styles]" if format_ == "ass" else "\n[V4 Styles]", file=fp)
|
||||
print(STYLE_FORMAT_LINE[format_], file=fp)
|
||||
for name, sty in subs.styles.items():
|
||||
fields = [field_to_string(f, getattr(sty, f)) for f in STYLE_FIELDS[format_]]
|
||||
print("Style: %s" % name, *fields, sep=",", file=fp)
|
||||
|
||||
print("\n[Events]", file=fp)
|
||||
print(EVENT_FORMAT_LINE[format_], file=fp)
|
||||
for ev in subs.events:
|
||||
fields = [field_to_string(f, getattr(ev, f)) for f in EVENT_FIELDS[format_]]
|
||||
print(ev.type, end=": ", file=fp)
|
||||
print(*fields, sep=",", file=fp)
|
||||
@@ -0,0 +1,147 @@
|
||||
from __future__ import division
|
||||
|
||||
from collections import namedtuple
|
||||
import re
|
||||
|
||||
|
||||
#: Pattern that matches both SubStation and SubRip timestamps.
|
||||
TIMESTAMP = re.compile(r"(\d{1,2}):(\d{2}):(\d{2})[.,](\d{2,3})")
|
||||
|
||||
Times = namedtuple("Times", ["h", "m", "s", "ms"])
|
||||
|
||||
def make_time(h=0, m=0, s=0, ms=0, frames=None, fps=None):
|
||||
"""
|
||||
Convert time to milliseconds.
|
||||
|
||||
See :func:`pysubs2.time.times_to_ms()`. When both frames and fps are specified,
|
||||
:func:`pysubs2.time.frames_to_ms()` is called instead.
|
||||
|
||||
Raises:
|
||||
ValueError: Invalid fps, or one of frames/fps is missing.
|
||||
|
||||
Example:
|
||||
>>> make_time(s=1.5)
|
||||
1500
|
||||
>>> make_time(frames=50, fps=25)
|
||||
2000
|
||||
|
||||
"""
|
||||
if frames is None and fps is None:
|
||||
return times_to_ms(h, m, s, ms)
|
||||
elif frames is not None and fps is not None:
|
||||
return frames_to_ms(frames, fps)
|
||||
else:
|
||||
raise ValueError("Both fps and frames must be specified")
|
||||
|
||||
def timestamp_to_ms(groups):
|
||||
"""
|
||||
Convert groups from :data:`pysubs2.time.TIMESTAMP` match to milliseconds.
|
||||
|
||||
Example:
|
||||
>>> timestamp_to_ms(TIMESTAMP.match("0:00:00.42").groups())
|
||||
420
|
||||
|
||||
"""
|
||||
h, m, s, frac = map(int, groups)
|
||||
ms = frac * 10**(3 - len(groups[-1]))
|
||||
ms += s * 1000
|
||||
ms += m * 60000
|
||||
ms += h * 3600000
|
||||
return ms
|
||||
|
||||
def times_to_ms(h=0, m=0, s=0, ms=0):
|
||||
"""
|
||||
Convert hours, minutes, seconds to milliseconds.
|
||||
|
||||
Arguments may be positive or negative, int or float,
|
||||
need not be normalized (``s=120`` is okay).
|
||||
|
||||
Returns:
|
||||
Number of milliseconds (rounded to int).
|
||||
|
||||
"""
|
||||
ms += s * 1000
|
||||
ms += m * 60000
|
||||
ms += h * 3600000
|
||||
return int(round(ms))
|
||||
|
||||
def frames_to_ms(frames, fps):
|
||||
"""
|
||||
Convert frame-based duration to milliseconds.
|
||||
|
||||
Arguments:
|
||||
frames: Number of frames (should be int).
|
||||
fps: Framerate (must be a positive number, eg. 23.976).
|
||||
|
||||
Returns:
|
||||
Number of milliseconds (rounded to int).
|
||||
|
||||
Raises:
|
||||
ValueError: fps was negative or zero.
|
||||
|
||||
"""
|
||||
if fps <= 0:
|
||||
raise ValueError("Framerate must be positive number (%f)." % fps)
|
||||
|
||||
return int(round(frames * (1000 / fps)))
|
||||
|
||||
def ms_to_frames(ms, fps):
|
||||
"""
|
||||
Convert milliseconds to number of frames.
|
||||
|
||||
Arguments:
|
||||
ms: Number of milliseconds (may be int, float or other numeric class).
|
||||
fps: Framerate (must be a positive number, eg. 23.976).
|
||||
|
||||
Returns:
|
||||
Number of frames (int).
|
||||
|
||||
Raises:
|
||||
ValueError: fps was negative or zero.
|
||||
|
||||
"""
|
||||
if fps <= 0:
|
||||
raise ValueError("Framerate must be positive number (%f)." % fps)
|
||||
|
||||
return int(round((ms / 1000) * fps))
|
||||
|
||||
def ms_to_times(ms):
|
||||
"""
|
||||
Convert milliseconds to normalized tuple (h, m, s, ms).
|
||||
|
||||
Arguments:
|
||||
ms: Number of milliseconds (may be int, float or other numeric class).
|
||||
Should be non-negative.
|
||||
|
||||
Returns:
|
||||
Named tuple (h, m, s, ms) of ints.
|
||||
Invariants: ``ms in range(1000) and s in range(60) and m in range(60)``
|
||||
|
||||
"""
|
||||
ms = int(round(ms))
|
||||
h, ms = divmod(ms, 3600000)
|
||||
m, ms = divmod(ms, 60000)
|
||||
s, ms = divmod(ms, 1000)
|
||||
return Times(h, m, s, ms)
|
||||
|
||||
def ms_to_str(ms, fractions=False):
|
||||
"""
|
||||
Prettyprint milliseconds to [-]H:MM:SS[.mmm]
|
||||
|
||||
Handles huge and/or negative times. Non-negative times with ``fractions=True``
|
||||
are matched by :data:`pysubs2.time.TIMESTAMP`.
|
||||
|
||||
Arguments:
|
||||
ms: Number of milliseconds (int, float or other numeric class).
|
||||
fractions: Whether to print up to millisecond precision.
|
||||
|
||||
Returns:
|
||||
str
|
||||
|
||||
"""
|
||||
sgn = "-" if ms < 0 else ""
|
||||
h, m, s, ms = ms_to_times(abs(ms))
|
||||
if fractions:
|
||||
return sgn + "{:01d}:{:02d}:{:02d}.{:03d}".format(h, m, s, ms)
|
||||
else:
|
||||
return sgn + "{:01d}:{:02d}:{:02d}".format(h, m, s)
|
||||
@@ -2,10 +2,19 @@
|
||||
|
||||
import subliminal
|
||||
import babelfish
|
||||
import logging
|
||||
|
||||
# patch subliminal's subtitle encoding detection
|
||||
from .patch_subtitle import PatchedSubtitle
|
||||
subliminal.subtitle.Subtitle = PatchedSubtitle
|
||||
from subliminal.providers.addic7ed import Addic7edSubtitle
|
||||
from subliminal.providers.podnapisi import PodnapisiSubtitle
|
||||
from subliminal.providers.tvsubtitles import TVsubtitlesSubtitle
|
||||
from subliminal.providers.opensubtitles import OpenSubtitlesSubtitle
|
||||
setattr(Addic7edSubtitle, "__bases__", (PatchedSubtitle,))
|
||||
setattr(PodnapisiSubtitle, "__bases__", (PatchedSubtitle,))
|
||||
setattr(TVsubtitlesSubtitle, "__bases__", (PatchedSubtitle,))
|
||||
setattr(OpenSubtitlesSubtitle, "__bases__", (PatchedSubtitle,))
|
||||
|
||||
from .patch_provider_pool import PatchedProviderPool
|
||||
from .patch_video import patched_search_external_subtitles, scan_video
|
||||
|
||||
@@ -3,7 +3,7 @@ import os
|
||||
import logging
|
||||
from bs4 import UnicodeDammit
|
||||
from subliminal.api import get_subtitle_path, io
|
||||
from subzero.lib.io import getViableEncoding
|
||||
from subzero.lib.io import get_viable_encoding
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -49,6 +49,8 @@ def save_subtitles(video, subtitles, single=False, directory=None, encoding=None
|
||||
# force unicode
|
||||
subtitle_path = UnicodeDammit(subtitle_path).unicode_markup
|
||||
|
||||
subtitle.storage_path = subtitle_path
|
||||
|
||||
# save content as is or in the specified encoding
|
||||
logger.info('Saving %r to %r', subtitle, subtitle_path)
|
||||
has_encoder = callable(encode_with)
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
import logging
|
||||
|
||||
import chardet
|
||||
import pysrt
|
||||
import pysubs2
|
||||
from bs4 import UnicodeDammit
|
||||
from subliminal.video import Episode, Movie
|
||||
from subliminal import Subtitle
|
||||
@@ -65,6 +67,8 @@ def compute_score(matches, video, scores=None):
|
||||
|
||||
|
||||
class PatchedSubtitle(Subtitle):
|
||||
storage_path = None
|
||||
|
||||
def guess_encoding(self):
|
||||
"""Guess encoding using the language, falling back on chardet.
|
||||
|
||||
@@ -88,10 +92,15 @@ class PatchedSubtitle(Subtitle):
|
||||
encodings.append('windows-1255')
|
||||
elif self.language.alpha3 == 'tur':
|
||||
encodings.extend(['iso-8859-9', 'windows-1254'])
|
||||
elif self.language.alpha3 == 'pol':
|
||||
|
||||
# Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian, Serbian (Latin script),
|
||||
# Romanian (before 1993 spelling reform) and Albanian
|
||||
elif self.language.alpha3 in ('pol', 'cze', 'svk', 'hun', 'svn', 'bih', 'hrv', 'srb', 'rou', 'alb'):
|
||||
# Eastern European Group 1
|
||||
encodings.extend(['windows-1250'])
|
||||
elif self.language.alpha3 == 'bul':
|
||||
|
||||
# Bulgarian, Serbian and Macedonian
|
||||
elif self.language.alpha3 in ('bul', 'srb', 'mkd'):
|
||||
# Eastern European Group 2
|
||||
encodings.extend(['windows-1251'])
|
||||
else:
|
||||
@@ -126,4 +135,33 @@ class PatchedSubtitle(Subtitle):
|
||||
return a.original_encoding
|
||||
raise ValueError(u"Couldn't guess the proper encoding for %s" % self)
|
||||
|
||||
return encoding
|
||||
return encoding
|
||||
|
||||
def is_valid(self):
|
||||
"""Check if a :attr:`text` is a valid SubRip format.
|
||||
|
||||
:return: whether or not the subtitle is valid.
|
||||
:rtype: bool
|
||||
|
||||
"""
|
||||
if not self.text:
|
||||
return False
|
||||
|
||||
# valid srt
|
||||
try:
|
||||
pysrt.from_string(self.text, error_handling=pysrt.ERROR_RAISE)
|
||||
except Exception, e:
|
||||
logger.error("PySRT-parsing failed: %s, trying pysubs2", e)
|
||||
else:
|
||||
return True
|
||||
|
||||
# something else, try to return srt
|
||||
try:
|
||||
logger.debug("Trying parsing with PySubs2")
|
||||
subs = pysubs2.SSAFile.from_string(self.text)
|
||||
self.content = subs.to_string("srt")
|
||||
except:
|
||||
logger.exception("Couldn't convert subtitle %s to .srt format", self)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
@@ -12,3 +12,21 @@ PREFIX = "/video/%s" % PLUGIN_IDENTIFIER_SHORT
|
||||
TITLE = "%s Subtitles" % PLUGIN_NAME
|
||||
ART = 'art-default.jpg'
|
||||
ICON = 'icon-default.jpg'
|
||||
|
||||
|
||||
# media types as on https://github.com/Arcanemagus/plex-api/wiki/MediaTypes
|
||||
MOVIE = 1
|
||||
SHOW = 2
|
||||
SEASON = 3
|
||||
EPISODE = 4
|
||||
TRAILER = 5
|
||||
COMIC = 6
|
||||
PERSON = 7
|
||||
ARTIST = 8
|
||||
ALBUM = 9
|
||||
TRACK = 10
|
||||
PHOTO_ALBUM = 11
|
||||
PICTURE = 12
|
||||
PHOTO = 13
|
||||
CLIP = 14
|
||||
PLAYLIST_ITEM = 15
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# coding=utf-8
|
||||
import threading
|
||||
|
||||
lock = threading.Lock()
|
||||
|
||||
|
||||
class Debouncer(object):
|
||||
call_history = set()
|
||||
|
||||
def get_lookup_key(self, args, kwargs):
|
||||
func_name = list(args).pop(0).__name__
|
||||
return tuple([func_name] + [(key, value) for key, value in kwargs.iteritems()])
|
||||
|
||||
def __contains__(self, item):
|
||||
args, kwargs = item
|
||||
lookup = self.get_lookup_key(args, kwargs)
|
||||
with lock:
|
||||
return lookup in self.call_history
|
||||
|
||||
def add(self, args, kwargs):
|
||||
with lock:
|
||||
self.call_history.add(self.get_lookup_key(args, kwargs))
|
||||
|
||||
debouncer = Debouncer()
|
||||
@@ -1,6 +1,9 @@
|
||||
# coding=utf-8
|
||||
|
||||
import datetime
|
||||
import threading
|
||||
|
||||
lock = threading.Lock()
|
||||
|
||||
|
||||
class TempIntent(dict):
|
||||
@@ -9,7 +12,8 @@ class TempIntent(dict):
|
||||
|
||||
def __init__(self, timeout=1000):
|
||||
self.timeout = timeout
|
||||
self.store = {}
|
||||
with lock:
|
||||
self.store = {}
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name in self:
|
||||
@@ -22,48 +26,63 @@ class TempIntent(dict):
|
||||
if name in self:
|
||||
del self[name]
|
||||
|
||||
def get(self, kind, key):
|
||||
if kind in self["store"]:
|
||||
now = datetime.datetime.now()
|
||||
hit = False
|
||||
for known_key in self["store"][kind].keys():
|
||||
# may need locking, for now just play it safe
|
||||
ends = self["store"][kind].get(known_key, None)
|
||||
if not ends:
|
||||
def get(self, kind, *keys):
|
||||
with lock:
|
||||
# iter all requested keys
|
||||
for key in keys:
|
||||
hit = False
|
||||
|
||||
# skip key if invalid
|
||||
if not key:
|
||||
continue
|
||||
|
||||
timed_out = False
|
||||
if now > ends:
|
||||
timed_out = True
|
||||
# valid kind?
|
||||
if kind in self["store"]:
|
||||
now = datetime.datetime.now()
|
||||
|
||||
if known_key == key and not timed_out:
|
||||
hit = True
|
||||
# iter all known kinds (previously created)
|
||||
for known_key in self["store"][kind].keys():
|
||||
# may need locking, for now just play it safe
|
||||
ends = self["store"][kind].get(known_key, None)
|
||||
if not ends:
|
||||
continue
|
||||
|
||||
if timed_out:
|
||||
try:
|
||||
del self["store"][kind][key]
|
||||
except:
|
||||
continue
|
||||
timed_out = False
|
||||
if now > ends:
|
||||
timed_out = True
|
||||
|
||||
if hit:
|
||||
return True
|
||||
# key and kind in storage, and not timed out = hit
|
||||
if known_key == key and not timed_out:
|
||||
hit = True
|
||||
|
||||
if timed_out:
|
||||
try:
|
||||
del self["store"][kind][key]
|
||||
except:
|
||||
continue
|
||||
|
||||
if hit:
|
||||
return True
|
||||
return False
|
||||
|
||||
def resolve(self, kind, key):
|
||||
if kind in self["store"] and key in self["store"][kind]:
|
||||
del self["store"][kind][key]
|
||||
return True
|
||||
return False
|
||||
with lock:
|
||||
if kind in self["store"] and key in self["store"][kind]:
|
||||
del self["store"][kind][key]
|
||||
return True
|
||||
return False
|
||||
|
||||
def set(self, kind, key, timeout=None):
|
||||
if kind not in self["store"]:
|
||||
self["store"][kind] = {}
|
||||
self["store"][kind][key] = datetime.datetime.now() + datetime.timedelta(milliseconds=timeout or self.timeout)
|
||||
with lock:
|
||||
if kind not in self["store"]:
|
||||
self["store"][kind] = {}
|
||||
self["store"][kind][key] = datetime.datetime.now() + datetime.timedelta(milliseconds=timeout or self.timeout)
|
||||
|
||||
def has(self, kind, key):
|
||||
if kind not in self["store"]:
|
||||
return False
|
||||
return key in self["store"][kind]
|
||||
with lock:
|
||||
if kind not in self["store"]:
|
||||
return False
|
||||
return key in self["store"][kind]
|
||||
|
||||
|
||||
intent = TempIntent()
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# coding=utf-8
|
||||
|
||||
|
||||
class PlexPyNativeResponseProxy(object):
|
||||
"""
|
||||
The equally stupid counterpart to Sub-Zero.support.lib.PlexPyNativeRequestProxy.
|
||||
Incompletely mimics a requests response object for the plex.py library to use.
|
||||
"""
|
||||
data = None
|
||||
headers = None
|
||||
response_code = None
|
||||
request = None
|
||||
|
||||
def __init__(self, response, status_code, request):
|
||||
if response:
|
||||
self.data = response.content
|
||||
self.headers = response.headers
|
||||
self.response_code = status_code
|
||||
self.request = request
|
||||
|
||||
def content(self):
|
||||
return self.data
|
||||
|
||||
content = property(content)
|
||||
|
||||
def status_code(self):
|
||||
return self.response_code
|
||||
|
||||
status_code = property(status_code)
|
||||
|
||||
def url(self):
|
||||
return self.request.url
|
||||
|
||||
url = property(url)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.data)
|
||||
|
||||
def __unicode__(self):
|
||||
return unicode(self.data)
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self.data)
|
||||
|
||||
|
||||
@@ -39,7 +39,8 @@ class FileIO(object):
|
||||
VALID_ENCODINGS = ("latin1", "utf-8", "mbcs")
|
||||
|
||||
|
||||
def getViableEncoding():
|
||||
def get_viable_encoding():
|
||||
# fixme: bad
|
||||
encoding = sys.getfilesystemencoding()
|
||||
return "utf-8" if not encoding or encoding.lower() not in VALID_ENCODINGS else encoding
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# coding=utf-8
|
||||
|
||||
import logging, sys
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
import subliminal_patch
|
||||
import subliminal
|
||||
|
||||
subliminal.region.configure('dogpile.cache.memory')
|
||||
from subliminal.video import scan_video
|
||||
|
||||
from subliminal.subtitle import compute_score
|
||||
from babelfish import Language
|
||||
from subliminal.api import download_best_subtitles
|
||||
|
||||
v = scan_video('Series/Midsomer Murders/S4/Midsomer.Murders.S04E02.Destroying_Angel.avi', dont_use_actual_file=True)
|
||||
|
||||
#pool = ProviderPool()
|
||||
#subs = pool.list_subtitles(v, set([Language.fromietf('nl')]))
|
||||
|
||||
#[pool.download_subtitle(sub) for sub in subs];"
|
||||
|
||||
download_best_subtitles([v], set([Language.fromietf('nl')]), providers=["opensubtitles"])
|
||||
@@ -1,192 +1,26 @@
|
||||
Sub-Zero for Plex, 1.3.31.513
|
||||
=================
|
||||
#Sub-Zero for Plex
|
||||
[](https://github.com/pannal/Sub-Zero.bundle/releases)
|
||||
[]()
|
||||
[]()
|
||||
|
||||

|
||||
|
||||
##### Subtitles done right
|
||||
Originally based on @bramwalet's awesome [Subliminal.bundle](https://github.com/bramwalet/Subliminal.bundle)
|
||||
|
||||
Plex forum thread: https://forums.plex.tv/discussion/186575
|
||||
|
||||
If you like this, buy me a beer: [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=G9VKR2B8PMNKG)
|
||||
|
||||
### Automatic Installation
|
||||
* Sub-Zero now is listed in the official Plex Channel Directory. You can install it from there.
|
||||
|
||||
### Manual/Development/Testing Installation
|
||||
* go to ```Library/Application Support/Plex Media Server/Plug-ins/```
|
||||
* ```rm -r Sub-Zero.bundle``` (remove the folder)
|
||||
* get the release you want from *https://github.com/pannal/Sub-Zero.bundle/releases/*
|
||||
* unzip the release
|
||||
* edit `Contents/Info.plist` and set `<key>PlexPluginDevMode</key>`'s value to `<string>1</string>` to avoid automatic updates with the stable release to your manual installation
|
||||
* restart your plex media server!!!
|
||||
|
||||
### Usage
|
||||
Use the following agent order:
|
||||
|
||||
1. Sub-Zero TV/Movie Subtitles
|
||||
2. Local Media Assets
|
||||
3. anything else
|
||||
|
||||
##### Recommended steps
|
||||
Create an account and provide your credentials (in the plugin configuration) for:
|
||||
|
||||
* [Addic7ed](http://www.addic7ed.com/newaccount.php)
|
||||
* [Opensubtitles](http://www.opensubtitles.org/en/newuser)
|
||||
* [Plex](https://plex.tv/users/sign_up)
|
||||
|
||||
### Attention on the initial refresh
|
||||
When you first use this plugin and run a refresh on all of your media, you may be
|
||||
blacklisted out of excessive usage by some or all of the subtitle providers depending on your library's size.
|
||||
This will result in a bunch of errors in the log files as well as missing subtitles.
|
||||
|
||||
Just be patient, after a day most of those providers will allow you to access them again and you can
|
||||
refresh the remaining items. If you use the default settings, this will also skip the items
|
||||
it has already downloaded all the wanted languages for. Also, as subtitles will be missing, the scheduler should pick up
|
||||
the items with missing subtitles automatically.
|
||||
|
||||
### Encountered a bug?
|
||||
* be sure to post your logs:
|
||||
* set your log_level to DEBUG in Sub-Zero's settings
|
||||
* get ```Library/Application Support/Plex Media Server/Logs/PMS Plugin Logs/com.plexapp.agents.subzero.log```; there may be multiple logs (com.plexapp.agents.subzero.log.*) depending on the amount of Videos you're refreshing
|
||||
* **Remember: If you're using the manual installation, before you open a bug-ticket please double-check, that you've deleted the Sub-Zero.bundle folder BEFORE every update** (to avoid .pyc leftovers)
|
||||
## Information
|
||||
I've been receiving great support by [@ukdtom](https://github.com/ukdtom) recently:<br/>
|
||||
He has created **[the Sub-Zero Wiki](https://github.com/pannal/Sub-Zero.bundle/wiki)**. Please have a look in case of any questions.
|
||||
|
||||
## Changelog
|
||||
|
||||
1.3.31.513
|
||||
|
||||
- core: add option to only download one language again (and skip the addition of .lang to the subtitle filename) (default: off); fixes #126
|
||||
- core: add option to always encode saved subtitles to UTF-8 (default: on); fixes #128
|
||||
- core: add fallback encoding detection using bs4.UnicodeDammit; hopefully fixes #101
|
||||
- core: update libraries: chardet, beautifulsoup, six
|
||||
- menu/core: check Plex libraries for permission problems on plugin start and report them in the channel menu (option, default: on); fixes #143
|
||||
- menu: while a manual refresh takes place, add a refresh button to the top of the SZ menu for convenience
|
||||
- menu: move the "add/remove X to ignore list" menu item to the bottom of the list on item detail
|
||||
1.3.33.522
|
||||
|
||||
- core: fix library permission detection on windows; fixes #151
|
||||
- core: "Restrict to one language" now behaves like it should (one found subtitle of any language is treated as sufficient); fixes #149
|
||||
- core: add support for other subtitle formats such as ssa/ass/microdvd, convert to srt; fixes #138
|
||||
- core: hopefully more consistent force-refresh handling (intent); fixes #118
|
||||
|
||||
[older changes](CHANGELOG.md)
|
||||
|
||||
Description
|
||||
------------
|
||||
|
||||
Plex Metadata agent plugin based on Subliminal. This agent will search on the following sites for the best matching subtitles:
|
||||
- OpenSubtitles
|
||||
- ~~TheSubDB~~
|
||||
- Podnapisi.NET
|
||||
- Addic7ed
|
||||
- TVsubtitles.net
|
||||
|
||||
All providers can be disabled or enabled on a per provider setting. Certain preferences change the behaviour of subliminal, for instance the minimum score of subtitles to download, or whether to download hearing impaired subtitles or not. The agent stores the subtitles as metadata, but can be configured (See Configuration) to store it next to the media files.
|
||||
|
||||
|
||||
Configuration
|
||||
-------------
|
||||
Several options are provided in the preferences of this agent.
|
||||
|
||||
* Enable Sub-Zero channel (disabling doesn't affect the subtitle features)?: Show or hide the Sub-Zero channel from your PMS
|
||||
* How many download tries per subtitle (on timeout or error): How often should we retry a failed subtitle download? (default: on)
|
||||
* Addic7ed username/password: Provide your addic7ed username here, otherwise the provider won't work. Please make sure your account is activated, before using the agent.
|
||||
* Plex.tv username/password: Generally recommended to be provided; needed if you use Plex Home to make the API work (the whole channel menu depends on it)
|
||||
* Opensubtitles username/password: Generally recommended to be provided (not necessarily needed, but avoids errors)
|
||||
* Subtitle language (1)/(2)/(3): Your preferred languages to download subtitles for.
|
||||
* Additional Subtitle Languages: Additional languages to download; comma-separated; use [ISO-639-1 codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes))
|
||||
* Restrict to one language (skips adding ".lang." to the subtitle filename; only uses "Subtitle Language (1)"): default: off
|
||||
* Normalize subtitle encoding to UTF-8: default: on
|
||||
* Provider: Enable ...: Enable/disable this provider. Affects both movies and series.
|
||||
* Addic7ed: (TV only) boost over hash score if requirements met: if an Addic7ed subtitle matches the video's series, season, episode, year, boost its score, possibly over OpenSubtitles/TheSubDB direct hash match. Recommended for higher quality subtitle results.
|
||||
* I keep the exact (release-) filename of my media files: If you don't rename your media files automatically or manually and keep the original release's file names, enabling this option may help finding suitable subtitles for your media. Otherwise: disable this.
|
||||
* Scan: Include embedded subtitles: When enabled, subliminal finds embedded subtitles (ignoring forced) that are already present within the media file.
|
||||
* Scan: Include external subtitles: When enabled, subliminal finds subtitles located near the media file on the filesystem.
|
||||
* Minimum score for download: When configured, what is the minimum score for subtitles to download them? Lower scored subtitles are not downloaded.
|
||||
* Download hearing impaired subtitles:
|
||||
* "prefer": score subtitles for hearing impaired higher
|
||||
* "don't prefer": score subtitles for hearing impaired lower
|
||||
* "force HI": skip subtitles if the hearing impaired flag isn't set
|
||||
* "force non-HI": skip subtitles if the hearing impaired flag is set
|
||||
* Store subtitles next to media files (instead of metadata): See Store as metadata or on filesystem
|
||||
* Subtitle folder: (default: current media file's folder) See Store as metadata or on filesystem
|
||||
* Custom Subtitle folder: See Store as metadata or on filesystem
|
||||
* Treat IETF language tags as ISO 639-1: Treats subtitle files with IETF language identifiers, such as pt-BR, as their ISO 639-1 counterpart. Thus "pt-BR" will be shown as "Portuguese" instead of "Unknown"
|
||||
* Ignore folders (...): If a folder contains one of the files named `subzero.ignore`, `.subzero.ignore`, `.nosz`, don't process them. This applies to sections/libraries, movies, series, seasons, episodes
|
||||
* Scheduler:
|
||||
* Periodically search for recent items with missing subtitles: self-explanatory, executes the task "Search for missing subtitles" from the channel menu regularly. Configure how often it should do that. For the average library 6 hours minimum is recommended, to not hammer the providers too heavily
|
||||
* Item age to be considered recent: The "Search for missing subtitles"-task only considers those items in the recently-added list, that are at most this old
|
||||
* Recent items to consider per library: How many items to consider for every section/library you have - used in "Search for missing subtitles"-task and "Items with missing subtitles"-menu. Change at your own risk!
|
||||
* Check for correct folder permissions of every library on plugin start: if enabled, SZ checks for necessary permissions of your library folders and warns about them in the plugin channel
|
||||
* How verbose should the logging be?: Controls how much info we write into the log files (default: only warnings)
|
||||
* Log to console (for development/debugging): You know when you need it
|
||||
|
||||
Scheduler
|
||||
---------------------------------------
|
||||
The built-in scheduler is capable of running a number of tasks periodically in a separate Thread of the plugin.
|
||||
This currently is used to automatically periodically search for new subtitles for your media items.
|
||||
See configuration above.
|
||||
|
||||
##### Ignore list
|
||||
There are numerous occasions where one wouldn't want a certain item or even a library be included in the periodic "Search for missing subtitles"-task or the "Items with missing subtitles" menu function.
|
||||
Anime libraries are a good example of that, or home videos. Perhaps you've got your favourite series in your native language and don't want subtitles for it.
|
||||
|
||||
The ignore list can be managed by going through your library using the "Browse all items" menu and the "Display ignore list" menu.
|
||||
|
||||
|
||||
The channel
|
||||
-----------
|
||||
Since 1.3.0 Sub-Zero not only comes as an agent plugin, but also has channel properties.
|
||||
By accessing the Sub-Zero channel you can get viable information about the scheduler state, search for missing subtitles,
|
||||
trigger forced-searches for individual items, and many more features yet to come.
|
||||
|
||||
Remoting the channel
|
||||
--------------------
|
||||
The features available in the channel menu are in fact accessible and usable from the outside,
|
||||
just as any other channel with routes.
|
||||
This means, that if you're not happy with the scheduler's interval for example, you can take the following URL:
|
||||
`http://plex_ip:32400/video/subzero/missing/refresh?X-Plex-Token=XXXXXXXXXXXXXXX` (the X-Plex-Token part may not be needed outside of
|
||||
a Plex Home) and open the URL using your favourite command line tool or script (curl, wget, ...).
|
||||
This will trigger the same background task which would be started by the scheduler or by clicking the item in the channel menu.
|
||||
|
||||
You can find all available routes by querying `http://plex_ip:32400/video/subzero` (look for the key="" entries).
|
||||
|
||||
|
||||
Store as metadata or on filesystem
|
||||
----------------------------------
|
||||
By default, Plex stores posters, fan art and subtitles as metadata in a separate folder which is not managed by the user.
|
||||
In Sub-Zero, though, 'Store subtitles next to media files' is enabled by default.
|
||||
The agent will write the subtitle files in the media folder next to the media file itself.
|
||||
The setting 'Subtitle folder' configures in which folder (current folder or other subfolder) the subtitles are stored. The expert user can also supply 'Custom Subtitle folder' which can also be an absolute path.
|
||||
|
||||
**When a subfolder (either custom or predefined) is used, the automatic scheduled refresh of Plex won't pick up your subtitles, only a manual refresh will!**
|
||||
|
||||
|
||||
BETA: Physically Ignoring Media
|
||||
-------------------------
|
||||
Sometimes subtitles aren't needed or wanted for parts of your library.
|
||||
|
||||
When creating a file named `subzero.ignore`, `.subzero.ignore`, or `.nosz` in any of your library's folders, be it
|
||||
the section itself, a TV show, a movie, or even a season, Sub-Zero will skip processing the contents of that folder.
|
||||
|
||||
BETA notes: This may still mean that the scheduler task for missing subtitles triggers refresh actions on those items,
|
||||
but the refresh handler itself will skip those.
|
||||
|
||||
License
|
||||
-------
|
||||
The Unlicense
|
||||
|
||||
Libraries
|
||||
---------
|
||||
Uses the following libraries and their LICENSE:
|
||||
- [babelfish](https://pypi.python.org/pypi/babelfish/) (BSD-3-Clause)
|
||||
- [beautifulsoup4](https://pypi.python.org/pypi/beautifulsoup4/) (MIT)
|
||||
- [chardet](https://pypi.python.org/pypi/chardet/) (LGPL)
|
||||
- [dogpile.core](https://pypi.python.org/pypi/dogpile.core/) (BSD)
|
||||
- [dogpile.cache](https://pypi.python.org/pypi/dogpile.cache/) (BSD)
|
||||
- [enzyme](https://pypi.python.org/pypi/enzyme/) (Apache 2.0)
|
||||
- [guessit](https://pypi.python.org/pypi/guessit/) (LGPLv3)
|
||||
- [html5lib](https://pypi.python.org/pypi/html5lib/) (MIT)
|
||||
- [pysrt](https://pypi.python.org/pypi/pysrt/) (GPLv3)
|
||||
- [requests](https://pypi.python.org/pypi/requests/) (Apache 2.0)
|
||||
- [stevedore](https://pypi.python.org/pypi/stevedore/) (Apache)
|
||||
- [subliminal](https://pypi.python.org/pypi/subliminal/) (MIT)
|
||||
- [xdg](https://pypi.python.org/pypi/pyxdg/) (LGPLv2)
|
||||
- [setuptools](https://pypi.python.org/pypi/setuptools/) (PSF ZPL)
|
||||
- [plexinc-agents/LocalMedia.bundle](https://github.com/plexinc-agents/LocalMedia.bundle) (Plex)
|
||||
- [fuzeman/plex.py](https://github.com/fuzeman/plex.py) (plex.py)
|
||||
If you like this, buy me a beer: [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=G9VKR2B8PMNKG)
|
||||
|
||||
|
After Width: | Height: | Size: 39 KiB |
|
After Width: | Height: | Size: 8.9 KiB |
|
After Width: | Height: | Size: 9.3 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 86 KiB |
|
After Width: | Height: | Size: 181 KiB |
|
After Width: | Height: | Size: 90 KiB |
|
After Width: | Height: | Size: 51 KiB |
|
After Width: | Height: | Size: 39 KiB |
|
After Width: | Height: | Size: 31 KiB |
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 20 KiB |
|
After Width: | Height: | Size: 46 KiB |
|
After Width: | Height: | Size: 213 KiB |