-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkanimedir
executable file
·119 lines (102 loc) · 2.76 KB
/
mkanimedir
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
#!/bin/bash
set -eo pipefail
throw() {
printf '%s: %s\n' "${0##*/}" "$1" >&2
return 1
}
sanitize_filename() {
# Replaces chars invalid in Windows file/dir names with a space
perl -pe 's/[<>:"\/\\|?*]/ /g; s/ +/ /g; s/^ +|[ \.]+$//g' <<<"$1"
}
if [[ $# == 0 || $1 =~ ^(--help|-h)$ ]]; then
cat >&2 <<EOF
Usage: mkanimedir <mal link> [<nyaa link>] [<dir>|<files...>]
Creates (or renames) a directory with the anime name and image (as a hidden
folder.jpg) from MAL, moving any given files into it.
If given a nyaa URL, will create a readme.md as well (using mkanimereadme).
Example:
mkanimedir https://myanimelist.net/anime/6547/Angel_Beats
EOF
exit 1
fi
# Parse options
mal_id=
nyaa_url=
existing_dir=
files=()
while (( $# > 0 )); do
case $1 in
-*)
throw "unknown option: $1"
;;
https://myanimelist.net/*)
if [[ ! $1 =~ ^https://myanimelist.net/anime/([[:digit:]]+) ]]; then
throw 'unrecognized mal url'
fi
mal_id=${BASH_REMATCH[1]}
;;
https://*nyaa.si/*)
nyaa_url=$1
;;
*)
if [[ -d $1 ]]; then
if [[ $existing_dir ]]; then
throw 'multiple directories given'
fi
existing_dir=$1
elif [[ -f $1 ]]; then
files+=("$1")
else
throw "'$1' is neither an option nor a file or directory..."
fi
;;
esac
shift
done
# Get mal info
if ! json=$(curl -sSL "https://api.jikan.moe/v4/anime/$mal_id"); then
throw 'failed to get mal data from jikan.moe'
fi
title=$(jq -r .data.title <<<"$json")
image=$(jq -r .data.images.jpg.large_image_url <<<"$json")
if [[ ! $title || $title == 'null' ]]; then
throw "$(printf 'unexpected response from jikan.moe:\n\n%s' "$(jq -C . <<<"$json" 2>/dev/null || echo "$json")")"
fi
# Create or rename directory
base=$(dirname "$existing_dir") # . if empty
dir=$base/$(sanitize_filename "$title")
if [[ ! $existing_dir -ef "$dir" ]]; then
if [[ -e $dir ]]; then
throw "a file/directory named '$dir' already exists"
fi
if [[ $existing_dir ]]; then
mv "$existing_dir" "$dir"
echo "Renamed '$existing_dir' to '$dir'"
else
mkdir "$dir"
echo "Created $dir"
fi
fi
# Download image
if [[ $image && $image != 'null' ]]; then
folderjpg="$dir/folder.jpg"
curl -fsSL "$image" -o "$folderjpg"
# Hide folder.jpg if on Windows
if command -v wslpath &>/dev/null; then
attrib.exe +h "$(wslpath -w "$folderjpg")"
echo "Downloaded folder.jpg (and set as hidden)"
else
echo "Downloaded folder.jpg"
fi
else
echo "No folder.jpg was created, as the api did not return an image" >&2
fi
# Create readme
if [[ $nyaa_url ]]; then
mkanimereadme "$nyaa_url" "$dir"
fi
# Move files into directory
if (( ${#files[@]} > 0 )); then
mv -t "$dir" "${files[@]}"
echo "Moved ${#files[@]} files"
fi