Skip to content

Commit

Permalink
[major] Overhaul of all API related communication to be up to date wi…
Browse files Browse the repository at this point in the history
…th the latest from qbittorrent, Radarr and Sonarr
  • Loading branch information
Feramance committed Aug 11, 2023
1 parent 95ac7e7 commit 028c756
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 107 deletions.
54 changes: 31 additions & 23 deletions qBitrr/arr_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,52 @@ class CommandsModel(Model):
Duration = TextField()
Exception = TextField()
Trigger = TextField()
Result = IntegerField()


class MoviesModel(Model):
class MoviesMetadataModel(Model):
Id = IntegerField()
ImdbId = IntegerField()
TmdbId = IntegerField()
ImdbId = TextField()
Images = TextField()
Genres = TextField()
Title = TextField()
TitleSlug = TextField()
SortTitle = TextField()
CleanTitle = TextField()
OriginalTitle = TextField()
CleanOriginalTitle = TextField()
OriginalLanguage = TextField()
Status = IntegerField()
Overview = TextField()
Images = TextField()
Path = TextField()
Monitored = BooleanField()
ProfileId = IntegerField()
LastInfoSync = DateTimeField(formats=["%Y-%m-%d %H:%M:%S.%f"])
LastDiskSync = DateTimeField(formats=["%Y-%m-%d %H:%M:%S.%f"])
Runtime = IntegerField()
InCinemas = DateTimeField(formats=["%Y-%m-%d %H:%M:%S.%f"])
PhysicalRelease = DateTimeField(formats=["%Y-%m-%d %H:%M:%S.%f"])
DigitalRelease = DateTimeField(formats=["%Y-%m-%d %H:%M:%S.%f"])
Year = IntegerField()
Added = DateTimeField(formats=["%Y-%m-%d %H:%M:%S.%f"])
SecondaryYear = IntegerField()
Ratings = TextField()
Genres = TextField()
Tags = TextField()
Recommendations = TextField()
Certification = TextField()
AddOptions = TextField()
MovieFileId = IntegerField()
TmdbId = IntegerField()
PhysicalRelease = DateTimeField(formats=["%Y-%m-%d %H:%M:%S.%f"])
YouTubeTrailerId = TextField()
Studio = TextField()
Overview = TextField()
Website = TextField()
Popularity = IntegerField()
CollectionTmdbId = IntegerField()
CollectionTitle = TextField()


class MoviesModel(Model):
Id = IntegerField()
Path = TextField()
Monitored = IntegerField()
ProfileId = IntegerField()
Added = DateTimeField(formats=["%Y-%m-%d %H:%M:%S.%f"])
Tags = TextField()
AddOptions = TextField()
MovieFileId = IntegerField()
MinimumAvailability = IntegerField()
# HasPreDBEntry = IntegerField()
SecondaryYear = IntegerField()
Collection = TextField()
Recommendations = TextField()
OriginalTitle = IntegerField()
DigitalRelease = DateTimeField(formats=["%Y-%m-%d %H:%M:%S.%f"])
MovieMetadataId = IntegerField()


class EpisodesModel(Model):
Expand Down Expand Up @@ -86,7 +94,7 @@ class SeriesModel(Model):
Id = IntegerField()
TvdbId = IntegerField()
TvRageId = IntegerField()
# ImdbId = TextField()
ImdbId = TextField()
Title = TextField()
TitleSlug = TextField()
CleanTitle = TextField()
Expand Down
166 changes: 82 additions & 84 deletions qBitrr/arss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import contextlib
import itertools
import logging
import os
import pathlib
import re
import shutil
Expand All @@ -13,18 +12,23 @@
from concurrent.futures import ThreadPoolExecutor
from copy import copy
from datetime import datetime, timedelta, timezone
from sqlite3 import DatabaseError
from typing import TYPE_CHECKING, Callable, Iterable, Iterator, NoReturn

import ffmpeg
import pathos
import qbittorrentapi
import requests
from peewee import JOIN, DatabaseError, SqliteDatabase
from peewee import JOIN, SqliteDatabase
from pyarr import RadarrAPI, SonarrAPI
from qbittorrentapi import TorrentDictionary, TorrentStates

