aboutsummaryrefslogtreecommitdiff
path: root/bulk-audio/get
blob: b7791aa7a4260fde058640219b5d92d9629e02aa (plain)
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
#!/bin/sh

KANJI="$1"
KANA="$2"

if [ -z "$KANJI" -o -z "$KANA" ]; then
	cat << EOF
usage: $0 <kanji> <kana> > <output>

return value is 0 if <output> was succesfully written, 1 if the word could not
be found. this script searches languagepod101, languagepod101 (alt) and
jisho.org.
EOF
	exit 1
fi

get_languagepod101() {
  URL="https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=$KANJI&kana=$KANA"
  # 52288 is the content-length of the "the audio for this clip is currently
  # not available. it will be recorded and uploaded shortly. thank you for your
  # patience" message (404, but server sends 200 anyways)
  curl -X HEAD -iso - "$URL" | awk '/^Content-length:/ { exit $2 == 52288 }'
	[ $? -ne 0 ] && return 1
	curl -so - "$URL"
}

get_languagepod101_alt() {
	HTML="$(curl -s -X POST \
		"https://www.japanesepod101.com/learningcenter/reference/dictionary_post" \
		-H "Content-Type: application/x-www-form-urlencoded" \
		-d "post=dictionary_reference&match_type=exact&search_query=$KANJI&vulgar=true" \
	)"
	[ $? -ne 0 ] && return 1
	URL="$(echo "$HTML" | pup "audio source attr{src}" | head -n1)"
	[ -z "$URL" ] && return 1
	curl -so - "$URL"
}

get_jisho() {
	HTML="$(curl -s "https://jisho.org/search/$KANJI")"
	[ $? -ne 0 ] && return 1
	URL="$(echo "$HTML" | pup "audio[id=\"audio_$KANJI:$KANA\"] source attr{src}" | head -n1)"
	[ -z "$URL" ] && return 1
	URL="https:$URL"
	curl -so - "$URL"
}

# get_languagepod101_alt
# [ $? -eq 0 ] && exit 0
# 
# get_jisho
# [ $? -eq 0 ] && exit 0

get_languagepod101
[ $? -eq 0 ] && exit 0

# if none were succesful, delete output file and exit with error
rm -f "$OUTPUT"
exit 1