Skip to content

Instantly share code, notes, and snippets.

@Fooftilly
Created October 11, 2023 18:18
Show Gist options
  • Save Fooftilly/e9bb53f2db78c8d26bb805d8db5f94a5 to your computer and use it in GitHub Desktop.
Save Fooftilly/e9bb53f2db78c8d26bb805d8db5f94a5 to your computer and use it in GitHub Desktop.
Torrent ratio for Qbittorrent - Automatically update stats
#!/usr/bin/python
from PyQt5.QtCore import QSettings, QVariant, QTimer, QCoreApplication
import sys
import signal
from datetime import datetime
import os
def bytes_to_tebibytes(bytes_value):
# Convert bytes to tebibytes (TiB) with 3 decimal points
tib_value = bytes_value / (1024 ** 4)
return f'{tib_value:.3f} TiB'
def bytes_to_gibibytes(bytes_value):
# Convert bytes to gibibytes (GiB)
gib_value = bytes_value / (1024 ** 3)
return f'{gib_value:.2f} GiB'
def bytes_to_mebibytes(bytes_value):
# Convert bytes to mebibytes (MiB)
mib_value = bytes_value / (1024 ** 2)
return f'{mib_value:.2f} MiB'
def read_qbittorrent_data(file_path):
# Load the data from the file using QSettings
settings = QSettings(file_path, QSettings.IniFormat)
settings.beginGroup('Stats')
all_stats_variant = settings.value('AllStats')
settings.endGroup()
return all_stats_variant
def display_stats(file_path):
all_stats_variant = read_qbittorrent_data(file_path)
if all_stats_variant is not None:
all_time_download_tib = bytes_to_tebibytes(all_stats_variant['AlltimeDL'])
all_time_upload_tib = bytes_to_tebibytes(all_stats_variant['AlltimeUL'])
print(f'ALL-TIME DOWNLOAD: {all_time_download_tib}')
print(f'ALL-TIME UPLOAD: {all_time_upload_tib}')
if all_stats_variant['AlltimeDL'] == 0:
print('RATIO: ∞ (No data downloaded)')
else:
current_share_ratio = all_stats_variant['AlltimeUL'] / all_stats_variant['AlltimeDL']
# Calculate the desired share ratio as the next hundredth increment of the current ratio
desired_share_ratio = (int(current_share_ratio * 100) + 1) / 100.0
additional_upload_bytes = (desired_share_ratio * all_stats_variant['AlltimeDL']) - all_stats_variant['AlltimeUL']
additional_upload_mib = bytes_to_mebibytes(additional_upload_bytes)
additional_upload_gib = bytes_to_gibibytes(additional_upload_bytes)
print(f'RATIO: {current_share_ratio:.4f}')
print(f'REQUIRED UPLOAD TO INCREASE RATIO: {additional_upload_mib} ({additional_upload_gib})')
current_time = datetime.now().strftime("%H:%M:%S")
print(f'Last updated: {current_time}')
print(f'----------------------------')
else:
print('Failed to read the data.')
def main():
app = QCoreApplication(sys.argv) # Create a QCoreApplication
# Dynamically obtain the home directory path
file_path = os.path.expanduser('~/.config/qBittorrent/qBittorrent-data.conf')
# Display stats at script startup and store them as previous stats
prev_stats = read_qbittorrent_data(file_path)
display_stats(file_path)
def check_for_changes():
nonlocal prev_stats
current_stats = read_qbittorrent_data(file_path)
if current_stats != prev_stats:
display_stats(file_path)
prev_stats = current_stats # Update the stored previous stats
# Create a QTimer object to schedule the periodic checking of the file for changes
timer = QTimer()
timer.timeout.connect(check_for_changes) # Connect the timer timeout signal to the check_for_changes function
timer.start(5000) # Set the timer interval to 5000 milliseconds (5 seconds)
# Setup signal handler to stop the application on Ctrl+C
signal.signal(signal.SIGINT, lambda *args: app.quit())
return app.exec_() # Start the QCoreApplication event loop
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment