aboutsummaryrefslogtreecommitdiff
path: root/src/x_command.sh
blob: a4eb1e38199a394575a54894cafbe4befc620cdb (plain) (blame)
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
# Default to anime, permit manga
if [[ "${args[--manga]}" = 1 ]]; then
	FORMAT="manga"
	TYPE="manga"
	FILTER="format"
elif [[ "${args[--novel]}" = 1 ]]; then
	FORMAT="novel"
	TYPE="manga"
	FILTER="format"
elif [[ "${args['--any-manga']}" = 1 ]]; then
	FORMAT="manga"
	TYPE="manga"
	FILTER="type"
else
	FORMAT="tv"
	TYPE="anime"
	FILTER="format"
fi

TITLE=$(array_to_string "${args[title]}")

# Obtain media IDs from AniList
ID=$(curl 'https://graphql.anilist.co/' \
	--silent \
	--request POST \
	--header 'Content-Type: application/json' \
	--header 'Accept: application/json' \
	--data "{ \"query\": \"{ Media(search: \\\"${TITLE}\\\", \
    ${FILTER}: $(echo "${FORMAT}" | tr '[:lower:]' '[:upper:]' || true) \
    sort: [ POPULARITY_DESC ]) { id idMal } }\" }")

# Open the anime or manga in AniList by default, permit MyAnimeList
if [[ "${args[--mal]}" = 1 ]]; then
	if [[ "${args[--social]}" = 1 ]]; then
		FULL_URL=$(curl --silent \
			"https://api.jikan.moe/v4/anime/$(echo "${ID}" |
				jq '.data.Media.idMal')/full" || true)

		xdg-open "$(echo "${FULL_URL}" | jq -r '.data.url' || true)/forum"
	else
		xdg-open https://myanimelist.net/"${TYPE}"/"$(echo "${ID}" | jq '.data.Media.idMal' || true)"
	fi
elif [[ "${args[--both]}" = 1 ]]; then
	if [[ "${args[--social]}" = 1 ]]; then
		FULL_URL=$(curl --silent \
			"https://api.jikan.moe/v4/anime/$(echo "${ID}" |
				jq '.data.Media.idMal')/full" || true)

		xdg-open https://anilist.co/"${TYPE}"/"$(echo "${ID}" | jq '.data.Media.id' || true)"/social
		xdg-open "$(echo "${FULL_URL}" | jq -r '.data.url' || true)/forum"
	else
		xdg-open https://anilist.co/"${TYPE}"/"$(echo "${ID}" | jq '.data.Media.id' || true)"
		xdg-open https://myanimelist.net/"${TYPE}"/"$(echo "${ID}" | jq '.data.Media.idMal' || true)"
	fi
else
	if [[ "${args[--social]}" = 1 ]]; then
		xdg-open https://anilist.co/"${TYPE}"/"$(echo "${ID}" | jq '.data.Media.id' || true)"/social
	else
		xdg-open https://anilist.co/"${TYPE}"/"$(echo "${ID}" | jq '.data.Media.id' || true)"
	fi
fi