-
Notifications
You must be signed in to change notification settings - Fork 1
/
tvdb.sh
executable file
·399 lines (362 loc) · 12.5 KB
/
tvdb.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/env bash
#
# Fetch TV series infomation from tvdb
#
#/ Usage:
#/ ./tvdb.sh [-c|-s|-y <year_range>|-f|-r|-d <date>] <search_text>
#/
#/ Options:
#/ -c Filter series status equals to continuing
#/ -s Show series only, without episodes list
#/ -y <year_range> Filter series first aired in the range of years, like: 2000-2016
#/ -f Filter episodes aired in the future
#/ -d <date> Filter episodes aired after the date, format like: 1999-12-20
#/ -d option overrules -f
#/ -r Show IMDb rating and votes per episode
#/ -h | --help Display this help message
#/
#/ Examples:
#/ \e[32m- Show `One-Punch Man` episodes list:\e[0m
#/ ~$ ./tvdb.sh one punch man
#/
#/ \e[32m- Show `One-Punch Man` series infomation only:\e[0m
#/ ~$ ./tvdb.sh \e[33m-s\e[0m one punch man
#/
#/ \e[32m- Show `One-Punch Man` episodes list with IMDb rating:\e[0m
#/ ~$ ./tvdb.sh \e[33m-r\e[0m one punch man
#/
#/ \e[32m- Show `One-Punch` Man episodes list aired in the future:\e[0m
#/ ~$ ./tvdb.sh \e[33m-f\e[0m one punch man
#/
#/ \e[32m- Show `One-Punch Man` episodes list aired after 2019-06-20:\e[0m
#/ ~$ ./tvdb.sh \e[33m-d 2019-06-20\e[0m one punch man
#/
#/ \e[32m- Show `Friends` episodes list, the series first aired in 1994:\e[0m
#/ ~$ ./tvdb.sh \e[33m-y 1994-1995\e[0m friends
#/ ...
#/ or
#/ ~$ ./tvdb.sh \e[33m-y 1994\e[0m friends
#/ ...
#/
#/ \e[32m- Show `Game of Thrones` series which is still continuing:\e[0m
#/ ~$ ./tvdb.sh \e[33m-c\e[0m game of thrones
set -e
set -u
usage() {
# Display usage message
printf "%b\n" "$(grep '^#/' "$0" | cut -c4-)" && exit 0
}
set_var() {
# Declare variables used in script
expr "$*" : ".*--help" > /dev/null && usage
while getopts ":hcfrsd:y:" opt; do
case $opt in
s)
_SHOW_SERIES_ONLY=false
;;
y)
_YEAR_RANGE_FIRSTAIRED="$OPTARG"
_MIN_YEAR_FIRSTAIRED=${_YEAR_RANGE_FIRSTAIRED%%-*}
_MAX_YEAR_FIRSTAIRED=${_YEAR_RANGE_FIRSTAIRED#*-}
check_firstaired_year_range "$_MIN_YEAR_FIRSTAIRED" "$_MAX_YEAR_FIRSTAIRED"
;;
r)
_SHOW_RATING=true
;;
d)
_DATE_AIRED="$OPTARG"
;;
f)
_FUTURE_AIRED=true
;;
c)
_CONTINUING_AIRED=true
;;
h)
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
esac
done
shift $((OPTIND-1))
_SEARCH_TEXT=$( echo "$*" | sed -E 's/ /%20/g')
_HOST="https://api.thetvdb.com"
_IMDB_URL="https://www.imdb.com/title"
_TOKEN_FILE="/tmp/.tvdb.token"
_TMP_FILE_SERIES_OVERVIEW="/tmp/.tvdb.tmp.series.overview"
_TMP_FILE_SERIES="/tmp/.tvdb.tmp.series"
_TMP_FILE_EPISODES="/tmp/.tvdb.tmp.episodes"
_TOKEN=""
_CURL=$(command -v curl) || command_not_found "curl"
_JQ=$(command -v jq) || command_not_found "jq"
true > $_TMP_FILE_SERIES_OVERVIEW
true > $_TMP_FILE_SERIES
true > $_TMP_FILE_EPISODES
}
set_api() {
# Declare vairables according to tvdb APIs
_API_LOGIN="$_HOST/login"
_API_REFRESH_TOKEN="$_HOST/refresh_token"
_API_SEARCH_SERIES="$_HOST/search/series"
_API_SERIES="$_HOST/series/{id}"
_API_SERIES_EPISODES="$_HOST/series/{id}/episodes"
}
command_not_found() {
# Show command not found message
# $1: command name
printf "%b\n" '\033[31m'"$1"'\033[0m command not found!' && exit 1
}
check_firstaired_year_range() {
# Check if year range is valid
# $1: min. year
# $2: max. year
if [[ "$1" -gt "$2" ]]; then
echo "Invalid year range: -y <year_range>!" && usage
fi
}
check_var() {
# Check _SEARCH_TEXT, TVDB_API_KEY, TVDB_USER_KEY and TVDB_USER_NAME
if [[ -z "${_SEARCH_TEXT:-}" ]]; then
echo 'No search text!'
usage
fi
if [[ -z "${TVDB_API_KEY:-}" ]]; then
echo 'API key is not set!'
echo ' ~$ export TVDB_API_KEY="<apikey>"'
usage
fi
if [[ -z "${TVDB_USER_KEY:-}" ]]; then
echo 'User key is not set!'
echo ' ~$ export TVDB_USER_KEY="<userkey>"'
usage
fi
if [[ -z "${TVDB_USER_NAME:-}" ]]; then
echo 'User name is not set!'
echo ' ~$ export TVDB_USER_NAME="<username>"'
usage
fi
}
get_token_from_result() {
# Return token from $1 data
if [[ "$1" == *"{\"token"* ]]; then
echo "$1" | $_JQ -r .token | tee "$_TOKEN_FILE"
else
echo "$1" >&2 && exit 1
fi
}
get_series_id_from_result() {
# Return series id from $1 data
if [[ "$1" == *"{\"data"* ]]; then
echo "$1" | tee "$_TMP_FILE_SERIES_OVERVIEW" | $_JQ -r '.data | .[].id'
else
echo "$1" >&2 && exit 1
fi
}
get_episodes_from_response() {
# Return data from $1 response
# $2: tmp file
if [[ "$1" == *"\"data\""* ]]; then
if [[ -s "$2" ]]; then
currentEpisodes=$($_JQ -r '.' < "$2")
newEpisodes=$(echo "$1" | $_JQ -r '.data')
echo "${currentEpisodes}${newEpisodes}" | sed -E 's/\]\[/,/' > "$2"
else
echo "$1" | $_JQ -r '.data' > "$2"
fi
else
echo "$1" >&2
fi
}
get_max_page_from_response() {
# Return max page of responses
if [[ "$1" == *"\"links\""* ]]; then
echo "$1" | $_JQ -r '.links.last'
else
echo "$1" >&2
fi
}
get_series_id() {
# Call series search API to get series id(s)
result=$($_CURL -sSX GET \
--header 'Accept: application/json' \
--header 'Authorization: Bearer '"$_TOKEN" "$_API_SEARCH_SERIES?name=$_SEARCH_TEXT")
get_series_id_from_result "$result"
}
get_series() {
# Call series API to get series data
# $1: series id
url=$(echo $_API_SERIES | sed -E 's/\{id\}/'"$id"'/')
$_CURL -sSX GET \
--header 'Accept: application/json' \
--header 'Authorization: Bearer '"$_TOKEN" "$url" > "$_TMP_FILE_SERIES"
}
get_episodes() {
# Call episodes API to get episodes data
# $1: series id
url=$(echo $_API_SERIES_EPISODES | sed -E 's/\{id\}/'"$id"'/')
result=$($_CURL -sSX GET \
--header 'Accept: application/json' \
--header 'Authorization: Bearer '"$_TOKEN" "$url")
maxpage=$(get_max_page_from_response "$result")
for (( i = 0; i < maxpage; i++ )); do
result=$($_CURL -sSX GET \
--header 'Accept: application/json' \
--header 'Authorization: Bearer '"$_TOKEN" "$url?page=$((i+1))")
get_episodes_from_response "$result" "$_TMP_FILE_EPISODES"
done
}
get_series_status() {
# Return series status from $2 data: Ended, Continuing...
# $1: series id
# $2: data file
$_JQ -r '.data | .[] | select(.id==($id | tonumber)) | .status' --arg id "$1" < "$2"
}
get_series_firstaired_year() {
# Return year of a series first aired
# $1: series id
# $2: data file
local date
date=$($_JQ -r '.data | .[] | select(.id==($id | tonumber)) | .firstAired' --arg id "$1" < "$2")
if [[ -z "$date" ]]; then
echo "0"
else
date -d"$date" +%Y
fi
}
get_imdb_id_from_file() {
# Return imdb id from $1
$_JQ -r '.[] | select(.airedSeason!=0 and .firstAired>=$date) | .imdbId' --arg date "$(get_search_date)" < "$1"
}
get_aired_season_from_file() {
# Return airedSeason from $1
# $2: imdb id
$_JQ -r '.[] | select(.imdbId=="'"$2"'") | .airedSeason' < "$1"
}
get_imdb_rating() {
# Get IMDb rating and inject imdbRating field into $1
# $1: episodes data
# $2: series data
local imdbSeriesId seasonNb previousSeasonNb ratingData rating
sed -i 's/imdbId.*,/& "imdbRating": "",/' "$1"
imdbSeriesId=$($_JQ -r '.data.imdbId' < "$2")
previousSeasonNb=""
for id in $(get_imdb_id_from_file "$1"); do
seasonNb=$(get_aired_season_from_file "$1" "$id")
if [[ "$seasonNb" != "$previousSeasonNb" ]]; then
ratingData=$($_CURL -sS "$_IMDB_URL/$imdbSeriesId/episodes?season=$seasonNb" \
| grep -B 1 'strong><a href="/title\|star__total' \
| grep 'href\|-rating' \
| sed -E 's/<strong.*title\///;s/\/"//;s/<span.*">//;s/<\/span>//;s/[[:space:]]//g' \
| awk '{if ($1 ~ "tt") {printf "\n%s ", $1}else{printf "%s", $1}}END{print ""}')
previousSeasonNb="$seasonNb"
fi
rating=$(echo "$ratingData" | grep "$id" | awk '{print $2}')
if [[ "$rating" ]]; then
sed -i "s/imdbId\": \"$id\", \"imdbRating\": \"\"/imdbId\": \"$id\", \"imdbRating\": \"$rating\"/" "$1"
fi
done
}
get_search_date() {
# Return search date accordingly
local date
date="0000-00-00"
[[ "${_FUTURE_AIRED:-}" == true ]] && date=$(date +"%Y-%m-%d")
[[ "${_DATE_AIRED:-}" ]] && date="$_DATE_AIRED"
echo "$date"
}
fetch_token() {
# Fetch token from tvdb API
if [[ ! -f "$_TOKEN_FILE" ]]; then
_TOKEN=$(login)
else
time_yesterday=$(date --date="yesterday" +%s)
time_file_modified=$(date +%s -r "$_TOKEN_FILE")
_TOKEN=$(cat "$_TOKEN_FILE")
if [[ "$time_yesterday" -ge "$time_file_modified" ]]; then
_TOKEN=$(refresh_token) || true
if [[ "$_TOKEN" == "" ]]; then
_TOKEN=$(login)
fi
fi
fi
}
login() {
# Call login API and save token
result=$($_CURL -sSX POST \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer '"$TVDB_API_KEY" \
-d '{
"apikey": "'"$TVDB_API_KEY"'",
"userkey": "'"$TVDB_USER_KEY"'",
"username": "'"$TVDB_USER_NAME"'"
}' "$_API_LOGIN")
get_token_from_result "$result"
}
refresh_token() {
# Call refresh token API
result=$($_CURL -sSX GET \
--header 'Accept: application/json' \
--header 'Authorization: Bearer '"$_TOKEN" "$_API_REFRESH_TOKEN")
get_token_from_result "$result"
}
print_series_info() {
# Print out series info
# $1: series id
# $2: data file
echo -e "$($_JQ -r '.data | .[] | select(.id==($id | tonumber)) | "\\033[1m\\033[33m" + .seriesName + "\\033[0m\\033[0m", "\\033[1mFirst Aired:\\033[0m+" + .firstAired, "\\033[1mStatus:\\033[0m+" + .status, "\\033[1mOverview:\\033[0m+" + .overview, "+"' --arg id "$1" < "$2" | column -t -s"+")"
}
print_episodes_info() {
# Print out series info
# $1: episodes file
# $2: series file
if [[ "${2:-}" ]]; then
get_imdb_rating "$1" "$2"
printf "%b\n" "$($_JQ -r -s '.[] | sort_by(.firstAired) | .[] | select(.airedSeason!=0 and .firstAired>=$date) | "\(.firstAired)+S\(.airedSeason)E\(.airedEpisodeNumber)+\\e[32m\(.episodeName)\\e[0m+\\e[33m\(.imdbRating)\\e[0m"' --arg date "$(get_search_date)"< "$1" | column -t -s "+")"
else
printf "%b\n" "$($_JQ -r -s '.[] | sort_by(.firstAired) | .[] | select(.airedSeason!=0 and .firstAired>=$date) | "\(.firstAired)+S\(.airedSeason)E\(.airedEpisodeNumber)+\\e[32m\(.episodeName)\\e[0m"' --arg date "$(get_search_date)"< "$1" | column -t -s"+")"
fi
}
search_tv_series() {
# Show serach results
for id in $(get_series_id);do
true > $_TMP_FILE_EPISODES
togglePrint=true
if [[ "${_YEAR_RANGE_FIRSTAIRED:-}" ]]; then
year=$(get_series_firstaired_year "$id" "$_TMP_FILE_SERIES_OVERVIEW")
if [[ "$year" -lt "$_MIN_YEAR_FIRSTAIRED" || "$year" -gt "$_MAX_YEAR_FIRSTAIRED" ]]; then
togglePrint=false
fi
fi
if [[ "${_CONTINUING_AIRED:-}" == true ]]; then
if [[ $(get_series_status "$id" "$_TMP_FILE_SERIES_OVERVIEW") != "Continuing" ]]; then
togglePrint=false
fi
fi
if [[ "$togglePrint" == true ]]; then
echo ""
print_series_info "$id" "$_TMP_FILE_SERIES_OVERVIEW"
if [[ -z "${_SHOW_SERIES_ONLY:-}" ]]; then
get_episodes "$id"
if [[ "${_SHOW_RATING:-}" ]]; then
get_series "$id" && print_episodes_info "$_TMP_FILE_EPISODES" "$_TMP_FILE_SERIES"
else
print_episodes_info "$_TMP_FILE_EPISODES"
fi
fi
fi
done
}
main() {
set_var "$@"
check_var
set_api
fetch_token
search_tv_series
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi