feat: Add aria2 download completion hook
This commit is contained in:
1 parent
06fcd7a10f
commit
be9670b653
1 file changed
+65
Executable
+65
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3
|
||||
import base64
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.parse import quote
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
logging.basicConfig(
|
||||
filename=Path(__file__).with_suffix(".log"),
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(levelname)s - %(message)s",
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def is_temp(file_path: Path) -> bool:
|
||||
return ".PRT" in file_path.name
|
||||
|
||||
|
||||
def delete_from_remote(file_path: Path) -> None:
|
||||
url = f"https://u201686.your-storagebox.de/files/_raw/prt/{quote(file_path.name)}"
|
||||
credentials = "u201686:T6672ICVoWAedECH"
|
||||
request = Request(url, method="DELETE", headers={"Authorization": f"Basic {base64.b64encode(credentials.encode()).decode()}"})
|
||||
|
||||
try:
|
||||
with urlopen(request, timeout=2):
|
||||
return
|
||||
except HTTPError as e:
|
||||
if e.code == 404:
|
||||
return
|
||||
|
||||
|
||||
def main():
|
||||
logger.info(sys.argv)
|
||||
|
||||
if len(sys.argv) < 3:
|
||||
logger.error("Missing expected argument: downloaded file path.")
|
||||
return
|
||||
|
||||
downloaded_path = Path(sys.argv[2])
|
||||
if not downloaded_path.is_file():
|
||||
logger.warning("File does not exist: %s", downloaded_path)
|
||||
return
|
||||
|
||||
if not is_temp(downloaded_path):
|
||||
logger.info("File evaluated as non-temp: %s", downloaded_path)
|
||||
return
|
||||
|
||||
target_dir = Path("~/Downloads/temp/").expanduser()
|
||||
target_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
target_path = target_dir / downloaded_path.name
|
||||
downloaded_path.rename(target_path)
|
||||
logger.info("Moved %s to %s", downloaded_path, target_path)
|
||||
try:
|
||||
delete_from_remote(downloaded_path)
|
||||
except Exception as e:
|
||||
logger.error("Error deleting %s from remote: %s", downloaded_path, e)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user