diff --git a/local/bin/trakt-id-from-url b/local/bin/trakt-id-from-url new file mode 100755 index 0000000..300016a --- /dev/null +++ b/local/bin/trakt-id-from-url @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail +if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace; fi + +TRAKT_CLIENT_ID="${TRAKT_CLIENT_ID:-$(secret TRAKT_CLIENT_ID)}" + +trakt_id() { + curl --silent --show-error --fail \ + --url "https://api.trakt.tv${1}" \ + --header "Content-Type: application/json" \ + --header "trakt-api-version: 2" \ + --header "trakt-api-key: ${TRAKT_CLIENT_ID}" | jq --raw-output '.ids.trakt' +} + +URL="${1}" +if [[ "${URL}" =~ https://trakt\.tv/shows/([^/]+)/seasons/([^/]+)/episodes/([^/]+) ]]; then + SHOW_SLUG="${BASH_REMATCH[1]}" + SEASON_NUMBER="${BASH_REMATCH[2]}" + EPISODE_NUMBER="${BASH_REMATCH[3]}" + trakt_id "/shows/${SHOW_SLUG}/seasons/${SEASON_NUMBER}/episodes/${EPISODE_NUMBER}" +elif [[ "${URL}" =~ https://trakt\.tv/shows/([^/]+) ]]; then + SLUG="${BASH_REMATCH[1]}" + trakt_id "/shows/${SLUG}" +elif [[ "${URL}" =~ https://trakt\.tv/movies/([^/]+) ]]; then + SLUG="${BASH_REMATCH[1]}" + trakt_id "/movies/${SLUG}" +else + echo "Unknown URL" >&2 + exit 1 +fi