Skip to content

Commit

Permalink
Merge pull request #565 from rivenmedia/revert-564-fix_ruff_errors
Browse files Browse the repository at this point in the history
Revert "fix: fix around 200 ruff errors"
  • Loading branch information
Gaisberg authored Jul 23, 2024
2 parents fe923f4 + 19c499e commit 0c8ad75
Show file tree
Hide file tree
Showing 45 changed files with 302 additions and 235 deletions.
12 changes: 9 additions & 3 deletions src/controllers/actions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from typing import Any, Dict
from typing import Any, Dict, Optional

import pydantic
from fastapi import APIRouter, Request
from program.media.item import MediaItem
from program.content.overseerr import Overseerr
from program.indexers.trakt import TraktIndexer, get_imdbid_from_tmdb
from program.media.item import MediaItem, Show
from requests import RequestException
from utils.logger import logger

from .models.overseerr import OverseerrWebhook

router = APIRouter(
prefix="/actions",
responses={404: {"description": "Not found"}},
Expand All @@ -15,7 +21,7 @@ async def request(request: Request, imdb_id: str) -> Dict[str, Any]:
try:
new_item = MediaItem({"imdb_id": imdb_id, "requested_by": "manually"})
request.app.program.add_to_queue(new_item)
except Exception:
except Exception as e:
logger.error(f"Failed to create item from imdb_id: {imdb_id}")
return {"success": False, "message": "Failed to create item from imdb_id"}

Expand Down
15 changes: 8 additions & 7 deletions src/controllers/default.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import time

import program.db.db_functions as DB
import requests
from fastapi import APIRouter, HTTPException, Request
from program.media.item import MediaItem
from program.content.trakt import TraktContent
from program.db.db import db
from program.media.item import Episode, MediaItem, Movie, Season, Show
from program.media.state import States
from program.scrapers import Scraping
from program.settings.manager import settings_manager
from sqlalchemy import func, select
from program.media.item import Episode, MediaItem, Movie, Season, Show
from program.db.db import db
from sqlalchemy import select, func
import program.db.db_functions as DB

router = APIRouter(
responses={404: {"description": "Not found"}},
Expand Down Expand Up @@ -101,12 +102,12 @@ async def trakt_oauth_callback(code: str, request: Request):


@router.get("/stats")
async def get_stats(_: Request):
async def get_stats(request: Request):
payload = {}
with db.Session() as session:

movies_symlinks = session.execute(select(func.count(Movie._id)).where(Movie.symlinked is True)).scalar_one()
episodes_symlinks = session.execute(select(func.count(Episode._id)).where(Episode.symlinked is True)).scalar_one()
movies_symlinks = session.execute(select(func.count(Movie._id)).where(Movie.symlinked == True)).scalar_one()
episodes_symlinks = session.execute(select(func.count(Episode._id)).where(Episode.symlinked == True)).scalar_one()
total_symlinks = movies_symlinks + episodes_symlinks

total_movies = session.execute(select(func.count(Movie._id))).scalar_one()
Expand Down
14 changes: 7 additions & 7 deletions src/controllers/items.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from typing import List, Optional

import Levenshtein
import program.db.db_functions as DB
from fastapi import APIRouter, HTTPException, Request
from program.db.db import db
from program.media.item import Episode, MediaItem, Season
from sqlalchemy import select, func
import program.db.db_functions as DB
from program.media.item import Episode, MediaItem, Movie, Season, Show
from program.media.state import States
from program.symlink import Symlinker
from pydantic import BaseModel
from sqlalchemy import func, select
from utils.logger import logger

router = APIRouter(
Expand Down Expand Up @@ -36,7 +36,7 @@ async def get_states():
description="Fetch media items with optional filters and pagination",
)
async def get_items(
_: Request,
request: Request,
limit: Optional[int] = 50,
page: Optional[int] = 1,
type: Optional[str] = None,
Expand Down Expand Up @@ -115,7 +115,7 @@ async def get_items(


@router.get("/extended/{item_id}")
async def get_extended_item_info(_: Request, item_id: str):
async def get_extended_item_info(request: Request, item_id: str):
with db.Session() as session:
item = DB._get_item_from_db(session, MediaItem({"imdb_id":str(item_id)}))
if item is None:
Expand Down Expand Up @@ -199,12 +199,12 @@ async def remove_item(
}
except Exception as e:
logger.error(f"Failed to remove item with {id_type} {item_id or imdb_id}: {e}")
raise HTTPException from e(status_code=500, detail="Internal server error")
raise HTTPException(status_code=500, detail="Internal server error")


@router.get("/imdb/{imdb_id}")
async def get_imdb_info(
_: Request,
request: Request,
imdb_id: str,
season: Optional[int] = None,
episode: Optional[int] = None,
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async def set_settings(settings: List[SetSettings]):
settings_manager.load(settings_dict=updated_settings.model_dump())
settings_manager.save() # Ensure the changes are persisted
except ValidationError as e:
raise HTTPException from e(
raise HTTPException(
status_code=400,
detail=f"Failed to update settings: {str(e)}",
)
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import pydantic
from fastapi import APIRouter, Request
from program.content.overseerr import Overseerr
from program.indexers.trakt import get_imdbid_from_tmdb
from program.media.item import MediaItem
from program.indexers.trakt import TraktIndexer, get_imdbid_from_tmdb
from program.media.item import MediaItem, Show
from requests import RequestException
from utils.logger import logger

Expand Down Expand Up @@ -38,7 +38,7 @@ async def overseerr(request: Request) -> Dict[str, Any]:
if not imdb_id:
try:
imdb_id = get_imdbid_from_tmdb(req.media.tmdbId)
except RequestException:
except RequestException as e:
logger.error(f"Failed to get imdb_id from TMDB: {req.media.tmdbId}")
return {"success": False, "message": "Failed to get imdb_id from TMDB", "title": req.subject}
if not imdb_id:
Expand All @@ -59,7 +59,7 @@ async def overseerr(request: Request) -> Dict[str, Any]:
try:
new_item = MediaItem({"imdb_id": imdb_id, "requested_by": "overseerr"})
request.app.program.add_to_queue(new_item)
except Exception:
except Exception as e:
logger.error(f"Failed to create item from imdb_id: {imdb_id}")
return {"success": False, "message": "Failed to create item from imdb_id", "title": req.subject}

Expand Down
6 changes: 3 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import traceback

import uvicorn
from controllers.actions import router as actions_router
from controllers.default import router as default_router
from controllers.items import router as items_router

# from controllers.metrics import router as metrics_router
from controllers.settings import router as settings_router
from controllers.tmdb import router as tmdb_router
from controllers.webhooks import router as webhooks_router
from controllers.actions import router as actions_router
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from program import Program
Expand Down Expand Up @@ -99,8 +99,8 @@ def run_in_thread(self):
self.should_exit = True
sys.exit(0)

def signal_handler():
logger.log("PROGRAM","Exiting Gracefully.")
def signal_handler(sig, frame):
logger.log('PROGRAM','Exiting Gracefully.')
app.program.stop()
sys.exit(0)

Expand Down
5 changes: 5 additions & 0 deletions src/program/content/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .listrr import Listrr
from .mdblist import Mdblist
from .overseerr import Overseerr
from .plex_watchlist import PlexWatchlist
from .trakt import TraktContent
2 changes: 1 addition & 1 deletion src/program/content/mdblist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from program.media.item import MediaItem
from program.settings.manager import settings_manager
from utils.logger import logger
from utils.ratelimiter import RateLimiter, RateLimitExceeded
from utils.request import get, ping
from utils.ratelimiter import RateLimiter, RateLimitExceeded


class Mdblist:
Expand Down
12 changes: 6 additions & 6 deletions src/program/content/overseerr.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def validate(self) -> bool:
)
return False
return response.is_ok
except (ConnectionError, RetryError, MaxRetryError, NewConnectionError):
logger.error("Overseerr URL is not reachable, or it timed out")
except (ConnectionError, RetryError, MaxRetryError, NewConnectionError) as e:
logger.error(f"Overseerr URL is not reachable, or it timed out")
return False
except Exception as e:
logger.error(f"Unexpected error during Overseerr validation: {str(e)}")
Expand All @@ -71,7 +71,7 @@ def run(self):
logger.error(f"Unexpected error during fetching requests: {str(e)}")
return

if not response.is_ok or not hasattr(response.data, "pageInfo") or getattr(response.data.pageInfo, "results", 0) == 0:
if not response.is_ok or not hasattr(response.data, 'pageInfo') or getattr(response.data.pageInfo, 'results', 0) == 0:
return

# Lets look at approved items only that are only in the pending state
Expand Down Expand Up @@ -117,13 +117,13 @@ def get_imdb_id(self, data) -> str:
)
except (ConnectionError, RetryError, MaxRetryError) as e:
logger.error(f"Failed to fetch media details from overseerr: {str(e)}")
return None
return
except Exception as e:
logger.error(f"Unexpected error during fetching media details: {str(e)}")
return None
return

if not response.is_ok or not hasattr(response.data, "externalIds"):
return None
return

imdb_id = getattr(response.data.externalIds, "imdbId", None)
if imdb_id:
Expand Down
15 changes: 9 additions & 6 deletions src/program/content/trakt.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""Trakt content module"""
import re
import time
from urllib.parse import urlencode
from types import SimpleNamespace
from urllib.parse import urlencode, urlparse

import regex
from program.media.item import MediaItem, Movie, Show
from program.settings.manager import settings_manager
from requests import RequestException
from utils.logger import logger
from utils.ratelimiter import RateLimiter
from utils.request import get, post
from utils.ratelimiter import RateLimiter


class TraktContent:
Expand Down Expand Up @@ -41,7 +43,7 @@ def validate(self) -> bool:
logger.error("Trakt API key is not set.")
return False
response = get(f"{self.api_url}/lists/2", additional_headers=self.headers)
if not getattr(response.data, "name", None):
if not getattr(response.data, 'name', None):
logger.error("Invalid user settings received from Trakt.")
return False
return True
Expand Down Expand Up @@ -220,7 +222,8 @@ def perform_oauth_flow(self) -> str:
"client_id": self.settings.oauth_client_id,
"redirect_uri": self.settings.oauth_redirect_uri,
}
return f"{self.api_url}/oauth/authorize?{urlencode(params)}"
auth_url = f"{self.api_url}/oauth/authorize?{urlencode(params)}"
return auth_url

def handle_oauth_callback(self, code: str) -> bool:
"""Handle the OAuth callback and exchange the code for an access token."""
Expand Down Expand Up @@ -355,6 +358,6 @@ def _resolve_short_url(short_url) -> str or None:
return None

patterns: dict[str, re.Pattern] = {
"user_list": re.compile(r"https://trakt.tv/users/([^/]+)/lists/([^/]+)"),
"short_list": re.compile(r"https://trakt.tv/lists/\d+")
"user_list": re.compile(r'https://trakt.tv/users/([^/]+)/lists/([^/]+)'),
"short_list": re.compile(r'https://trakt.tv/lists/\d+')
}
1 change: 1 addition & 0 deletions src/program/db/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .db import db
15 changes: 6 additions & 9 deletions src/program/db/db.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import os

from alembic.autogenerate import compare_metadata
from alembic.runtime.migration import MigrationContext
from program.settings.manager import settings_manager
from sqla_wrapper import Alembic, SQLAlchemy
from program.settings.manager import settings_manager
from utils import data_dir_path

db = SQLAlchemy(settings_manager.settings.database.host)

script_location = data_dir_path / "alembic/"


import os
if not os.path.exists(script_location):
os.makedirs(script_location)

alembic = Alembic(db, script_location)
alembic.init(script_location)

from alembic.autogenerate import compare_metadata
from alembic.runtime.migration import MigrationContext

# https://stackoverflow.com/questions/61374525/how-do-i-check-if-alembic-migrations-need-to-be-generated
def need_upgrade_check() -> bool:
Expand All @@ -26,11 +24,10 @@ def need_upgrade_check() -> bool:
diff = compare_metadata(mc, db.Model.metadata)
return diff != []


def run_migrations() -> None:
try:
if need_upgrade_check():
alembic.revision("auto-upg")
alembic.upgrade()
except Exception as _:
alembic.upgrade()
except:
alembic.upgrade()
14 changes: 7 additions & 7 deletions src/program/db/db_functions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import os

from program.media.item import Episode, MediaItem, Movie, Season, Show
from program.types import Event
from sqlalchemy import func, select
from sqlalchemy import select, func
from sqlalchemy.orm import joinedload
from utils.logger import logger

from program.types import Event
from .db import db


Expand All @@ -18,7 +16,7 @@ def _ensure_item_exists_in_db(item:MediaItem) -> bool:
def _get_item_type_from_db(item: MediaItem) -> str:
with db.Session() as session:
if item._id is None:
return session.execute(select(MediaItem.type).where( (MediaItem.imdb_id==item.imdb_id ) & ( (MediaItem.type == "show") | (MediaItem.type == "movie") ) )).scalar_one()
return session.execute(select(MediaItem.type).where( (MediaItem.imdb_id==item.imdb_id ) & ( (MediaItem.type == 'show') | (MediaItem.type == 'movie') ) )).scalar_one()
return session.execute(select(MediaItem.type).where(MediaItem._id==item._id)).scalar_one()

def _store_item(item: MediaItem):
Expand Down Expand Up @@ -57,7 +55,8 @@ def _get_item_from_db(session, item: MediaItem):
return None

def _check_for_and_run_insertion_required(session, item: MediaItem) -> None:
if _ensure_item_exists_in_db(item) is False and isinstance(item, (Show, Movie, Season, Episode)):
if _ensure_item_exists_in_db(item) == False:
if isinstance(item, (Show, Movie, Season, Episode)):
item.store_state()
session.add(item)
session.commit()
Expand All @@ -83,7 +82,7 @@ def _run_thread_with_db_item(fn, service, program, input_item: MediaItem | None)
all_media_items = False

program._remove_from_running_items(item, service.__name__)
if all_media_items is True:
if all_media_items == True:
for i in res:
program._push_event_queue(Event(emitted_by="_run_thread_with_db_item", item=i))
session.commit()
Expand Down Expand Up @@ -119,6 +118,7 @@ def _run_thread_with_db_item(fn, service, program, input_item: MediaItem | None)

reset = os.getenv("HARD_RESET", None)
if reset is not None and reset.lower() in ["true","1"]:
print("Hard reset detected, dropping all tables") # Logging isn't initialized here yet.
def run_delete(_type):
with db.Session() as session:
all = session.execute(select(_type).options(joinedload("*"))).unique().scalars().all()
Expand Down
7 changes: 3 additions & 4 deletions src/program/downloaders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from program.media.item import MediaItem
from utils.logger import logger

from .alldebrid import AllDebridDownloader
from .realdebrid import RealDebridDownloader
from .alldebrid import AllDebridDownloader
from .torbox import TorBoxDownloader
from program.media.item import MediaItem
from utils.logger import logger


class Downloader:
Expand Down
Loading

0 comments on commit 0c8ad75

Please sign in to comment.