From 15098f379e0e2012e229d275095afe618304567f Mon Sep 17 00:00:00 2001 From: Goldy <153996346+g0ldyy@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:01:42 +0100 Subject: [PATCH] feat: reverse order results --- comet/api/stream.py | 73 ++++++++++++++++---------------------- comet/templates/index.html | 9 +++-- comet/utils/general.py | 4 +++ comet/utils/models.py | 1 + 4 files changed, 43 insertions(+), 44 deletions(-) diff --git a/comet/api/stream.py b/comet/api/stream.py index 29b2f48..8cc0d25 100644 --- a/comet/api/stream.py +++ b/comet/api/stream.py @@ -197,51 +197,40 @@ async def stream(request: Request, b64config: str, type: str, id: str): ) balanced_hashes = get_balanced_hashes(all_sorted_ranked_files, config) - seen_hashes = set() - for hash, hash_data in all_sorted_ranked_files.items(): - if hash in seen_hashes: - continue - - for resolution, hash_list in balanced_hashes.items(): - if hash in hash_list: - data = hash_data["data"] - - the_stream = { - "name": f"[{debrid_extension}{debrid_emoji}] Comet {data['resolution']}", - "description": format_title(data, config), - "torrentTitle": ( - data["torrent_title"] - if "torrent_title" in data - else None - ), - "torrentSize": ( - data["torrent_size"] if "torrent_size" in data else None - ), - "behaviorHints": { - "filename": data["raw_title"], - "bingeGroup": "comet|" + hash, - }, - } - - if config["debridApiKey"] != "": - the_stream["url"] = ( - f"{request.url.scheme}://{request.url.netloc}/{b64config}/playback/{hash}/{data['index']}" - ) - else: - the_stream["infoHash"] = hash - - index = data["index"] - the_stream["fileIdx"] = ( - 1 if "|" in index else int(index) - ) # 1 because for Premiumize it's impossible to get the file index + for resolution in balanced_hashes: + for hash in balanced_hashes[resolution]: + data = all_sorted_ranked_files[hash]["data"] + + the_stream = { + "name": f"[{debrid_extension}{debrid_emoji}] Comet {data['resolution']}", + "description": format_title(data, config), + "torrentTitle": ( + data["torrent_title"] if "torrent_title" in data else None + ), + "torrentSize": ( + data["torrent_size"] if "torrent_size" in data else None + ), + "behaviorHints": { + "filename": data["raw_title"], + "bingeGroup": "comet|" + hash, + }, + } - the_stream["sources"] = trackers + if config["debridApiKey"] != "": + the_stream["url"] = ( + f"{request.url.scheme}://{request.url.netloc}/{b64config}/playback/{hash}/{data['index']}" + ) + else: + the_stream["infoHash"] = hash - results.append(the_stream) + index = data["index"] + the_stream["fileIdx"] = ( + 1 if "|" in index else int(index) + ) # 1 because for Premiumize it's impossible to get the file index - seen_hashes.add(hash) + the_stream["sources"] = trackers - break + results.append(the_stream) results_count = len(results) if results_count != 0: @@ -416,7 +405,7 @@ async def stream(request: Request, b64config: str, type: str, id: str): ranked_files.add(ranked_file) except Exception as e: - logger.info(f"Filtered: {e}") + logger.info(f"Filtered out: {e}") pass sorted_ranked_files = sort_torrents(ranked_files) diff --git a/comet/templates/index.html b/comet/templates/index.html index bd899d0..55717a2 100644 --- a/comet/templates/index.html +++ b/comet/templates/index.html @@ -559,6 +559,7 @@
+ Reverse Result Order
@@ -660,7 +661,8 @@ customElements.whenDefined("sl-select"), customElements.whenDefined("sl-input"), customElements.whenDefined("sl-option"), - customElements.whenDefined("sl-icon") + customElements.whenDefined("sl-icon"), + customElements.whenDefined("sl-checkbox") ]); const webConfig = {{webConfig|tojson}}; @@ -717,6 +719,7 @@ const maxResults = document.getElementById("maxResults").value; const maxResultsPerResolution = document.getElementById("maxResultsPerResolution").value; const maxSize = document.getElementById("maxSize").value; + const reverseResultOrder = document.getElementById("reverseResultOrder").checked; const resultFormat = Array.from(document.getElementById("resultFormat").selectedOptions).map(option => option.value); const debridService = document.getElementById("debridService").value; const debridApiKey = document.getElementById("debridApiKey").value; @@ -730,6 +733,7 @@ maxResults: parseInt(maxResults), maxResultsPerResolution: parseInt(maxResultsPerResolution), maxSize: parseFloat(maxSize * 1073741824), + reverseResultOrder: reverseResultOrder, resultFormat: selectedResultFormat, resolutions: selectedResolutions, languages: selectedLanguages, @@ -780,9 +784,10 @@ } function populateFormFromSettings(settings) { - console.log(settings.maxResultsPerResolution); if (settings.maxResults !== null) document.getElementById("maxResults").value = settings.maxResults; + if (settings.reverseResultOrder !== null) + document.getElementById("reverseResultOrder").checked = settings.reverseResultOrder; if (settings.maxResultsPerResolution !== null) document.getElementById("maxResultsPerResolution").value = settings.maxResultsPerResolution; if (settings.maxSize !== null) diff --git a/comet/utils/general.py b/comet/utils/general.py index 6c5191e..4315c4d 100644 --- a/comet/utils/general.py +++ b/comet/utils/general.py @@ -567,6 +567,10 @@ def get_balanced_hashes(hashes: dict, config: dict): hashes_by_resolution[resolution] = [] hashes_by_resolution[resolution].append(hash) + if config["reverseResultOrder"]: + for resolution in hashes_by_resolution: + hashes_by_resolution[resolution].reverse() + total_resolutions = len(hashes_by_resolution) if max_results == 0 and max_results_per_resolution == 0 or total_resolutions == 0: return hashes_by_resolution diff --git a/comet/utils/models.py b/comet/utils/models.py index 66d1364..4c76099 100644 --- a/comet/utils/models.py +++ b/comet/utils/models.py @@ -69,6 +69,7 @@ class ConfigModel(BaseModel): indexers: List[str] languages: Optional[List[str]] = ["All"] resolutions: Optional[List[str]] = ["All"] + reverseResultOrder: Optional[bool] = False resultFormat: Optional[List[str]] = ["All"] maxResults: Optional[int] = 0 maxResultsPerResolution: Optional[int] = 0