Skip to content

Commit

Permalink
Move parser to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
Spoked authored and Spoked committed Jan 6, 2024
1 parent a1bae19 commit 30ecdc3
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 67 deletions.
1 change: 0 additions & 1 deletion backend/program/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def start(self):
super().start()
self.running = True


def run(self):
while self.running:
if self.validate():
Expand Down
5 changes: 1 addition & 4 deletions backend/program/media/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
Library,
LibraryPartial,
)
from utils.utils import parser
from program.scrapers import Scraping
from program.realdebrid import Debrid
from program.symlink import Symlinker
from utils.parser import parser


class MediaItem:
Expand Down
2 changes: 1 addition & 1 deletion backend/program/realdebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from utils.logger import logger
from utils.request import get, post, ping
from utils.settings import settings_manager
from utils.utils import parser
from utils.parser import parser


WANTED_FORMATS = [".mkv", ".mp4", ".avi"]
Expand Down
6 changes: 3 additions & 3 deletions backend/program/scrapers/jackett.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
""" Jackett scraper module """
from typing import Optional
from pydantic import BaseModel, Field
from pydantic import BaseModel
from requests import RequestException
from utils.logger import logger
from utils.settings import settings_manager
from utils.utils import parser
from utils.request import RateLimitExceeded, get, ping, RateLimiter
from utils.parser import parser
from utils.request import RateLimitExceeded, get, RateLimiter


class JackettConfig(BaseModel):
Expand Down
3 changes: 1 addition & 2 deletions backend/program/scrapers/orionoid.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
""" Orionoid scraper module """
from datetime import datetime
from typing import Optional
from pydantic import BaseModel
from requests.exceptions import RequestException
from utils.logger import logger
from utils.request import RateLimitExceeded, RateLimiter, get
from utils.settings import settings_manager
from utils.utils import parser
from utils.parser import parser

KEY_APP = "D3CH6HMX9KD9EMD68RXRCDUNBDJV5HRR"

Expand Down
2 changes: 1 addition & 1 deletion backend/program/scrapers/torrentio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from utils.logger import logger
from utils.request import RateLimitExceeded, get, RateLimiter
from utils.settings import settings_manager
from utils.utils import parser
from utils.parser import parser


class TorrentioConfig(BaseModel):
Expand Down
56 changes: 56 additions & 0 deletions backend/utils/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import PTN


class Parser:
def __init__(self):
self.resolution = ["1080p", "720p"]
self.language = ["English"]

def _parse(self, string):
parse = PTN.parse(string)

# episodes
episodes = []
if parse.get("episode", False):
episode = parse.get("episode")
if type(episode) == list:
for sub_episode in episode:
episodes.append(int(sub_episode))
else:
episodes.append(int(episode))

season = parse.get("season")

resolution = parse.get("resolution")
quality = parse.get("quality")
language = parse.get("language")
if not language:
language = "English"
extended = parse.get("extended")

return {
"episodes": episodes or [],
"resolution": resolution or [],
"language": language or [],
"extended": extended,
"season": season,
}

def episodes(self, string):
parse = self._parse(string)
return parse["episodes"]

def episodes_in_season(self, season, string):
parse = self._parse(string)
if parse["season"] == season:
return parse["episodes"]
return []

def parse(self, string):
parse = self._parse(string)
return (
parse["resolution"] in self.resolution
and parse["language"] in self.language
)

parser = Parser()
55 changes: 0 additions & 55 deletions backend/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import threading
import time
import PTN


class Pickly(threading.Thread):
Expand Down Expand Up @@ -38,57 +37,3 @@ def run(self):
break
time.sleep(i)


class Parser:
def __init__(self):
self.resolution = ["1080p", "720p"]
self.language = ["English"]

def _parse(self, string):
parse = PTN.parse(string)

# episodes
episodes = []
if parse.get("episode", False):
episode = parse.get("episode")
if type(episode) == list:
for sub_episode in episode:
episodes.append(int(sub_episode))
else:
episodes.append(int(episode))

season = parse.get("season")

resolution = parse.get("resolution")
quality = parse.get("quality")
language = parse.get("language")
if not language:
language = "English"
extended = parse.get("extended")

return {
"episodes": episodes or [],
"resolution": resolution or [],
"language": language or [],
"extended": extended,
"season": season,
}

def episodes(self, string):
parse = self._parse(string)
return parse["episodes"]

def episodes_in_season(self, season, string):
parse = self._parse(string)
if parse["season"] == season:
return parse["episodes"]
return []

def parse(self, string):
parse = self._parse(string)
return (
parse["resolution"] in self.resolution
and parse["language"] in self.language
)

parser = Parser()

0 comments on commit 30ecdc3

Please sign in to comment.