From 385694da916c9dc8f3456a184b1be27bf7fa5ce3 Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Tue, 24 Sep 2024 03:34:32 +0200 Subject: [PATCH] download torrents from imdb links --- pyproject.toml | 6 +++++- src/torrent_downloader/app.py | 4 ++++ src/torrent_downloader/imdb.py | 12 ++++++++++++ static/style.css | 1 + 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/torrent_downloader/imdb.py diff --git a/pyproject.toml b/pyproject.toml index decce9f..90f30b1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,11 @@ dev-dependencies = [ allow-direct-references = true [tool.hatch.build.targets.wheel] -packages = ["src/torrent_downloader"] +packages = [ + "src/torrent_downloader", + "static", + "templates", +] [tool.rye.scripts] client = "python -m torrent_downloader" diff --git a/src/torrent_downloader/app.py b/src/torrent_downloader/app.py index ab32515..b850c06 100644 --- a/src/torrent_downloader/app.py +++ b/src/torrent_downloader/app.py @@ -8,6 +8,7 @@ from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates from torrent_downloader.client import TorrentDownloader +from torrent_downloader.imdb import search_string_from_url app = FastAPI() app.mount("/static", StaticFiles(directory="static"), name="static") @@ -53,6 +54,9 @@ async def search_for_torrents(request: Request): @app.get("/list", response_class=HTMLResponse) @hx async def list_torrents(request: Request, query: str, downloader=Depends(get_downloader)): + if query.startswith("https") and "imdb.com" in query: + query = search_string_from_url(query) + torrents = downloader.search(query) return templates.TemplateResponse( request=request, name="torrents.html", context={"torrents": torrents} diff --git a/src/torrent_downloader/imdb.py b/src/torrent_downloader/imdb.py new file mode 100644 index 0000000..1258267 --- /dev/null +++ b/src/torrent_downloader/imdb.py @@ -0,0 +1,12 @@ +from urllib.parse import unquote, urlsplit + +from imdb import Cinemagoer + + +def search_string_from_url(url: str) -> str: + print(url) + _, _, imdb_id = urlsplit(unquote(url)).path.strip("/").rpartition("/") + + cg = Cinemagoer() + movie = cg.get_movie(imdb_id[2:]) + return f"{movie['title']} {movie['year']}" diff --git a/static/style.css b/static/style.css index 60ff6e1..4506f07 100644 --- a/static/style.css +++ b/static/style.css @@ -87,4 +87,5 @@ ul.torrents li { } ul.torrents li:hover { background-color: bisque; + cursor: pointer; }