From 43033572ad99023ad616ac15e52bb16da35fefe6 Mon Sep 17 00:00:00 2001
From: Kris Lamoureux <kris@lamoureux.io>
Date: Sat, 15 Mar 2025 23:29:39 -0400
Subject: [PATCH] testing

---
 tarc/main.py | 55 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 47 insertions(+), 8 deletions(-)

diff --git a/tarc/main.py b/tarc/main.py
index 744af84..98fb61d 100644
--- a/tarc/main.py
+++ b/tarc/main.py
@@ -19,7 +19,7 @@ import qbittorrentapi
 from sqlalchemy import create_engine, inspect
 from sqlalchemy.orm import Session
 
-from .models import Base, SchemaVersion, Client
+from .models import Base, SchemaVersion, Client, Torrent, TorrentFile
 
 # SCHEMA format is YYYYMMDDX
 SCHEMA = 202503100
@@ -101,15 +101,17 @@ def auth_qbittorrent(endpoint, username, password):
     return qb
 
 
-def scan_torrents(qb_client, debug=False):
+def scan_torrents(qb_client, engine, debug=False):
     """
     Scan torrents using the provided qBittorrent client.
     """
     torrents = qb_client.torrents_info()
     print(f"[INFO]: There are {len(torrents)} torrents\n")
+
     for torrent in torrents[:2]:
         files = qb_client.torrents_files(torrent.hash)
         trackers = qb_client.torrents_trackers(torrent.hash)
+
         print(f"[name]: {torrent.name}")
         print(f"[infohash_v1]: {torrent.hash}")
         print(f"[content_path]: {torrent.content_path}")
@@ -117,11 +119,48 @@ def scan_torrents(qb_client, debug=False):
         print(f"[completed_on]: {torrent.completed}\n")
         print(f"[trackers]: {len(trackers)}")
         print(f"[file_count]: {len(files)}\n")
-        if debug:
-            print(f"[DEBUG]: {repr(torrent)}")
-            for elem in trackers:
-                print(f"[DEBUG]: Tracker {repr(elem)}")
-            print("\n", end="")
+
+        with Session(engine) as session:
+            completed_on = (
+                datetime.fromtimestamp(torrent.completed) if torrent.completed else None
+            )
+            torrent_instance = Torrent(
+                info_hash_v1=torrent.hash,
+                file_count=len(files),
+                completed_on=completed_on,
+            )
+
+            if not session.query(Torrent).filter_by(info_hash_v1=torrent.hash).first():
+                session.add(torrent_instance)
+                session.commit()
+                print(f"[INFO]: Added torrent {torrent.name} to the database.")
+
+            for file in files:
+                if (
+                    not session.query(TorrentFile)
+                    .filter_by(file_path=file.name)
+                    .first()
+                ):
+                    torrent_file_instance = TorrentFile(
+                        torrent_id=torrent_instance.id,
+                        file_id=file.id,
+                        client_id=1,
+                        file_index=file.index,
+                        file_path=file.name,
+                        is_downloaded=file.progress == 1,
+                        last_checked=datetime.now(timezone.utc),
+                    )
+                    session.add(torrent_file_instance)
+                    session.commit()
+            print(
+                f"[INFO]: Added {len(files)} files of torrent {torrent.name} to the database."
+            )
+
+            if debug:
+                print(f"[DEBUG]: {repr(torrent)}")
+                for elem in trackers:
+                    print(f"[DEBUG]: Tracker {repr(elem)}")
+                print("\n", end="")
 
 
 def scan(args, engine):
@@ -135,7 +174,7 @@ def scan(args, engine):
             qb_client = auth_qbittorrent(
                 client_info.endpoint, args.username, args.password
             )
-            scan_torrents(qb_client, debug=args.debug)
+            scan_torrents(qb_client, engine, debug=args.debug)
         elif len(clients) == 0:
             raise ValueError(
                 f'Client with name "{args.name}" not found. '