Skip to content

Commit

Permalink
feat: reverse order results
Browse files Browse the repository at this point in the history
  • Loading branch information
g0ldyy committed Nov 18, 2024
1 parent 01d16b4 commit 15098f3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 44 deletions.
73 changes: 31 additions & 42 deletions comet/api/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions comet/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@

<sl-details summary="Advanced Settings">
<div class="form-item">
<sl-checkbox id="reverseResultOrder" help-text="Reverse the order of the results for each resolution (useful for those who need small file sizes)">Reverse Result Order</sl-checkbox>
<sl-select id="resultFormat" multiple label="Result Format" placeholder="Select what to show in result title" hoist max-options-visible=10>
</sl-select>
</div>
Expand Down Expand Up @@ -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}};
Expand Down Expand Up @@ -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;
Expand All @@ -730,6 +733,7 @@
maxResults: parseInt(maxResults),
maxResultsPerResolution: parseInt(maxResultsPerResolution),
maxSize: parseFloat(maxSize * 1073741824),
reverseResultOrder: reverseResultOrder,
resultFormat: selectedResultFormat,
resolutions: selectedResolutions,
languages: selectedLanguages,
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions comet/utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions comet/utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 15098f3

Please sign in to comment.