import os import sys from pathlib import Path import libtorrent sys.path.insert(0, os.path.dirname(os.path.dirname((os.path.abspath(__file__))))) # magic import-fixer from silverstream.bittorrent.database import TorrentDatabase, IndexStatus try: torrents_dir, database_filepath = map(Path, sys.argv[1:]) except (IndexError, ValueError): exit(f"Usage: {__file__} ") torrent_database = TorrentDatabase(filepath=database_filepath) num_torrents = num_skipped = num_songs = 0 for torrent in torrents_dir.iterdir(): num_torrents += 1 print() print("---") print("Adding", torrent) torrent_info = libtorrent.torrent_info(str(torrent)) info_hash = torrent_info.info_hash().to_bytes() torrent_name = torrent_info.name() print("Info hash:", info_hash.hex()) print("Torrent name:", torrent_name) torrent_database.add_peer(info_hash, name=torrent_name) if torrent_database.get_index_status(info_hash) == IndexStatus.Indexed: print("Torrent already indexed: skipping") num_skipped += 1 continue songs = [] files = torrent_info.files() for file_index in range(files.num_files()): file_path = Path(files.file_path(file_index)) if file_path.suffix in (".aac", ".flac", ".m4a", ".mp3", ".mpc", ".ogg", ".opus", ".wav", ".wma"): songs.append((file_index, " / ".join(file_path.parts))) num_songs += 1 if songs: torrent_database.add_songs(info_hash, songs) print("Songs:") for file_index, song_name in songs: print(song_name) torrent_database.set_index_status(info_hash, IndexStatus.Indexed) print("-----") print("Torrents:", num_torrents) print("Skipped:", num_skipped) print("Songs:", num_songs)