116 lines
3.6 KiB
Python
116 lines
3.6 KiB
Python
# coding=utf-8
|
|
|
|
import datetime
|
|
import pprint
|
|
import copy
|
|
|
|
from helpers import get_video_display_title
|
|
|
|
|
|
def get_subtitle_info(rating_key):
|
|
return Dict["subs"].get(rating_key)
|
|
|
|
|
|
def whack_missing_parts(scanned_video_part_map, 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 scanned_video_part_map: videos to check for
|
|
:return:
|
|
"""
|
|
# shortcut
|
|
|
|
if "subs" not in Dict:
|
|
return
|
|
|
|
if not existing_parts:
|
|
existing_parts = []
|
|
for part in scanned_video_part_map.viewvalues():
|
|
existing_parts.append(part.id)
|
|
|
|
whacked_parts = False
|
|
for video in scanned_video_part_map.keys():
|
|
if video.id not in Dict["subs"]:
|
|
continue
|
|
|
|
parts = Dict["subs"][video.id].keys()
|
|
|
|
for part_id in parts:
|
|
if part_id not in existing_parts:
|
|
Log.Info("Whacking part %s in internal storage of video %s (%s, %s)", part_id, video.id,
|
|
repr(existing_parts), repr(parts))
|
|
del Dict["subs"][video.id][part_id]
|
|
whacked_parts = True
|
|
|
|
if whacked_parts:
|
|
Dict.Save()
|
|
|
|
|
|
def store_subtitle_info(scanned_video_part_map, downloaded_subtitles, storage_type):
|
|
"""
|
|
stores information about downloaded subtitles in plex's Dict()
|
|
"""
|
|
if "subs" not in Dict:
|
|
Dict["subs"] = {}
|
|
|
|
existing_parts = []
|
|
for video, video_subtitles in downloaded_subtitles.items():
|
|
part = scanned_video_part_map[video]
|
|
|
|
if video.id not in Dict["subs"]:
|
|
Dict["subs"][video.id] = {}
|
|
|
|
video_dict = copy.deepcopy(Dict["subs"][video.id])
|
|
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)
|
|
if lang not in part_dict:
|
|
part_dict[lang] = {}
|
|
lang_dict = part_dict[lang]
|
|
sub_key = subtitle.provider_name, subtitle.id
|
|
metadata = video.plexapi_metadata
|
|
|
|
# compute title
|
|
title = get_video_display_title(
|
|
"show" if metadata["series_id"] else "movie",
|
|
metadata["title"],
|
|
parent_title=metadata.get("series", None),
|
|
season=metadata.get("season", None),
|
|
episode=metadata.get("episode", None),
|
|
section_title=metadata.get("section", None),
|
|
add_section_title=True
|
|
)
|
|
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(), title=title)
|
|
lang_dict["current"] = sub_key
|
|
|
|
Dict["subs"][video.id] = video_dict
|
|
|
|
#Dict.Save()
|
|
|
|
#if existing_parts:
|
|
# whack_missing_parts(scanned_video_part_map, existing_parts=existing_parts)
|
|
Dict.Save()
|
|
|
|
|
|
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
|
|
#Plex[":/plugins/*/prefs"].set("com.plexapp.agents.subzero", "reset_storage", False)
|
|
"""
|
|
|
|
Log.Debug("resetting storage")
|
|
Dict[key] = {}
|
|
Dict.Save()
|
|
|
|
|
|
def log_storage(key):
|
|
if key in Dict:
|
|
Log.Debug(pprint.pformat(Dict[key]))
|