Skip to content

Instantly share code, notes, and snippets.

@janlay
Last active October 8, 2022 03:22
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 janlay/c8d45acd45fb7e0ecfece577b7c52869 to your computer and use it in GitHub Desktop.
Save janlay/c8d45acd45fb7e0ecfece577b7c52869 to your computer and use it in GitHub Desktop.
jq Module for OpenSubtitles

This module picks the best subtitle from OpenSubtitles' search result. Created by @janlay

Input:

OpenSubtitles's search result

Arguments:

  • language: Required. Format should be recognized by OpenSubtitles like "en", "zh", "zh-CN"
  • group: Optional. Movie's release group name

Output:

It outputs the picked file_id if best subtitle matched, otherwise, nothing output.

Usage:

jq -n --argjson accepts_ssa 1 --argjson accepts_srt 1 --arg group EVO --arg language zh \
  'import "./opensubtitles" as OpenSubtitles; input | OpenSubtitles::pick_best'
  • You may want to change the imported file path.
  • For the halt usage, -n option is required.
# This module picks the best subtitle from OpenSubtitles' search result.
# Created by @janlay
#
# Input:
# OpenSubtitles's search result
#
# Arguments:
# - language: Required. Format should be recognized by OpenSubtitles like "en", "zh", "zh-CN"
# - group: Optional. Movie's release group name
#
# Output:
# It outputs the picked file_id if best subtitle matched, otherwise, nothing output.
#
# Usage:
# jq -n --argjson accepts_ssa 1 --argjson accepts_srt 1 --arg group EVO --arg language zh 'import "./opensubtitles" as OpenSubtitles; input | OpenSubtitles::pick_best'
# * You may want to change the imported file path.
# * For the `halt' usage, `-n' option is required.
#
def pick_best:
if .total_count == 0 then halt else .data end
# pre-select items
| map(
select(
(.attributes.language | startswith($language))
and
(.attributes.files | length == 1)
)
) as $list
| if any then . else halt end
# find ssa
| if $accepts_ssa == 1 then
map(
select(.attributes.files[0].file_name | endswith(".ssa") or endswith(".ass"))
)
else [] end
# fallback to srt if allowed
| if (length == 0 and $accepts_srt == 1) then
$list | map(
select(.attributes.files[0].file_name | endswith(".srt"))
)
else . end
| if any then . else halt end
| . as $subs
# pick the best
| if $group | length > 0 then
map(
select(.attributes.release | endswith($group))
)
| if any then . else $subs end
else . end
| sort_by(.id | tonumber)
| last.attributes.files[0].file_id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment