Skip to content

Instantly share code, notes, and snippets.

@onedr0p
Created August 21, 2023 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onedr0p/76f968ce43910c18309b47098dd24032 to your computer and use it in GitHub Desktop.
Save onedr0p/76f968ce43910c18309b47098dd24032 to your computer and use it in GitHub Desktop.
import os
import qbittorrentapi
torrent_client = qbittorrentapi.Client(host='https://qb.devbu.io')
download_directory = '/eros/Media/Downloads/qbittorrent/complete'
def get_qbittorrent_files():
torrent_list = torrent_client.torrents.info()
download_directory_list = download_directory.split(os.sep)
qbittorrent_files = set()
for torrent in torrent_list:
torrent_files = torrent['content_path'].replace('/media', '/eros/Media') # replace container path with nas path
if os.path.isfile(torrent_files):
torrent_path_list = torrent_files.split(os.sep)
merged_list = [value for value in torrent_path_list if value not in download_directory_list]
if len(merged_list) > 2:
torrent_files = os.path.dirname(torrent_files)
qbittorrent_files.add(torrent_files)
if os.path.isdir(torrent_files):
qbittorrent_files.add(torrent_files)
return qbittorrent_files
def delete_extra_files(qbittorrent_files, download_directory):
folders = [folder for folder in os.listdir(download_directory) if os.path.isdir(os.path.join(download_directory, folder))]
for folder in folders:
folder_path = os.path.join(download_directory, folder)
contents = os.listdir(folder_path)
print(f"Contents of '{folder_path}':")
for item in contents:
item_path = os.path.join(folder_path, item)
if item_path not in qbittorrent_files:
print(f"delete: {item_path}")
def main():
try:
torrent_client.auth_log_in()
qbittorrent_files = get_qbittorrent_files()
delete_extra_files(qbittorrent_files, download_directory)
except qbittorrentapi.LoginFailed as e:
print("Failed to log in to qBittorrent.")
except Exception as e:
print(f"An error occurred: {str(e)}")
finally:
torrent_client.auth_log_out()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment