Compare commits
1 Commits
7b42d55cc1
...
aitest
Author | SHA1 | Date | |
---|---|---|---|
998872cb4f |
@@ -1,2 +1,2 @@
|
|||||||
qbittorrent-api==2025.2.0
|
python-qbittorrent==0.4.3
|
||||||
SQLAlchemy==2.0.38
|
sqlalchemy==2.0.27
|
||||||
|
148
tarc/main.py
148
tarc/main.py
@@ -15,78 +15,63 @@ import uuid
|
|||||||
import argparse
|
import argparse
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
import qbittorrentapi
|
import qbittorrent
|
||||||
from sqlalchemy import create_engine, inspect
|
from sqlalchemy import create_engine, event
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import sessionmaker
|
||||||
from sqlalchemy.exc import DatabaseError
|
|
||||||
|
|
||||||
from .models import Base, SchemaVersion, Client
|
from .models import Base, Client
|
||||||
|
|
||||||
# SCHEMA format is YYYYMMDDX
|
# SCHEMA format is YYYYMMDDX
|
||||||
SCHEMA = 202503100
|
SCHEMA = 202410060
|
||||||
|
|
||||||
|
|
||||||
def init_db(engine):
|
def init_db(engine):
|
||||||
"""
|
"""
|
||||||
Initialize database
|
Initialize database
|
||||||
"""
|
"""
|
||||||
|
# Create all tables first
|
||||||
Base.metadata.create_all(engine)
|
Base.metadata.create_all(engine)
|
||||||
|
|
||||||
with Session(engine) as session:
|
# Set the schema version using SQLAlchemy primitives
|
||||||
if not session.query(SchemaVersion).first():
|
@event.listens_for(engine, 'connect')
|
||||||
now = datetime.now(timezone.utc)
|
def set_sqlite_pragma(dbapi_connection, connection_record):
|
||||||
version = SchemaVersion(version=SCHEMA, applied_at=now)
|
cursor = dbapi_connection.cursor()
|
||||||
session.add(version)
|
cursor.execute(f"PRAGMA user_version = {SCHEMA}")
|
||||||
session.commit()
|
cursor.close()
|
||||||
|
|
||||||
|
|
||||||
def get_schema_version(engine):
|
|
||||||
"""
|
|
||||||
Get current schema version from database
|
|
||||||
"""
|
|
||||||
with Session(engine) as session:
|
|
||||||
version = session.query(SchemaVersion).order_by(SchemaVersion.id.desc()).first()
|
|
||||||
return version.version if version else None
|
|
||||||
|
|
||||||
|
|
||||||
def list_tables(engine):
|
def list_tables(engine):
|
||||||
"""
|
"""
|
||||||
List all tables in database
|
List all tables in database
|
||||||
"""
|
"""
|
||||||
inspector = inspect(engine)
|
return Base.metadata.tables.keys()
|
||||||
return inspector.get_table_names()
|
|
||||||
|
|
||||||
|
|
||||||
def add_client(engine, name, endpoint, last_seen):
|
def add_client(session, name, endpoint, last_seen):
|
||||||
"""
|
"""
|
||||||
Add a new client endpoint to database
|
Add a new client endpoint to database
|
||||||
"""
|
"""
|
||||||
with Session(engine) as session:
|
new_client = Client(
|
||||||
client = Client(
|
uuid=str(uuid.uuid4()),
|
||||||
uuid=str(uuid.uuid4()), name=name, endpoint=endpoint, last_seen=last_seen
|
name=name,
|
||||||
|
endpoint=endpoint,
|
||||||
|
last_seen=last_seen
|
||||||
)
|
)
|
||||||
session.add(client)
|
session.add(new_client)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
|
||||||
def find_client(engine, endpoint):
|
def find_client(session, endpoint):
|
||||||
"""
|
"""
|
||||||
Find existing client
|
Find existing client
|
||||||
"""
|
"""
|
||||||
with Session(engine) as session:
|
clients = session.query(Client.id, Client.name, Client.uuid).filter(Client.endpoint == endpoint).all()
|
||||||
clients = (
|
|
||||||
session.query(Client.id, Client.name, Client.uuid)
|
|
||||||
.filter_by(endpoint=endpoint)
|
|
||||||
.all()
|
|
||||||
)
|
|
||||||
return clients
|
return clients
|
||||||
|
|
||||||
|
|
||||||
def list_clients(engine):
|
def list_clients(session):
|
||||||
"""
|
"""
|
||||||
List all stored clients
|
List all stored clients
|
||||||
"""
|
"""
|
||||||
with Session(engine) as session:
|
|
||||||
return session.query(Client).all()
|
return session.query(Client).all()
|
||||||
|
|
||||||
|
|
||||||
@@ -117,52 +102,64 @@ def main():
|
|||||||
|
|
||||||
if args.command == "scan":
|
if args.command == "scan":
|
||||||
if args.storage is None:
|
if args.storage is None:
|
||||||
storage_path = os.path.expanduser("~/.tarc.db")
|
STORAGE = os.path.expanduser("~/.tarch.db")
|
||||||
else:
|
else:
|
||||||
storage_path = args.storage
|
STORAGE = args.storage
|
||||||
|
|
||||||
try:
|
try:
|
||||||
engine = create_engine(f"sqlite:///{storage_path}")
|
engine = create_engine(f"sqlite:///{STORAGE}")
|
||||||
tables = list_tables(engine)
|
tables = list_tables(engine)
|
||||||
except DatabaseError as e:
|
Session = sessionmaker(bind=engine)
|
||||||
print(f'[ERROR]: Database Error "{storage_path}" ({str(e)})')
|
session = Session()
|
||||||
|
except Exception as e:
|
||||||
|
print(f'[ERROR]: Database Error "{STORAGE}" ({str(e)})')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
if len(tables) == 0:
|
||||||
if not tables:
|
print(f"[INFO]: Initializing database at {STORAGE}")
|
||||||
print(f"[INFO]: Initializing database at {storage_path}")
|
|
||||||
init_db(engine)
|
init_db(engine)
|
||||||
|
|
||||||
schema_found = get_schema_version(engine)
|
# Check schema version using SQLAlchemy primitives
|
||||||
if schema_found is None:
|
schema_found = None
|
||||||
print("[ERROR]: Could not determine schema version")
|
|
||||||
sys.exit(1)
|
@event.listens_for(engine, 'connect')
|
||||||
|
def get_sqlite_pragma(dbapi_connection, connection_record):
|
||||||
|
nonlocal schema_found
|
||||||
|
cursor = dbapi_connection.cursor()
|
||||||
|
cursor.execute("PRAGMA user_version")
|
||||||
|
schema_found = cursor.fetchone()[0]
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
# Force a connection to trigger the event
|
||||||
|
with engine.connect() as conn:
|
||||||
|
pass
|
||||||
|
|
||||||
if not SCHEMA == schema_found:
|
if not SCHEMA == schema_found:
|
||||||
print(f"[ERROR]: SCHEMA {schema_found}, expected {SCHEMA}")
|
print(f"[ERROR]: SCHEMA {schema_found}, expected {SCHEMA}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
if not args.directory is None:
|
||||||
if args.directory is not None:
|
|
||||||
print("[INFO]: --directory is not implemented")
|
print("[INFO]: --directory is not implemented")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif args.endpoint is not None:
|
elif not args.endpoint is None:
|
||||||
qb = qbittorrentapi.Client(host=args.endpoint,
|
qb = qbittorrent.Client(args.endpoint)
|
||||||
username=args.username, password=args.password)
|
if args.username and args.password:
|
||||||
try:
|
qb.login(args.username, args.password)
|
||||||
qb.auth_log_in()
|
if qb.qbittorrent_version is None:
|
||||||
except qbittorrentapi.LoginFailed as e:
|
print(f'[ERROR]: Couldn\'t find client version at "{args.endpoint}"')
|
||||||
print(f'[ERROR]: Login failed for endpoint "{args.endpoint}": {e}')
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
if not re.match(r"^v?\d+(\.\d+)*$", qb.app.version):
|
elif not re.match(r"^v?\d+(\.\d+)*$", qb.qbittorrent_version):
|
||||||
print(f'[ERROR]: Invalid version "{qb.app.version}" found at "{args.endpoint}"')
|
print(f'[ERROR]: Invalid version found at "{args.endpoint}"')
|
||||||
|
if args.debug:
|
||||||
|
print(f"[DEBUG]: {qb.qbittorrent_version}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
print(f'[INFO]: Found qBittorrent {qb.app.version} at "{args.endpoint}"')
|
print(
|
||||||
|
f'[INFO]: Found qbittorrent {qb.qbittorrent_version} at "{args.endpoint}"'
|
||||||
clients = find_client(engine, args.endpoint)
|
)
|
||||||
|
clients = find_client(session, args.endpoint)
|
||||||
if args.confirm_add:
|
if args.confirm_add:
|
||||||
if len(clients) == 0:
|
if len(clients) == 0:
|
||||||
if args.name is not None:
|
if not args.name is None:
|
||||||
now = datetime.now(timezone.utc)
|
now = datetime.now(timezone.utc)
|
||||||
add_client(engine, args.name, args.endpoint, now)
|
add_client(session, args.name, args.endpoint, now)
|
||||||
print(f"[INFO]: Added client {args.name} ({args.endpoint})")
|
print(f"[INFO]: Added client {args.name} ({args.endpoint})")
|
||||||
else:
|
else:
|
||||||
print("[ERROR]: Must specify --name for a new client")
|
print("[ERROR]: Must specify --name for a new client")
|
||||||
@@ -180,16 +177,17 @@ def main():
|
|||||||
print("[ERROR]: Use --confirm-add to add a new endpoint")
|
print("[ERROR]: Use --confirm-add to add a new endpoint")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
elif len(clients) == 1:
|
elif len(clients) == 1:
|
||||||
torrents = qb.torrents_info()
|
torrents = qb.torrents()
|
||||||
print(f"[INFO]: There are {len(torrents)} torrents\n")
|
print(f"[INFO]: There are {len(torrents)} torrents\n")
|
||||||
|
|
||||||
for torrent in torrents[:2]:
|
for torrent in torrents[:2]:
|
||||||
files = qb.torrents_files(torrent.hash)
|
files = qb.get_torrent_files(torrent["hash"])
|
||||||
trackers = qb.torrents_trackers(torrent.hash)
|
trackers = qb.get_torrent_trackers(torrent["hash"])
|
||||||
print(f"[name]: {torrent.name}")
|
print(f"[name]: {torrent['name']}")
|
||||||
print(f"[infohash_v1]: {torrent.hash}")
|
print(f"[infohash_v1]: {torrent['infohash_v1']}")
|
||||||
print(f"[content_path]: {torrent.content_path}")
|
print(f"[content_path]: {torrent['content_path']}")
|
||||||
print(f"[magnet_uri]: {torrent.magnet_uri[:80]}")
|
print(f"[magent_uri]: {torrent['magnet_uri'][0:80]}")
|
||||||
print(f"[completed_on]: {torrent.completed}\n")
|
print(f"[completed_on]: {torrent['completed']}")
|
||||||
print(f"[trackers]: {len(trackers)}")
|
print(f"[trackers]: {len(trackers)}")
|
||||||
print(f"[file_count]: {len(files)}\n")
|
print(f"[file_count]: {len(files)}\n")
|
||||||
if args.debug:
|
if args.debug:
|
||||||
|
127
tarc/models.py
127
tarc/models.py
@@ -1,106 +1,109 @@
|
|||||||
"""SQLAlchemy models for the tarc database."""
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Database models for Torrent Archiver
|
||||||
|
"""
|
||||||
|
|
||||||
from sqlalchemy import (
|
from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, UniqueConstraint
|
||||||
Column,
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
Integer,
|
from sqlalchemy.orm import relationship
|
||||||
String,
|
|
||||||
DateTime,
|
|
||||||
Boolean,
|
|
||||||
ForeignKey,
|
|
||||||
UniqueConstraint,
|
|
||||||
)
|
|
||||||
from sqlalchemy.orm import declarative_base
|
|
||||||
|
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
|
||||||
|
class Client(Base):
|
||||||
|
__tablename__ = 'clients'
|
||||||
|
|
||||||
class SchemaVersion(Base): # pylint: disable=too-few-public-methods
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
"""Database schema version tracking."""
|
|
||||||
|
|
||||||
__tablename__ = "schema_version"
|
|
||||||
id = Column(Integer, primary_key=True)
|
|
||||||
version = Column(Integer, nullable=False)
|
|
||||||
applied_at = Column(DateTime, nullable=False)
|
|
||||||
|
|
||||||
|
|
||||||
class Client(Base): # pylint: disable=too-few-public-methods
|
|
||||||
"""BitTorrent client instance."""
|
|
||||||
|
|
||||||
__tablename__ = "clients"
|
|
||||||
id = Column(Integer, primary_key=True)
|
|
||||||
name = Column(String, nullable=False, unique=True)
|
name = Column(String, nullable=False, unique=True)
|
||||||
uuid = Column(String, nullable=False, unique=True)
|
uuid = Column(String, nullable=False, unique=True)
|
||||||
endpoint = Column(String, nullable=False)
|
endpoint = Column(String, nullable=False)
|
||||||
last_seen = Column(DateTime, nullable=False)
|
last_seen = Column(DateTime, nullable=False)
|
||||||
|
|
||||||
|
torrent_clients = relationship("TorrentClient", back_populates="client")
|
||||||
|
torrent_trackers = relationship("TorrentTracker", back_populates="client")
|
||||||
|
torrent_files = relationship("TorrentFile", back_populates="client")
|
||||||
|
|
||||||
class Torrent(Base): # pylint: disable=too-few-public-methods
|
|
||||||
"""BitTorrent metadata."""
|
|
||||||
|
|
||||||
__tablename__ = "torrents"
|
class Torrent(Base):
|
||||||
id = Column(Integer, primary_key=True)
|
__tablename__ = 'torrents'
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
info_hash_v1 = Column(String, nullable=False, unique=True)
|
info_hash_v1 = Column(String, nullable=False, unique=True)
|
||||||
info_hash_v2 = Column(String, unique=True)
|
info_hash_v2 = Column(String, unique=True)
|
||||||
file_count = Column(Integer, nullable=False)
|
file_count = Column(Integer, nullable=False)
|
||||||
completed_on = Column(DateTime, nullable=False)
|
completed_on = Column(DateTime, nullable=False)
|
||||||
|
|
||||||
|
torrent_clients = relationship("TorrentClient", back_populates="torrent")
|
||||||
|
torrent_trackers = relationship("TorrentTracker", back_populates="torrent")
|
||||||
|
torrent_files = relationship("TorrentFile", back_populates="torrent")
|
||||||
|
|
||||||
class TorrentClient(Base): # pylint: disable=too-few-public-methods
|
|
||||||
"""Association between torrents and clients."""
|
|
||||||
|
|
||||||
__tablename__ = "torrent_clients"
|
class TorrentClient(Base):
|
||||||
id = Column(Integer, primary_key=True)
|
__tablename__ = 'torrent_clients'
|
||||||
torrent_id = Column(Integer, ForeignKey("torrents.id"), nullable=False)
|
|
||||||
client_id = Column(Integer, ForeignKey("clients.id"), nullable=False)
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
torrent_id = Column(Integer, ForeignKey('torrents.id'), nullable=False)
|
||||||
|
client_id = Column(Integer, ForeignKey('clients.id'), nullable=False)
|
||||||
name = Column(String, nullable=False)
|
name = Column(String, nullable=False)
|
||||||
content_path = Column(String, nullable=False)
|
content_path = Column(String, nullable=False)
|
||||||
last_seen = Column(DateTime, nullable=False)
|
last_seen = Column(DateTime, nullable=False)
|
||||||
__table_args__ = (UniqueConstraint("torrent_id", "client_id"),)
|
|
||||||
|
torrent = relationship("Torrent", back_populates="torrent_clients")
|
||||||
|
client = relationship("Client", back_populates="torrent_clients")
|
||||||
|
|
||||||
|
__table_args__ = (UniqueConstraint('torrent_id', 'client_id'),)
|
||||||
|
|
||||||
|
|
||||||
class Tracker(Base): # pylint: disable=too-few-public-methods
|
class Tracker(Base):
|
||||||
"""BitTorrent tracker information."""
|
__tablename__ = 'trackers'
|
||||||
|
|
||||||
__tablename__ = "trackers"
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
id = Column(Integer, primary_key=True)
|
|
||||||
url = Column(String, nullable=False, unique=True)
|
url = Column(String, nullable=False, unique=True)
|
||||||
last_seen = Column(DateTime, nullable=False)
|
last_seen = Column(DateTime, nullable=False)
|
||||||
|
|
||||||
|
torrent_trackers = relationship("TorrentTracker", back_populates="tracker")
|
||||||
|
|
||||||
class TorrentTracker(Base): # pylint: disable=too-few-public-methods
|
|
||||||
"""Association between torrents and trackers."""
|
|
||||||
|
|
||||||
__tablename__ = "torrent_trackers"
|
class TorrentTracker(Base):
|
||||||
id = Column(Integer, primary_key=True)
|
__tablename__ = 'torrent_trackers'
|
||||||
client_id = Column(Integer, ForeignKey("clients.id"), nullable=False)
|
|
||||||
torrent_id = Column(Integer, ForeignKey("torrents.id"), nullable=False)
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
tracker_id = Column(Integer, ForeignKey("trackers.id"), nullable=False)
|
client_id = Column(Integer, ForeignKey('clients.id'), nullable=False)
|
||||||
|
torrent_id = Column(Integer, ForeignKey('torrents.id'), nullable=False)
|
||||||
|
tracker_id = Column(Integer, ForeignKey('trackers.id'), nullable=False)
|
||||||
last_seen = Column(DateTime, nullable=False)
|
last_seen = Column(DateTime, nullable=False)
|
||||||
__table_args__ = (UniqueConstraint("client_id", "torrent_id", "tracker_id"),)
|
|
||||||
|
client = relationship("Client", back_populates="torrent_trackers")
|
||||||
|
torrent = relationship("Torrent", back_populates="torrent_trackers")
|
||||||
|
tracker = relationship("Tracker", back_populates="torrent_trackers")
|
||||||
|
|
||||||
|
__table_args__ = (UniqueConstraint('client_id', 'torrent_id', 'tracker_id'),)
|
||||||
|
|
||||||
|
|
||||||
class File(Base): # pylint: disable=too-few-public-methods
|
class File(Base):
|
||||||
"""File metadata and hashes."""
|
__tablename__ = 'files'
|
||||||
|
|
||||||
__tablename__ = "files"
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
id = Column(Integer, primary_key=True)
|
|
||||||
size = Column(Integer, nullable=False)
|
size = Column(Integer, nullable=False)
|
||||||
oshash = Column(String, nullable=False, unique=True)
|
oshash = Column(String, nullable=False, unique=True)
|
||||||
hash = Column(String, unique=True)
|
hash = Column(String, unique=True)
|
||||||
|
|
||||||
|
torrent_files = relationship("TorrentFile", back_populates="file")
|
||||||
|
|
||||||
class TorrentFile(Base): # pylint: disable=too-few-public-methods
|
|
||||||
"""Association between torrents and files."""
|
|
||||||
|
|
||||||
__tablename__ = "torrent_files"
|
class TorrentFile(Base):
|
||||||
id = Column(Integer, primary_key=True)
|
__tablename__ = 'torrent_files'
|
||||||
file_id = Column(Integer, ForeignKey("files.id"), nullable=False)
|
|
||||||
torrent_id = Column(Integer, ForeignKey("torrents.id"), nullable=False)
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
client_id = Column(Integer, ForeignKey("clients.id"), nullable=False)
|
file_id = Column(Integer, ForeignKey('files.id'), nullable=False)
|
||||||
|
torrent_id = Column(Integer, ForeignKey('torrents.id'), nullable=False)
|
||||||
|
client_id = Column(Integer, ForeignKey('clients.id'), nullable=False)
|
||||||
file_index = Column(Integer, nullable=False)
|
file_index = Column(Integer, nullable=False)
|
||||||
file_path = Column(String, nullable=False)
|
file_path = Column(String, nullable=False)
|
||||||
is_downloaded = Column(Boolean, nullable=False)
|
is_downloaded = Column(Boolean, nullable=False)
|
||||||
last_checked = Column(DateTime, nullable=False)
|
last_checked = Column(DateTime, nullable=False)
|
||||||
__table_args__ = (
|
|
||||||
UniqueConstraint("file_id", "torrent_id", "client_id", "file_index"),
|
file = relationship("File", back_populates="torrent_files")
|
||||||
)
|
torrent = relationship("Torrent", back_populates="torrent_files")
|
||||||
|
client = relationship("Client", back_populates="torrent_files")
|
||||||
|
|
||||||
|
__table_args__ = (UniqueConstraint('file_id', 'torrent_id', 'client_id', 'file_index'),)
|
Reference in New Issue
Block a user