2022-05-01 12:00:20 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-07-29 17:03:45 +00:00
|
|
|
import os
|
2021-05-26 20:47:14 +00:00
|
|
|
import atexit
|
|
|
|
import json
|
2020-07-12 20:37:20 +00:00
|
|
|
import ast
|
2021-10-21 00:46:22 +00:00
|
|
|
import time
|
2022-05-01 12:00:20 +00:00
|
|
|
|
2022-01-03 03:59:30 +00:00
|
|
|
from peewee import Model, AutoField, TextField, IntegerField, ForeignKeyField, BlobField, BooleanField
|
2021-05-26 20:47:14 +00:00
|
|
|
from playhouse.sqliteq import SqliteQueueDatabase
|
2022-01-03 03:59:30 +00:00
|
|
|
from playhouse.migrate import SqliteMigrator, migrate
|
2021-07-20 12:05:30 +00:00
|
|
|
from playhouse.sqlite_ext import RowIDField
|
2019-07-29 17:03:45 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
from utilities.path_mappings import path_mappings
|
|
|
|
|
|
|
|
from .config import settings, get_array_from
|
|
|
|
from .get_args import args
|
2019-07-29 17:03:45 +00:00
|
|
|
|
2021-05-26 20:47:14 +00:00
|
|
|
database = SqliteQueueDatabase(os.path.join(args.config_dir, 'db', 'bazarr.db'),
|
2021-10-21 00:46:22 +00:00
|
|
|
use_gevent=False,
|
2021-05-26 20:47:14 +00:00
|
|
|
autostart=True,
|
|
|
|
queue_max_size=256)
|
2021-06-19 04:03:40 +00:00
|
|
|
migrator = SqliteMigrator(database)
|
2021-05-26 20:47:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@atexit.register
|
|
|
|
def _stop_worker_threads():
|
|
|
|
database.stop()
|
|
|
|
|
|
|
|
|
|
|
|
class UnknownField(object):
|
|
|
|
def __init__(self, *_, **__): pass
|
|
|
|
|
|
|
|
|
|
|
|
class BaseModel(Model):
|
|
|
|
class Meta:
|
|
|
|
database = database
|
|
|
|
|
|
|
|
|
|
|
|
class System(BaseModel):
|
|
|
|
configured = TextField(null=True)
|
|
|
|
updated = TextField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'system'
|
|
|
|
primary_key = False
|
|
|
|
|
|
|
|
|
|
|
|
class TableBlacklist(BaseModel):
|
|
|
|
language = TextField(null=True)
|
|
|
|
provider = TextField(null=True)
|
|
|
|
sonarr_episode_id = IntegerField(null=True)
|
|
|
|
sonarr_series_id = IntegerField(null=True)
|
|
|
|
subs_id = TextField(null=True)
|
|
|
|
timestamp = IntegerField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_blacklist'
|
|
|
|
primary_key = False
|
|
|
|
|
|
|
|
|
|
|
|
class TableBlacklistMovie(BaseModel):
|
|
|
|
language = TextField(null=True)
|
|
|
|
provider = TextField(null=True)
|
|
|
|
radarr_id = IntegerField(null=True)
|
|
|
|
subs_id = TextField(null=True)
|
|
|
|
timestamp = IntegerField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_blacklist_movie'
|
|
|
|
primary_key = False
|
|
|
|
|
|
|
|
|
|
|
|
class TableEpisodes(BaseModel):
|
2021-07-20 12:05:30 +00:00
|
|
|
rowid = RowIDField()
|
2021-05-26 20:47:14 +00:00
|
|
|
audio_codec = TextField(null=True)
|
|
|
|
audio_language = TextField(null=True)
|
|
|
|
episode = IntegerField()
|
|
|
|
episode_file_id = IntegerField(null=True)
|
|
|
|
failedAttempts = TextField(null=True)
|
|
|
|
ffprobe_cache = BlobField(null=True)
|
|
|
|
file_size = IntegerField(default=0, null=True)
|
|
|
|
format = TextField(null=True)
|
|
|
|
missing_subtitles = TextField(null=True)
|
|
|
|
monitored = TextField(null=True)
|
|
|
|
path = TextField()
|
|
|
|
resolution = TextField(null=True)
|
|
|
|
scene_name = TextField(null=True)
|
|
|
|
season = IntegerField()
|
|
|
|
sonarrEpisodeId = IntegerField(unique=True)
|
|
|
|
sonarrSeriesId = IntegerField()
|
|
|
|
subtitles = TextField(null=True)
|
|
|
|
title = TextField()
|
|
|
|
video_codec = TextField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_episodes'
|
|
|
|
primary_key = False
|
|
|
|
|
|
|
|
|
|
|
|
class TableHistory(BaseModel):
|
|
|
|
action = IntegerField()
|
|
|
|
description = TextField()
|
|
|
|
id = AutoField()
|
|
|
|
language = TextField(null=True)
|
|
|
|
provider = TextField(null=True)
|
|
|
|
score = TextField(null=True)
|
|
|
|
sonarrEpisodeId = IntegerField()
|
|
|
|
sonarrSeriesId = IntegerField()
|
|
|
|
subs_id = TextField(null=True)
|
|
|
|
subtitles_path = TextField(null=True)
|
|
|
|
timestamp = IntegerField()
|
|
|
|
video_path = TextField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_history'
|
|
|
|
|
|
|
|
|
|
|
|
class TableHistoryMovie(BaseModel):
|
|
|
|
action = IntegerField()
|
|
|
|
description = TextField()
|
|
|
|
id = AutoField()
|
|
|
|
language = TextField(null=True)
|
|
|
|
provider = TextField(null=True)
|
|
|
|
radarrId = IntegerField()
|
|
|
|
score = TextField(null=True)
|
|
|
|
subs_id = TextField(null=True)
|
|
|
|
subtitles_path = TextField(null=True)
|
|
|
|
timestamp = IntegerField()
|
|
|
|
video_path = TextField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_history_movie'
|
|
|
|
|
|
|
|
|
|
|
|
class TableLanguagesProfiles(BaseModel):
|
|
|
|
cutoff = IntegerField(null=True)
|
2022-03-15 00:54:36 +00:00
|
|
|
originalFormat = BooleanField(null=True)
|
2021-05-26 20:47:14 +00:00
|
|
|
items = TextField()
|
|
|
|
name = TextField()
|
|
|
|
profileId = AutoField()
|
2021-12-11 12:44:53 +00:00
|
|
|
mustContain = TextField(null=True)
|
|
|
|
mustNotContain = TextField(null=True)
|
2021-05-26 20:47:14 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_languages_profiles'
|
|
|
|
|
|
|
|
|
|
|
|
class TableMovies(BaseModel):
|
2021-07-20 12:05:30 +00:00
|
|
|
rowid = RowIDField()
|
2021-05-26 20:47:14 +00:00
|
|
|
alternativeTitles = TextField(null=True)
|
|
|
|
audio_codec = TextField(null=True)
|
|
|
|
audio_language = TextField(null=True)
|
|
|
|
failedAttempts = TextField(null=True)
|
|
|
|
fanart = TextField(null=True)
|
|
|
|
ffprobe_cache = BlobField(null=True)
|
|
|
|
file_size = IntegerField(default=0, null=True)
|
|
|
|
format = TextField(null=True)
|
|
|
|
imdbId = TextField(null=True)
|
|
|
|
missing_subtitles = TextField(null=True)
|
|
|
|
monitored = TextField(null=True)
|
|
|
|
movie_file_id = IntegerField(null=True)
|
|
|
|
overview = TextField(null=True)
|
|
|
|
path = TextField(unique=True)
|
|
|
|
poster = TextField(null=True)
|
|
|
|
profileId = IntegerField(null=True)
|
|
|
|
radarrId = IntegerField(unique=True)
|
|
|
|
resolution = TextField(null=True)
|
|
|
|
sceneName = TextField(null=True)
|
|
|
|
sortTitle = TextField(null=True)
|
|
|
|
subtitles = TextField(null=True)
|
|
|
|
tags = TextField(null=True)
|
|
|
|
title = TextField()
|
2021-07-20 12:05:30 +00:00
|
|
|
tmdbId = TextField(unique=True)
|
2021-05-26 20:47:14 +00:00
|
|
|
video_codec = TextField(null=True)
|
|
|
|
year = TextField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_movies'
|
|
|
|
|
|
|
|
|
|
|
|
class TableMoviesRootfolder(BaseModel):
|
|
|
|
accessible = IntegerField(null=True)
|
|
|
|
error = TextField(null=True)
|
|
|
|
id = IntegerField(null=True)
|
|
|
|
path = TextField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_movies_rootfolder'
|
|
|
|
primary_key = False
|
|
|
|
|
|
|
|
|
|
|
|
class TableSettingsLanguages(BaseModel):
|
|
|
|
code2 = TextField(null=True)
|
|
|
|
code3 = TextField(primary_key=True)
|
|
|
|
code3b = TextField(null=True)
|
|
|
|
enabled = IntegerField(null=True)
|
|
|
|
name = TextField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_settings_languages'
|
|
|
|
|
|
|
|
|
|
|
|
class TableSettingsNotifier(BaseModel):
|
|
|
|
enabled = IntegerField(null=True)
|
|
|
|
name = TextField(null=True, primary_key=True)
|
|
|
|
url = TextField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_settings_notifier'
|
|
|
|
|
|
|
|
|
|
|
|
class TableShows(BaseModel):
|
|
|
|
alternateTitles = TextField(null=True)
|
|
|
|
audio_language = TextField(null=True)
|
|
|
|
fanart = TextField(null=True)
|
|
|
|
imdbId = TextField(default='""', null=True)
|
2022-10-06 02:51:54 +00:00
|
|
|
monitored = TextField(null=True)
|
2021-05-26 20:47:14 +00:00
|
|
|
overview = TextField(null=True)
|
|
|
|
path = TextField(unique=True)
|
|
|
|
poster = TextField(null=True)
|
|
|
|
profileId = IntegerField(null=True)
|
|
|
|
seriesType = TextField(null=True)
|
|
|
|
sonarrSeriesId = IntegerField(unique=True)
|
|
|
|
sortTitle = TextField(null=True)
|
|
|
|
tags = TextField(null=True)
|
|
|
|
title = TextField()
|
|
|
|
tvdbId = AutoField()
|
|
|
|
year = TextField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_shows'
|
|
|
|
|
|
|
|
|
|
|
|
class TableShowsRootfolder(BaseModel):
|
|
|
|
accessible = IntegerField(null=True)
|
|
|
|
error = TextField(null=True)
|
|
|
|
id = IntegerField(null=True)
|
|
|
|
path = TextField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_shows_rootfolder'
|
|
|
|
primary_key = False
|
|
|
|
|
|
|
|
|
2021-06-10 20:00:42 +00:00
|
|
|
class TableCustomScoreProfiles(BaseModel):
|
|
|
|
id = AutoField()
|
|
|
|
name = TextField(null=True)
|
|
|
|
media = TextField(null=True)
|
|
|
|
score = IntegerField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_custom_score_profiles'
|
|
|
|
|
|
|
|
|
|
|
|
class TableCustomScoreProfileConditions(BaseModel):
|
|
|
|
profile_id = ForeignKeyField(TableCustomScoreProfiles, to_field="id")
|
2022-01-03 03:59:30 +00:00
|
|
|
type = TextField(null=True) # provider, uploader, regex, etc
|
|
|
|
value = TextField(null=True) # opensubtitles, jane_doe, [a-z], etc
|
2021-06-10 20:00:42 +00:00
|
|
|
required = BooleanField(default=False)
|
|
|
|
negate = BooleanField(default=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'table_custom_score_profile_conditions'
|
|
|
|
|
|
|
|
|
2021-05-27 16:28:46 +00:00
|
|
|
def init_db():
|
|
|
|
# Create tables if they don't exists.
|
|
|
|
database.create_tables([System,
|
|
|
|
TableBlacklist,
|
|
|
|
TableBlacklistMovie,
|
|
|
|
TableEpisodes,
|
|
|
|
TableHistory,
|
|
|
|
TableHistoryMovie,
|
|
|
|
TableLanguagesProfiles,
|
|
|
|
TableMovies,
|
|
|
|
TableMoviesRootfolder,
|
|
|
|
TableSettingsLanguages,
|
|
|
|
TableSettingsNotifier,
|
|
|
|
TableShows,
|
2021-06-10 20:00:42 +00:00
|
|
|
TableShowsRootfolder,
|
|
|
|
TableCustomScoreProfiles,
|
|
|
|
TableCustomScoreProfileConditions])
|
2021-05-27 16:28:46 +00:00
|
|
|
|
|
|
|
# add the system table single row if it's not existing
|
|
|
|
# we must retry until the tables are created
|
|
|
|
tables_created = False
|
|
|
|
while not tables_created:
|
|
|
|
try:
|
|
|
|
if not System.select().count():
|
|
|
|
System.insert({System.configured: '0', System.updated: '0'}).execute()
|
2022-01-03 03:59:30 +00:00
|
|
|
except Exception:
|
2021-10-21 00:46:22 +00:00
|
|
|
time.sleep(0.1)
|
2021-05-27 16:28:46 +00:00
|
|
|
else:
|
|
|
|
tables_created = True
|
|
|
|
|
2019-10-26 18:52:22 +00:00
|
|
|
|
2021-06-19 04:03:40 +00:00
|
|
|
def migrate_db():
|
|
|
|
migrate(
|
|
|
|
migrator.add_column('table_shows', 'year', TextField(null=True)),
|
|
|
|
migrator.add_column('table_shows', 'alternateTitles', TextField(null=True)),
|
|
|
|
migrator.add_column('table_shows', 'tags', TextField(default='[]', null=True)),
|
|
|
|
migrator.add_column('table_shows', 'seriesType', TextField(default='""', null=True)),
|
|
|
|
migrator.add_column('table_shows', 'imdbId', TextField(default='""', null=True)),
|
|
|
|
migrator.add_column('table_shows', 'profileId', IntegerField(null=True)),
|
2022-10-06 02:51:54 +00:00
|
|
|
migrator.add_column('table_shows', 'monitored', TextField(null=True)),
|
2021-06-19 04:03:40 +00:00
|
|
|
migrator.add_column('table_episodes', 'format', TextField(null=True)),
|
|
|
|
migrator.add_column('table_episodes', 'resolution', TextField(null=True)),
|
|
|
|
migrator.add_column('table_episodes', 'video_codec', TextField(null=True)),
|
|
|
|
migrator.add_column('table_episodes', 'audio_codec', TextField(null=True)),
|
|
|
|
migrator.add_column('table_episodes', 'episode_file_id', IntegerField(null=True)),
|
|
|
|
migrator.add_column('table_episodes', 'audio_language', TextField(null=True)),
|
|
|
|
migrator.add_column('table_episodes', 'file_size', IntegerField(default=0, null=True)),
|
|
|
|
migrator.add_column('table_episodes', 'ffprobe_cache', BlobField(null=True)),
|
|
|
|
migrator.add_column('table_movies', 'sortTitle', TextField(null=True)),
|
|
|
|
migrator.add_column('table_movies', 'year', TextField(null=True)),
|
|
|
|
migrator.add_column('table_movies', 'alternativeTitles', TextField(null=True)),
|
|
|
|
migrator.add_column('table_movies', 'format', TextField(null=True)),
|
|
|
|
migrator.add_column('table_movies', 'resolution', TextField(null=True)),
|
|
|
|
migrator.add_column('table_movies', 'video_codec', TextField(null=True)),
|
|
|
|
migrator.add_column('table_movies', 'audio_codec', TextField(null=True)),
|
|
|
|
migrator.add_column('table_movies', 'imdbId', TextField(null=True)),
|
|
|
|
migrator.add_column('table_movies', 'movie_file_id', IntegerField(null=True)),
|
|
|
|
migrator.add_column('table_movies', 'tags', TextField(default='[]', null=True)),
|
|
|
|
migrator.add_column('table_movies', 'profileId', IntegerField(null=True)),
|
|
|
|
migrator.add_column('table_movies', 'file_size', IntegerField(default=0, null=True)),
|
|
|
|
migrator.add_column('table_movies', 'ffprobe_cache', BlobField(null=True)),
|
|
|
|
migrator.add_column('table_history', 'video_path', TextField(null=True)),
|
|
|
|
migrator.add_column('table_history', 'language', TextField(null=True)),
|
|
|
|
migrator.add_column('table_history', 'provider', TextField(null=True)),
|
|
|
|
migrator.add_column('table_history', 'score', TextField(null=True)),
|
|
|
|
migrator.add_column('table_history', 'subs_id', TextField(null=True)),
|
|
|
|
migrator.add_column('table_history', 'subtitles_path', TextField(null=True)),
|
|
|
|
migrator.add_column('table_history_movie', 'video_path', TextField(null=True)),
|
|
|
|
migrator.add_column('table_history_movie', 'language', TextField(null=True)),
|
|
|
|
migrator.add_column('table_history_movie', 'provider', TextField(null=True)),
|
|
|
|
migrator.add_column('table_history_movie', 'score', TextField(null=True)),
|
|
|
|
migrator.add_column('table_history_movie', 'subs_id', TextField(null=True)),
|
2021-12-11 12:44:53 +00:00
|
|
|
migrator.add_column('table_history_movie', 'subtitles_path', TextField(null=True)),
|
|
|
|
migrator.add_column('table_languages_profiles', 'mustContain', TextField(null=True)),
|
|
|
|
migrator.add_column('table_languages_profiles', 'mustNotContain', TextField(null=True)),
|
2022-03-15 00:54:36 +00:00
|
|
|
migrator.add_column('table_languages_profiles', 'originalFormat', BooleanField(null=True)),
|
2021-06-19 04:03:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-10-26 18:52:22 +00:00
|
|
|
class SqliteDictPathMapper:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
2021-05-26 20:47:14 +00:00
|
|
|
@staticmethod
|
|
|
|
def path_replace(values_dict):
|
2019-10-27 01:16:59 +00:00
|
|
|
if type(values_dict) is list:
|
|
|
|
for item in values_dict:
|
2020-05-19 13:27:13 +00:00
|
|
|
item['path'] = path_mappings.path_replace(item['path'])
|
2019-10-27 01:16:59 +00:00
|
|
|
elif type(values_dict) is dict:
|
2020-05-19 13:27:13 +00:00
|
|
|
values_dict['path'] = path_mappings.path_replace(values_dict['path'])
|
2019-10-27 03:17:14 +00:00
|
|
|
else:
|
2020-05-19 13:27:13 +00:00
|
|
|
return path_mappings.path_replace(values_dict)
|
2019-10-26 18:52:22 +00:00
|
|
|
|
2021-05-26 20:47:14 +00:00
|
|
|
@staticmethod
|
|
|
|
def path_replace_movie(values_dict):
|
2019-10-27 01:16:59 +00:00
|
|
|
if type(values_dict) is list:
|
|
|
|
for item in values_dict:
|
2020-05-19 13:27:13 +00:00
|
|
|
item['path'] = path_mappings.path_replace_movie(item['path'])
|
2019-10-27 01:16:59 +00:00
|
|
|
elif type(values_dict) is dict:
|
2020-05-19 13:27:13 +00:00
|
|
|
values_dict['path'] = path_mappings.path_replace_movie(values_dict['path'])
|
2019-10-27 03:17:14 +00:00
|
|
|
else:
|
2020-05-19 13:27:13 +00:00
|
|
|
return path_mappings.path_replace_movie(values_dict)
|
2019-10-26 18:52:22 +00:00
|
|
|
|
|
|
|
|
2019-10-28 04:05:28 +00:00
|
|
|
dict_mapper = SqliteDictPathMapper()
|
|
|
|
|
|
|
|
|
2021-05-26 20:47:14 +00:00
|
|
|
def get_exclusion_clause(exclusion_type):
|
|
|
|
where_clause = []
|
|
|
|
if exclusion_type == 'series':
|
2020-07-12 20:37:20 +00:00
|
|
|
tagsList = ast.literal_eval(settings.sonarr.excluded_tags)
|
2020-09-01 16:30:31 +00:00
|
|
|
for tag in tagsList:
|
2021-05-27 20:29:05 +00:00
|
|
|
where_clause.append(~(TableShows.tags.contains("\'"+tag+"\'")))
|
2020-07-12 20:37:20 +00:00
|
|
|
else:
|
|
|
|
tagsList = ast.literal_eval(settings.radarr.excluded_tags)
|
2020-09-01 16:30:31 +00:00
|
|
|
for tag in tagsList:
|
2021-05-27 20:29:05 +00:00
|
|
|
where_clause.append(~(TableMovies.tags.contains("\'"+tag+"\'")))
|
2020-07-13 14:57:43 +00:00
|
|
|
|
2021-05-26 20:47:14 +00:00
|
|
|
if exclusion_type == 'series':
|
2020-09-01 16:30:31 +00:00
|
|
|
monitoredOnly = settings.sonarr.getboolean('only_monitored')
|
|
|
|
if monitoredOnly:
|
2021-05-26 20:47:14 +00:00
|
|
|
where_clause.append((TableEpisodes.monitored == 'True'))
|
2022-10-06 02:51:54 +00:00
|
|
|
where_clause.append((TableShows.monitored == 'True'))
|
2020-07-13 14:57:43 +00:00
|
|
|
else:
|
2020-09-01 16:30:31 +00:00
|
|
|
monitoredOnly = settings.radarr.getboolean('only_monitored')
|
|
|
|
if monitoredOnly:
|
2021-05-26 20:47:14 +00:00
|
|
|
where_clause.append((TableMovies.monitored == 'True'))
|
2020-07-13 14:57:43 +00:00
|
|
|
|
2021-05-26 20:47:14 +00:00
|
|
|
if exclusion_type == 'series':
|
2021-03-25 14:22:43 +00:00
|
|
|
typesList = get_array_from(settings.sonarr.excluded_series_types)
|
2021-05-26 20:47:14 +00:00
|
|
|
for item in typesList:
|
|
|
|
where_clause.append((TableShows.seriesType != item))
|
2020-07-13 14:57:43 +00:00
|
|
|
|
2021-12-26 18:52:30 +00:00
|
|
|
exclude_season_zero = settings.sonarr.getboolean('exclude_season_zero')
|
|
|
|
if exclude_season_zero:
|
|
|
|
where_clause.append((TableEpisodes.season != 0))
|
|
|
|
|
2020-09-01 16:30:31 +00:00
|
|
|
return where_clause
|
2021-01-19 04:49:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_profile_id_list():
|
2021-05-26 20:47:14 +00:00
|
|
|
profile_id_list = TableLanguagesProfiles.select(TableLanguagesProfiles.profileId,
|
|
|
|
TableLanguagesProfiles.name,
|
|
|
|
TableLanguagesProfiles.cutoff,
|
2021-12-11 12:44:53 +00:00
|
|
|
TableLanguagesProfiles.items,
|
|
|
|
TableLanguagesProfiles.mustContain,
|
2022-03-15 00:54:36 +00:00
|
|
|
TableLanguagesProfiles.mustNotContain,
|
|
|
|
TableLanguagesProfiles.originalFormat).dicts()
|
2021-05-26 20:47:14 +00:00
|
|
|
profile_id_list = list(profile_id_list)
|
2021-03-25 14:22:43 +00:00
|
|
|
for profile in profile_id_list:
|
2021-05-26 20:47:14 +00:00
|
|
|
profile['items'] = json.loads(profile['items'])
|
2022-04-05 13:50:14 +00:00
|
|
|
profile['mustContain'] = ast.literal_eval(profile['mustContain']) if profile['mustContain'] else []
|
|
|
|
profile['mustNotContain'] = ast.literal_eval(profile['mustNotContain']) if profile['mustNotContain'] else []
|
2021-03-25 14:22:43 +00:00
|
|
|
|
2022-04-30 12:48:19 +00:00
|
|
|
return profile_id_list
|
|
|
|
|
2021-01-19 04:49:51 +00:00
|
|
|
|
|
|
|
def get_profiles_list(profile_id=None):
|
2022-04-30 12:48:19 +00:00
|
|
|
profile_id_list = update_profile_id_list()
|
2021-01-19 04:49:51 +00:00
|
|
|
|
2021-05-02 12:02:16 +00:00
|
|
|
if profile_id and profile_id != 'null':
|
2021-01-19 04:49:51 +00:00
|
|
|
for profile in profile_id_list:
|
|
|
|
if profile['profileId'] == profile_id:
|
|
|
|
return profile
|
|
|
|
else:
|
|
|
|
return profile_id_list
|
|
|
|
|
|
|
|
|
|
|
|
def get_desired_languages(profile_id):
|
|
|
|
languages = []
|
2022-04-30 12:48:19 +00:00
|
|
|
profile_id_list = update_profile_id_list()
|
2021-01-19 04:49:51 +00:00
|
|
|
|
2021-05-02 12:02:16 +00:00
|
|
|
if profile_id and profile_id != 'null':
|
2021-01-19 04:49:51 +00:00
|
|
|
for profile in profile_id_list:
|
2022-03-15 00:54:36 +00:00
|
|
|
profileId, name, cutoff, items, mustContain, mustNotContain, originalFormat = profile.values()
|
2022-08-05 01:28:44 +00:00
|
|
|
try:
|
|
|
|
profile_id_int = int(profile_id)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if profileId == profile_id_int:
|
|
|
|
languages = [x['language'] for x in items]
|
|
|
|
break
|
2021-01-19 04:49:51 +00:00
|
|
|
|
|
|
|
return languages
|
|
|
|
|
|
|
|
|
|
|
|
def get_profile_id_name(profile_id):
|
|
|
|
name_from_id = None
|
2022-04-30 12:48:19 +00:00
|
|
|
profile_id_list = update_profile_id_list()
|
2021-01-19 04:49:51 +00:00
|
|
|
|
2021-05-02 12:02:16 +00:00
|
|
|
if profile_id and profile_id != 'null':
|
2021-01-19 04:49:51 +00:00
|
|
|
for profile in profile_id_list:
|
2022-03-15 00:54:36 +00:00
|
|
|
profileId, name, cutoff, items, mustContain, mustNotContain, originalFormat = profile.values()
|
2021-01-19 04:49:51 +00:00
|
|
|
if profileId == int(profile_id):
|
|
|
|
name_from_id = name
|
|
|
|
break
|
|
|
|
|
|
|
|
return name_from_id
|
|
|
|
|
|
|
|
|
|
|
|
def get_profile_cutoff(profile_id):
|
|
|
|
cutoff_language = None
|
2022-04-30 12:48:19 +00:00
|
|
|
profile_id_list = update_profile_id_list()
|
2021-01-19 04:49:51 +00:00
|
|
|
|
2021-05-02 12:02:16 +00:00
|
|
|
if profile_id and profile_id != 'null':
|
2021-01-19 04:49:51 +00:00
|
|
|
cutoff_language = []
|
|
|
|
for profile in profile_id_list:
|
2022-03-15 00:54:36 +00:00
|
|
|
profileId, name, cutoff, items, mustContain, mustNotContain, originalFormat = profile.values()
|
2021-01-19 04:49:51 +00:00
|
|
|
if cutoff:
|
|
|
|
if profileId == int(profile_id):
|
2021-03-25 14:22:43 +00:00
|
|
|
for item in items:
|
2021-01-19 04:49:51 +00:00
|
|
|
if item['id'] == cutoff:
|
|
|
|
return [item]
|
|
|
|
elif cutoff == 65535:
|
|
|
|
cutoff_language.append(item)
|
|
|
|
|
|
|
|
if not len(cutoff_language):
|
|
|
|
cutoff_language = None
|
|
|
|
|
|
|
|
return cutoff_language
|
|
|
|
|
|
|
|
|
|
|
|
def get_audio_profile_languages(series_id=None, episode_id=None, movie_id=None):
|
2022-05-01 12:00:20 +00:00
|
|
|
from languages.get_languages import alpha2_from_language, alpha3_from_language
|
2021-01-19 04:49:51 +00:00
|
|
|
audio_languages = []
|
|
|
|
|
|
|
|
if series_id:
|
2021-05-26 20:47:14 +00:00
|
|
|
audio_languages_list_str = TableShows.get(TableShows.sonarrSeriesId == series_id).audio_language
|
2021-01-19 04:49:51 +00:00
|
|
|
elif episode_id:
|
2021-05-26 20:47:14 +00:00
|
|
|
audio_languages_list_str = TableEpisodes.get(TableEpisodes.sonarrEpisodeId == episode_id).audio_language
|
2021-01-19 04:49:51 +00:00
|
|
|
elif movie_id:
|
2021-05-26 20:47:14 +00:00
|
|
|
audio_languages_list_str = TableMovies.get(TableMovies.radarrId == movie_id).audio_language
|
|
|
|
else:
|
|
|
|
return audio_languages
|
|
|
|
|
|
|
|
try:
|
|
|
|
audio_languages_list = ast.literal_eval(audio_languages_list_str)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
for language in audio_languages_list:
|
|
|
|
audio_languages.append(
|
|
|
|
{"name": language,
|
|
|
|
"code2": alpha2_from_language(language) or None,
|
|
|
|
"code3": alpha3_from_language(language) or None}
|
|
|
|
)
|
2021-01-19 04:49:51 +00:00
|
|
|
|
|
|
|
return audio_languages
|
2021-05-08 14:25:29 +00:00
|
|
|
|
2021-05-26 20:47:14 +00:00
|
|
|
|
2021-12-11 12:44:53 +00:00
|
|
|
def get_profile_id(series_id=None, episode_id=None, movie_id=None):
|
|
|
|
if series_id:
|
2022-02-11 15:59:09 +00:00
|
|
|
data = TableShows.select(TableShows.profileId)\
|
|
|
|
.where(TableShows.sonarrSeriesId == series_id)\
|
|
|
|
.get_or_none()
|
|
|
|
if data:
|
|
|
|
return data.profileId
|
2021-12-11 12:44:53 +00:00
|
|
|
elif episode_id:
|
2022-02-11 15:59:09 +00:00
|
|
|
data = TableShows.select(TableShows.profileId)\
|
2021-12-11 12:44:53 +00:00
|
|
|
.join(TableEpisodes, on=(TableShows.sonarrSeriesId == TableEpisodes.sonarrSeriesId))\
|
|
|
|
.where(TableEpisodes.sonarrEpisodeId == episode_id)\
|
2022-02-11 15:59:09 +00:00
|
|
|
.get_or_none()
|
|
|
|
if data:
|
|
|
|
return data.profileId
|
|
|
|
|
2021-12-11 12:44:53 +00:00
|
|
|
elif movie_id:
|
2022-02-11 15:59:09 +00:00
|
|
|
data = TableMovies.select(TableMovies.profileId)\
|
|
|
|
.where(TableMovies.radarrId == movie_id)\
|
|
|
|
.get_or_none()
|
|
|
|
if data:
|
|
|
|
return data.profileId
|
2021-12-11 12:44:53 +00:00
|
|
|
|
2022-02-11 15:59:09 +00:00
|
|
|
return None
|
2021-12-11 12:44:53 +00:00
|
|
|
|
|
|
|
|
2021-05-08 14:25:29 +00:00
|
|
|
def convert_list_to_clause(arr: list):
|
|
|
|
if isinstance(arr, list):
|
|
|
|
return f"({','.join(str(x) for x in arr)})"
|
|
|
|
else:
|
|
|
|
return ""
|