22import json
33import posixpath
44import time
5+ import uuid
56from datetime import datetime
67from typing import Literal
78from urllib .parse import urlencode
89
910from aiohttp import ClientResponse , ClientSession
1011from pydantic import BaseModel , TypeAdapter
11- from sqlmodel import Session
12+ from sqlmodel import Session , select
1213from torf import BdecodeError , MetainfoError , ReadError , Torrent
1314
1415from app .internal .indexers .abstract import SessionContainer
2122 TorrentSource ,
2223 UsenetSource ,
2324)
24- from app .internal .notifications import send_all_notifications
25+ from app .internal .notifications import (
26+ send_all_manual_notifications ,
27+ send_all_notifications ,
28+ )
2529from app .internal .prowlarr .source_metadata import edit_source_metadata
2630from app .internal .prowlarr .util import (
2731 prowlarr_config ,
@@ -55,12 +59,13 @@ async def _get_torrent_info_hash(
5559
5660
5761async def start_download (
62+ * ,
5863 session : Session ,
5964 client_session : ClientSession ,
6065 guid : str ,
6166 indexer_id : int ,
62- book_asin : str ,
6367 prowlarr_source : ProwlarrSource | None = None ,
68+ asin_or_uuid : str | None = None ,
6469) -> ClientResponse :
6570 prowlarr_config .raise_if_invalid (session )
6671 base_url = prowlarr_config .get_base_url (session )
@@ -71,6 +76,13 @@ async def start_download(
7176 logger .debug ("Starting download" , guid = guid )
7277 headers = {"X-Api-Key" : api_key , "User-Agent" : USER_AGENT }
7378
79+ manual_book_request : ManualBookRequest | None = None
80+ try :
81+ uuid_obj = uuid .UUID (asin_or_uuid )
82+ manual_book_request = session .get (ManualBookRequest , uuid_obj )
83+ except ValueError :
84+ pass
85+
7486 async with client_session .post (
7587 url ,
7688 json = {"guid" : guid , "indexerId" : indexer_id },
@@ -83,18 +95,31 @@ async def start_download(
8395 response = response ,
8496 text = await response .text (),
8597 )
86- await send_all_notifications (
87- EventEnum .on_failed_download ,
88- book_asin ,
89- {
90- "errorStatus" : str (response .status ),
91- "errorReason" : response .reason or "<unknown>" ,
92- },
93- )
98+
99+ if manual_book_request :
100+ await send_all_manual_notifications (
101+ EventEnum .on_failed_download ,
102+ manual_book_request ,
103+ {
104+ "errorStatus" : str (response .status ),
105+ "errorReason" : response .reason or "<unknown>" ,
106+ },
107+ )
108+ else :
109+ await send_all_notifications (
110+ EventEnum .on_failed_download ,
111+ asin_or_uuid ,
112+ {
113+ "errorStatus" : str (response .status ),
114+ "errorReason" : response .reason or "<unknown>" ,
115+ },
116+ )
94117 return response
95118
96119 # Find additional metadata/replacements to pass along notifications
97- additional_replacements : dict [str , str ] = {"bookASIN" : book_asin }
120+ additional_replacements : dict [str , str ] = (
121+ {"bookASIN" : asin_or_uuid } if asin_or_uuid else {}
122+ )
98123 if prowlarr_source :
99124 if prowlarr_source .download_url and prowlarr_source .protocol == "torrent" :
100125 if info_hash := await _get_torrent_info_hash (
@@ -113,11 +138,28 @@ async def start_download(
113138 additional_replacements ["sourceProtocol" ] = prowlarr_source .protocol
114139
115140 logger .debug ("Download successfully started" , guid = guid )
116- await send_all_notifications (
117- EventEnum .on_successful_download ,
118- book_asin ,
119- additional_replacements ,
120- )
141+ if manual_book_request :
142+ manual_book_request .downloaded = True
143+ session .add (manual_book_request )
144+ session .commit ()
145+ await send_all_manual_notifications (
146+ EventEnum .on_successful_download ,
147+ manual_book_request ,
148+ additional_replacements ,
149+ )
150+ else :
151+ same_books = session .exec (
152+ select (Audiobook ).where (Audiobook .asin == asin_or_uuid )
153+ ).all ()
154+ for b in same_books :
155+ b .downloaded = True
156+ session .add (b )
157+
158+ await send_all_notifications (
159+ EventEnum .on_successful_download ,
160+ asin_or_uuid ,
161+ additional_replacements ,
162+ )
121163
122164 return response
123165
0 commit comments