Skip to content

Commit

Permalink
Merge pull request #78 from dreulavelle/gb_tweaks
Browse files Browse the repository at this point in the history
Add tweaks, add some kind of parsing for torrents etc.
  • Loading branch information
Gaisberg authored Dec 22, 2023
2 parents 4fdf3b3 + 3625577 commit 2f668f2
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 224 deletions.
8 changes: 6 additions & 2 deletions backend/program/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Program main module"""
import os
import threading
import time
from typing import Optional
from pydantic import BaseModel, HttpUrl, Field
from program.symlink import Symlinker
Expand All @@ -10,6 +12,7 @@
from program.debrid.realdebrid import Debrid as RealDebrid
from program.content import Content
from program.scrapers import Scraping
from utils.utils import Pickly


# Pydantic models for configuration
Expand Down Expand Up @@ -58,7 +61,8 @@ def __init__(self):
self.settings = settings_manager.get_all()
self.media_items = MediaItemContainer(items=[])
self.data_path = get_data_path()
self.media_items.load(os.path.join(self.data_path, "media.pkl"))
self.pickly = Pickly(self.media_items, self.data_path)
self.pickly.start()
self.threads = [
Content(self.media_items), # Content must be first
Plex(self.media_items),
Expand All @@ -75,4 +79,4 @@ def start(self):
def stop(self):
for thread in self.threads:
thread.stop()
self.media_items.save(os.path.join(self.data_path, "media.pkl"))
self.pickly.stop()
1 change: 1 addition & 0 deletions backend/program/content/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def run(self) -> None:
for service in self.services:
if service.initialized:
service.run()
time.sleep(1)

def start(self) -> None:
self.running = True
Expand Down
1 change: 0 additions & 1 deletion backend/program/content/mdblist.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def run(self):
items += self._get_items_from_list(
list_id, self.settings["api_key"]
)

new_items = [item for item in items if item not in self.media_items]
container = self.updater.create_items(new_items)
for item in container:
Expand Down
10 changes: 3 additions & 7 deletions backend/program/content/plex_watchlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ def _get_items_from_plex_watchlist(self) -> list:
for item in items:
imdb_id = next((guid.split("//")[-1] for guid in item.get("guids") if "imdb://" in guid), None)
if imdb_id:
tvdb_id = next((guid.split("//")[-1] for guid in item.get("guids") if "tvdb://" in guid), None)
if tvdb_id:
# TODO: Convert tvdb to imdb. Will work on this later.
logger.debug("Found imdb id %s and tvdb id %s for %s", imdb_id, tvdb_id, item.get("title"))
else:
# TODO: Needs testing to determine if streams only return tvdb/imdb or if there are other options.
logger.debug("No imdb id or tvdb id found for %s", item.get("title"))
ids.append(imdb_id)
else:
logger.warning("Could not find IMDb ID for %s in Plex watchlist", item.get("title"))
return ids
44 changes: 24 additions & 20 deletions backend/program/debrid/realdebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def get_user():
return response.json()


class Debrid(
threading.Thread
): # TODO CHECK TORRENTS LIST BEFORE DOWNLOAD, IF DOWNLOADED AND NOT IN LIBRARY CHOOSE ANOTHER TORRENT
class Debrid(threading.Thread):
"""Real-Debrid API Wrapper"""

def __init__(self, media_items: MediaItemContainer):
Expand All @@ -41,7 +39,9 @@ def __init__(self, media_items: MediaItemContainer):
if self._validate_settings():
self._torrents = {}
break
logger.error("Realdebrid settings incorrect or not premium, retrying in 2...")
logger.error(
"Realdebrid settings incorrect or not premium, retrying in 2..."
)
time.sleep(2)

def _validate_settings(self):
Expand All @@ -59,6 +59,7 @@ def _validate_settings(self):
def run(self):
while self.running:
self.download()
time.sleep(1)

def start(self) -> None:
self.running = True
Expand Down Expand Up @@ -96,36 +97,44 @@ def download(self):

def _download(self, item):
"""Download movie from real-debrid.com"""
downloaded = 0
self._check_stream_availability(item)
self._determine_best_stream(item)
if not self._is_downloaded(item):
self._download_item(item)
return 1
return 0
downloaded = self._download_item(item)
self._update_torrent_info(item)
return downloaded

def _is_downloaded(self, item):
if not item.get("active_stream", None):
return False
torrents = self.get_torrents()
if any(torrent.hash == item.active_stream.get("hash") for torrent in torrents):
logger.debug("Torrent already downloaded")
return True
for torrent in torrents:
if torrent.hash == item.active_stream.get("hash"):
item.set("active_stream.id", torrent.id)
logger.debug("Torrent already downloaded")
return True
return False

def _update_torrent_info(self, item):
info = self.get_torrent_info(item.get("active_stream")["id"])
item.active_stream["name"] = info.filename

def _download_item(self, item):
if not item.get("active_stream", None):
return 0
request_id = self.add_magnet(item)

time.sleep(0.3)
self.select_files(request_id, item)
item.set("active_stream.id", request_id)

if item.type == "movie":
log_string = item.title
if item.type == "season":
log_string = f"{item.parent.title} S{item.number}"
if item.type == "episode":
log_string = f"{item.parent.parent.title} S{item.parent.number}E{item.number}"
log_string = (
f"{item.parent.parent.title} S{item.parent.number}E{item.number}"
)

logger.debug("Downloaded %s", log_string)
return 1
Expand Down Expand Up @@ -183,9 +192,7 @@ def _determine_best_stream(self, item) -> bool:
def _check_stream_availability(self, item: MediaItem):
if len(item.streams) == 0:
return
streams = "/".join(
list(item.streams)
)
streams = "/".join(list(item.streams))
response = get(
f"https://api.real-debrid.com/rest/1.0/torrents/instantAvailability/{streams}/",
additional_headers=self.auth_headers,
Expand Down Expand Up @@ -276,10 +283,7 @@ def get_torrents(self) -> str:
"""Add magnet link to real-debrid.com"""
response = get(
"https://api.real-debrid.com/rest/1.0/torrents/",
data = {
"offset": 0,
"limit": 2500
},
data={"offset": 0, "limit": 2500},
additional_headers=self.auth_headers,
)
if response.is_ok:
Expand Down
Loading

0 comments on commit 2f668f2

Please sign in to comment.