95 lines
2.1 KiB
Bash
95 lines
2.1 KiB
Bash
#!/bin/bash
|
|
|
|
set -x
|
|
|
|
tmdbid() {
|
|
local type q url
|
|
type="$1"
|
|
shift
|
|
|
|
q=$(printf '%s' "$*" | jq -sRr @uri)
|
|
echo "tmdbid: type=$type encoded query: $q" >&2
|
|
|
|
if [ "$type" = "tv" ]; then
|
|
url="https://www.themoviedb.org/search/tv?query=$q"
|
|
else
|
|
url="https://www.themoviedb.org/search/movie?query=$q"
|
|
fi
|
|
echo "tmdbid: url: $url" >&2
|
|
|
|
local result
|
|
result=$(curl -sL -A 'Mozilla/5.0' "$url" \
|
|
| grep -oE '/(movie|tv)/[0-9]+' \
|
|
| head -1 \
|
|
| cut -d/ -f3)
|
|
echo "tmdbid: result: $result" >&2
|
|
echo "$result"
|
|
}
|
|
|
|
clean_title() {
|
|
local s="$1"
|
|
echo "clean_title: input: $s" >&2
|
|
|
|
s=$(echo "$s" | sed -E 's/ *\([^)]*[0-9]{4}[^)]*\)//')
|
|
echo "clean_title: after year removal: $s" >&2
|
|
|
|
s="${s//ä/ae}"; s="${s//ö/oe}"; s="${s//ü/ue}"
|
|
s="${s//Ä/Ae}"; s="${s//Ö/Oe}"; s="${s//Ü/Ue}"
|
|
echo "clean_title: after umlauts: $s" >&2
|
|
|
|
s=$(echo "$s" | tr -d '0-9')
|
|
echo "clean_title: after number removal: $s" >&2
|
|
|
|
s=$(echo "$s" | tr -cd '[:alpha:] [:space:]')
|
|
echo "clean_title: after special char removal: $s" >&2
|
|
|
|
s=$(echo "$s" | tr -s ' ')
|
|
echo "clean_title: final: $s" >&2
|
|
|
|
echo "$s"
|
|
}
|
|
|
|
ws="${1:-3}"
|
|
|
|
title=$(hyprctl clients -j | jq ".[] | select(.workspace.id == $ws)" | grep "The Movie Database" | sed 's/ — The Movie Database.*//' | sed 's/ "title": "//' | sed 's/"$//')
|
|
echo "title: $title" >&2
|
|
|
|
search_title=$(echo "$title" | sed -E 's/ *\([^)]*\) *//g')
|
|
echo "search_title: $search_title" >&2
|
|
|
|
if echo "$title" | grep -qi "TV [Ss]eries"; then
|
|
type="tv"
|
|
else
|
|
type="movie"
|
|
fi
|
|
echo "detected type: $type" >&2
|
|
|
|
tmdbid=$(tmdbid "$type" "$search_title")
|
|
echo "tmdbid: $tmdbid" >&2
|
|
|
|
if [ -z "$tmdbid" ]; then
|
|
if [ "$type" = "tv" ]; then
|
|
echo "retrying as movie" >&2
|
|
tmdbid=$(tmdbid "movie" "$search_title")
|
|
else
|
|
echo "retrying as tv" >&2
|
|
tmdbid=$(tmdbid "tv" "$search_title")
|
|
fi
|
|
echo "tmdbid (retry): $tmdbid" >&2
|
|
fi
|
|
|
|
if [[ $title =~ ([0-9]{4}) ]]; then
|
|
year="${BASH_REMATCH[1]}"
|
|
else
|
|
year=""
|
|
fi
|
|
echo "year: $year" >&2
|
|
|
|
clean=$(clean_title "$title")
|
|
echo "clean: $clean" >&2
|
|
|
|
result="$clean ($year) [tmdb-$tmdbid]"
|
|
echo "final wtype input: $result" >&2
|
|
|
|
wtype "$result"
|