Skip to content

Commit

Permalink
Merge pull request #187 from davidemarcoli/feature/results-per-resolu…
Browse files Browse the repository at this point in the history
…tion

feat: add max results per resolution configuration option
  • Loading branch information
g0ldyy authored Nov 17, 2024
2 parents ba4e176 + 58669db commit b2ecc86
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 7 additions & 0 deletions comet/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,10 @@
<sl-input id="maxResults" type="number" min=0 value=0 label="Max Results" placeholder="Enter max results"></sl-input>
</div>

<div class="form-item">
<sl-input id="maxResultsPerResolution" type="number" min=0 value=0 label="Max Results Per Resolution" placeholder="Enter max results per resolution"></sl-input>
</div>

<div class="form-item">
<sl-input id="maxSize" type="number" min=0 value=0 label="Max Size (GB)" placeholder="Enter max size in gigabytes"></sl-input>
</div>
Expand Down Expand Up @@ -710,6 +714,7 @@
const languages = Array.from(document.getElementById("languages").selectedOptions).map(option => option.value);
const resolutions = Array.from(document.getElementById("resolutions").selectedOptions).map(option => option.value);
const maxResults = document.getElementById("maxResults").value;
const maxResultsPerResolution = document.getElementById("maxResultsPerResolution").value;
const maxSize = document.getElementById("maxSize").value;
const resultFormat = Array.from(document.getElementById("resultFormat").selectedOptions).map(option => option.value);
const debridService = document.getElementById("debridService").value;
Expand All @@ -722,6 +727,7 @@
return {
indexers: indexers,
maxResults: parseInt(maxResults),
maxResultsPerResolution: parseInt(maxResultsPerResolution),
maxSize: parseFloat(maxSize * 1073741824),
resultFormat: selectedResultFormat,
resolutions: selectedResolutions,
Expand Down Expand Up @@ -774,6 +780,7 @@

function populateFormFromSettings(settings) {
document.getElementById("maxResults").value = settings.maxResults;
document.getElementById("maxResultsPerResolution").value = settings.maxResultsPerResolution;
document.getElementById("maxSize").value = settings.maxSize / 1073741824; // Convert back from bytes to GB
document.getElementById("debridService").value = settings.debridService;
document.getElementById("debridApiKey").value = settings.debridApiKey;
Expand Down
7 changes: 5 additions & 2 deletions comet/utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ async def get_torrent_hash(session: aiohttp.ClientSession, torrent: tuple):

def get_balanced_hashes(hashes: dict, config: dict):
max_results = config["maxResults"]
max_results_per_resolution = config["maxResultsPerResolution"]

max_size = config["maxSize"]
config_resolutions = [resolution.lower() for resolution in config["resolutions"]]
Expand Down Expand Up @@ -565,15 +566,17 @@ def get_balanced_hashes(hashes: dict, config: dict):
hashes_by_resolution[resolution].append(hash)

total_resolutions = len(hashes_by_resolution)
if max_results == 0 or total_resolutions == 0:
if max_results == 0 and max_results_per_resolution == 0 or total_resolutions == 0:
return hashes_by_resolution

hashes_per_resolution = max_results // total_resolutions
hashes_per_resolution = max_results // total_resolutions if max_results > 0 else max_results_per_resolution
extra_hashes = max_results % total_resolutions

balanced_hashes = {}
for resolution, hash_list in hashes_by_resolution.items():
selected_count = hashes_per_resolution + (1 if extra_hashes > 0 else 0)
if max_results_per_resolution > 0:
selected_count = min(selected_count, max_results_per_resolution)
balanced_hashes[resolution] = hash_list[:selected_count]
if extra_hashes > 0:
extra_hashes -= 1
Expand Down
7 changes: 7 additions & 0 deletions comet/utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class ConfigModel(BaseModel):
resolutions: Optional[List[str]] = ["All"]
resultFormat: Optional[List[str]] = ["All"]
maxResults: Optional[int] = 0
maxResultsPerResolution: Optional[int] = 0
maxSize: Optional[float] = 0
debridService: str
debridApiKey: str
Expand All @@ -95,6 +96,12 @@ def check_max_results(cls, v):
if v < 0:
v = 0
return v

@field_validator("maxResultsPerResolution")
def check_max_results_per_resolution(cls, v):
if v < 0:
v = 0
return v

@field_validator("maxSize")
def check_max_size(cls, v):
Expand Down

0 comments on commit b2ecc86

Please sign in to comment.