from qBitrr.arr_tables import CommandsModel, EpisodesModel, MoviesModel, SeriesModel
from qBitrr.arr_tables import (
CommandsModel,
EpisodesModel,
MoviesMetadataModel,
MoviesModel,
SeriesModel,
)
from qBitrr.config import (
APPDATA_FOLDER,
COMPLETED_DOWNLOAD_FOLDER,
Expand Down Expand Up @@ -462,6 +466,7 @@ def __init__(
self.search_setup_completed = False
self.model_arr_file: EpisodesModel | MoviesModel = None
self.model_arr_series_file: SeriesModel = None
self.model_arr_movies_file: MoviesMetadataModel = None

self.model_arr_command: CommandsModel = None
self.model_file: EpisodeFilesModel | MoviesFilesModel = None
Expand Down Expand Up @@ -536,12 +541,12 @@ def _get_arr_modes(
) -> tuple[
type[EpisodesModel] | type[MoviesModel],
type[CommandsModel],
type[SeriesModel] | None,
type[SeriesModel] | type[MoviesMetadataModel],
]:
if self.type == "sonarr":
return EpisodesModel, CommandsModel, SeriesModel
elif self.type == "radarr":
return MoviesModel, CommandsModel, None
return MoviesModel, CommandsModel, MoviesMetadataModel

def _get_models(
self,
Expand Down Expand Up @@ -956,9 +961,10 @@ def arr_db_query_commands_count(self) -> int:
(self.model_arr_command.EndedAt.is_null(True))
& (self.model_arr_command.Name.endswith("Search"))
)
.execute()
.count()
)
return len(list(search_commands))

return search_commands

def _search_todays(self, condition):
if self.prioritize_todays_release:
Expand Down Expand Up @@ -1231,22 +1237,27 @@ def _db_request_update(self, request_ids: dict[str, set[int | str]]):
self.db_update_single_series(db_entry=db_entry, request=True)
elif self.type == "radarr" and any(i in request_ids for i in ["ImdbId", "TmdbId"]):
self.model_arr_file: MoviesModel
condition = self.model_arr_file.Year <= datetime.now().year
condition &= self.model_arr_file.Year > 0
self.model_arr_movies_file: MoviesMetadataModel
condition = self.model_arr_movies_file.Year <= datetime.now().year
condition &= self.model_arr_movies_file.Year > 0
tmdb_con = None
imdb_con = None
if ImdbIds := request_ids.get("ImdbId"):
imdb_con = self.model_arr_file.ImdbId.in_(ImdbIds)
imdb_con = self.model_arr_movies_file.ImdbId.in_(ImdbIds)
if TmdbIds := request_ids.get("TmdbId"):
tmdb_con = self.model_arr_file.TmdbId.in_(TmdbIds)
tmdb_con = self.model_arr_movies_file.TmdbId.in_(TmdbIds)
if tmdb_con and imdb_con:
condition &= tmdb_con | imdb_con
elif tmdb_con:
condition &= tmdb_con
elif imdb_con:
condition &= imdb_con
for db_entry in (
self.model_arr_file.select()
self.model_arr_file.select(self.model_arr_file)
.join(
self.model_arr_movies_file,
on=(self.model_arr_file.MovieMetadataId == self.model_arr_movies_file.Id),
)
.where(condition)
.order_by(self.model_arr_file.Added.desc())
):
Expand Down Expand Up @@ -1330,12 +1341,16 @@ def db_update(self):
):
self.db_update_single_series(db_entry=series, series=True)
elif self.type == "radarr":
for series in (
self.model_arr_file.select()
.where(self.model_arr_file.Year == self.search_current_year)
for movies in (
self.model_arr_file.select(self.model_arr_file)
.join(
self.model_arr_movies_file,
on=(self.model_arr_file.MovieMetadataId == self.model_arr_movies_file.Id),
)
.where(self.model_arr_movies_file.Year == self.search_current_year)
.order_by(self.model_arr_file.Added.desc())
):
self.db_update_single_series(db_entry=series)
self.db_update_single_series(db_entry=movies)
self.logger.trace(f"Finished updating database")

def db_update_single_series(
Expand Down Expand Up @@ -1466,10 +1481,14 @@ def db_update_single_series(
self.model_queue.EntryId == db_entry.Id
).execute()

title = db_entry.Title
metadata = self.model_arr_movies_file.get(
self.model_arr_movies_file.Id == db_entry.MovieMetadataId
)

title = metadata.Title
monitored = db_entry.Monitored
tmdbId = db_entry.TmdbId
year = db_entry.Year
tmdbId = metadata.TmdbId
year = metadata.Year
EntryId = db_entry.Id
MovieFileId = db_entry.MovieFileId

Expand Down Expand Up @@ -2964,7 +2983,7 @@ class Meta:
database = self.arr_db
table_name = "Commands"

if db3:
if self.type == "sonarr":

class Series(db3):
class Meta:
Expand All @@ -2973,6 +2992,15 @@ class Meta:

self.model_arr_series_file = Series

elif self.type == "radarr":

class Movies(db3):
class Meta:
database = self.arr_db
table_name = "MovieMetadata"

self.model_arr_movies_file = Movies

self.model_arr_file = Files
self.model_arr_command = Commands
self.search_setup_completed = True
Expand All @@ -2999,27 +3027,17 @@ def run_request_search(self):
except NoConnectionrException as e:
self.logger.error(e.message)
raise DelayLoopException(length=300, type=e.type)
except DatabaseError as e:
self.logger.error(e)
included_extensions = ["db", "db-shm", "db-wal"]
relevant_path = "/config"
file_names = [
fn
for fn in os.listdir(relevant_path)
if any(fn.endswith(ext) for ext in included_extensions)
]
for f in file_names:
os.remove(relevant_path + "/" + f)
relevant_path_2 = "/config"
file_names_2 = [
fn
for fn in os.listdir(relevant_path_2)
if any(fn.endswith(ext) for ext in included_extensions)
]
for f_2 in file_names_2:
os.remove(relevant_path_2 + "/" + f_2)
cmd = "docker restart qbitrr"
os.system(cmd)
# except DatabaseError as e:
# self.logger.error("Restarting to reconnect to Arr.db")
# included_extensions = ["db", "db-shm", "db-wal"]
# relevant_path = "/config/.config/qBitManager"
# for fn in os.listdir(relevant_path):
# for ext in included_extensions:
# if fn.endswith(self._name + "." + ext):
# self.logger.error(fn)
# os.remove(relevant_path + "/" + fn)
# cmd = "docker restart qbitrr"
# os.system(cmd)
except DelayLoopException:
raise
except Exception as e:
Expand Down Expand Up @@ -3100,27 +3118,17 @@ def run_search_loop(self) -> NoReturn:
self.logger.error(e.message)
self.manager.qbit_manager.should_delay_torrent_scan = True
raise DelayLoopException(length=300, type=e.type)
except DatabaseError as e:
self.logger.error(e)
included_extensions = ["db", "db-shm", "db-wal"]
relevant_path = "/config"
file_names = [
fn
for fn in os.listdir(relevant_path)
if any(fn.endswith(ext) for ext in included_extensions)
]
for f in file_names:
os.remove(relevant_path + "/" + f)
relevant_path_2 = "/config"
file_names_2 = [
fn
for fn in os.listdir(relevant_path_2)
if any(fn.endswith(ext) for ext in included_extensions)
]
for f_2 in file_names_2:
os.remove(relevant_path_2 + "/" + f_2)
cmd = "docker restart qbitrr"
os.system(cmd)
# except DatabaseError as e:
# self.logger.error("Restarting to reconnect to Arr.db")
# included_extensions = ["db", "db-shm", "db-wal"]
# relevant_path = "/config/.config/qBitManager"
# for fn in os.listdir(relevant_path):
# for ext in included_extensions:
# if fn.endswith(self._name + "." + ext):
# self.logger.error(fn)
# os.remove(relevant_path + "/" + fn)
# cmd = "docker restart qbitrr"
# os.system(cmd)
except DelayLoopException:
raise
except ValueError:
Expand Down Expand Up @@ -3156,27 +3164,17 @@ def run_search_loop(self) -> NoReturn:
)
time.sleep(e.length)
self.manager.qbit_manager.should_delay_torrent_scan = False
except DatabaseError as e:
self.logger.error(e)
included_extensions = ["db", "db-shm", "db-wal"]
relevant_path = "/config"
file_names = [
fn
for fn in os.listdir(relevant_path)
if any(fn.endswith(ext) for ext in included_extensions)
]
for f in file_names:
os.remove(relevant_path + "/" + f)
relevant_path_2 = "/config"
file_names_2 = [
fn
for fn in os.listdir(relevant_path_2)
if any(fn.endswith(ext) for ext in included_extensions)
]
for f_2 in file_names_2:
os.remove(relevant_path_2 + "/" + f_2)
cmd = "docker restart qbitrr"
os.system(cmd)
# except DatabaseError as e:
# self.logger.error("Restarting to reconnect to Arr.db")
# included_extensions = ["db", "db-shm", "db-wal"]
# relevant_path = "/config/.config/qBitManager"
# for fn in os.listdir(relevant_path):
# for ext in included_extensions:
# if fn.endswith(self._name + "." + ext):
# self.logger.error(fn)
# os.remove(relevant_path + "/" + fn)
# cmd = "docker restart qbitrr"
# os.system(cmd)
except KeyboardInterrupt:
self.logger.hnotice("Detected Ctrl+C - Terminating process")
sys.exit(0)
Expand Down

0 comments on commit 028c756

Please sign in to comment.