Skip to content

Commit

Permalink
Parser (#119)
Browse files Browse the repository at this point in the history
* Move parser to its own module

* Add ORIGIN to env vars

* Fix overseerr, watchlist, jackett validation.

---------

Co-authored-by: Spoked <Spoked@localhost>
  • Loading branch information
dreulavelle and Spoked authored Jan 8, 2024
1 parent bdbe23b commit 97d2b18
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 35 deletions.
21 changes: 12 additions & 9 deletions backend/program/content/overseerr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Mdblist content module"""
from typing import Optional
from pydantic import BaseModel
from requests import ConnectTimeout
from utils.settings import settings_manager
from utils.logger import logger
from utils.request import get, ping
Expand All @@ -14,34 +13,38 @@ class OverseerrConfig(BaseModel):
url: Optional[str]
api_key: Optional[str]


class Overseerr:
"""Content class for overseerr"""

def __init__(self, media_items: MediaItemContainer):
self.key = "overseerr"
self.settings = OverseerrConfig(**settings_manager.get(f"content.{self.key}"))
if not self.settings.enabled:
logger.debug("Overseerr is set to disabled.")
return
self.headers = {"X-Api-Key": self.settings.api_key}
self.initialized = self.validate_settings()
if not self.initialized:
logger.info("Overseerr is not configured and will not be used.")
return
self.updater = Trakt()
self.media_items = media_items
self.updater = Trakt()
self.not_found_ids = []
logger.info("Overseerr initialized!")

def validate_settings(self):
def validate_settings(self) -> bool:
if not self.settings.enabled:
logger.debug("Overseerr is set to disabled.")
return False
if self.settings.api_key == "" or len(self.settings.api_key) != 68:
logger.error("Overseerr api key is not set.")
return False
try:
response = ping(
self.settings.url + "/api/v1/auth/me",
additional_headers=self.headers,
timeout=1,
timeout=15,
)
return response.ok
except ConnectTimeout:
except Exception:
logger.error("Overseerr url is not reachable.")
return False

def run(self):
Expand Down
9 changes: 5 additions & 4 deletions backend/program/content/plex_watchlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from requests import ConnectTimeout
from utils.request import get, ping
from utils.logger import logger
from utils.settings import settings_manager as settings
from utils.settings import settings_manager
from program.media.container import MediaItemContainer
from program.updaters.trakt import Updater as Trakt
import json
Expand All @@ -14,17 +14,18 @@ class PlexWatchlistConfig(BaseModel):
enabled: bool
rss: Optional[str]


class PlexWatchlist:
"""Class for managing Plex Watchlists"""

def __init__(self, media_items: MediaItemContainer):
self.key = "plex_watchlist"
self.rss_enabled = False
self.settings = PlexWatchlistConfig(**settings.get(f"content.{self.key}"))
self.settings = PlexWatchlistConfig(**settings_manager.get(f"content.{self.key}"))
self.initialized = self.validate_settings()
self.token = settings.get("plex.token")
if not self.initialized:
return
self.token = settings_manager.get("plex.token")
self.media_items = media_items
self.prev_count = 0
self.updater = Trakt()
Expand All @@ -35,7 +36,7 @@ def validate_settings(self):
return False
if self.settings.rss:
try:
response = ping(self.settings.rss, timeout=30)
response = ping(self.settings.rss, timeout=15)
if response.ok:
self.rss_enabled = True
return True
Expand Down
2 changes: 1 addition & 1 deletion backend/program/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, media_items: MediaItemContainer):
self.log_worker_count = False
self.media_items = media_items
self._update_items(init=True)
except Exception as e:
except Exception:
logger.error("Plex is not configured!")
return
logger.info("Plex initialized!")
Expand Down
51 changes: 30 additions & 21 deletions backend/program/scrapers/jackett.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" Jackett scraper module """
from typing import Optional
from pydantic import BaseModel
from requests import RequestException
from requests import ReadTimeout, RequestException
from utils.logger import logger
from utils.settings import settings_manager
from utils.parser import parser
Expand Down Expand Up @@ -32,11 +32,16 @@ def validate_settings(self) -> bool:
if not self.settings.enabled:
return False
if self.settings.url:
url = f"{self.settings.url}/api/v2.0/server/config"
response = get(url=url, retry_if_failed=False)
if response.is_ok:
self.api_key = response.data.api_key
try:
url = f"{self.settings.url}/api/v2.0/server/config"
response = get(url=url, retry_if_failed=False, timeout=60)
if response.is_ok:
self.api_key = response.data.api_key
return True
except ReadTimeout:
return True
except Exception:
return False
logger.info("Jackett is not configured and will not be used.")
return False

Expand Down Expand Up @@ -72,20 +77,24 @@ def api_scrape(self, item):
query = f"&t=tv-search&imdbid={item.parent.parent.imdb_id}&season={item.parent.number}&ep={item.number}"

url = (
f"{self.settings.url}/api/v2.0/indexers/!status:failing,test:passed/results/torznab?apikey={self.api_key}{query}"
f"{self.settings.url}/api/v2.0/indexers/all/results/torznab?apikey={self.api_key}{query}"
)
response = get(url=url, retry_if_failed=False, timeout=30)
if response.is_ok:
data = {}
if not hasattr(response.data['rss']['channel'], "item"):
return {}
for stream in response.data['rss']['channel']['item']:
title = stream.get('title')
for attr in stream.get('torznab:attr', []):
if attr.get('@name') == 'infohash':
infohash = attr.get('@value')
if parser.parse(title) and infohash:
data[infohash] = {"name": title}
if len(data) > 0:
return data
return {}
try:
response = get(url=url, retry_if_failed=False, timeout=60)
if response.is_ok:
data = {}
if not hasattr(response.data['rss']['channel'], "item"):
return {}
for stream in response.data['rss']['channel']['item']:
title = stream.get('title')
for attr in stream.get('torznab:attr', []):
if attr.get('@name') == 'infohash':
infohash = attr.get('@value')
if parser.parse(title) and infohash:
data[infohash] = {"name": title}
if len(data) > 0:
return data
return {}
except ReadTimeout:
logger.debug("Jackett timed out for %s", item.log_string)
return {}
1 change: 1 addition & 0 deletions backend/utils/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def _parse(self, string):
return {
"episodes": episodes or [],
"resolution": resolution or [],
"quality": quality or [],
"language": language or [],
"extended": extended,
"season": season,
Expand Down

0 comments on commit 97d2b18

Please sign in to comment.