Skip to content

Commit

Permalink
Add util for getting trakt ids
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Oct 8, 2024
1 parent a896503 commit 299efcf
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions local/bin/trakt-id-from-url
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 299efcf

Please sign in to comment.