-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmal-episodes-renamer.sh
53 lines (45 loc) · 1.27 KB
/
mal-episodes-renamer.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
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: $0 url files..."
echo
echo "Url is MAL (https://myanimelist.net) link to the anime, such as https://myanimelist.net/anime/1/Cowboy_Bebop"
echo "Files are list of files (has to be same as number of episodes) in given order, to be renamed."
echo "This script strips certain characters from the names, such as: ? ! . , '"
echo
echo "This script is part of Script-Tools-Apps collection <https://github.com/Zereges/scripts-tools-apps>"
exit 1
fi
mal_url=`echo $1 | sed 's#/$##'`
shift
titles=`curl $mal_url/episode 2>/dev/null |
grep 'class="fl-l fw-b "' |
sed -E 's#.*>(.*)</a>$#\1#' |
sed '
s/ / /g;
s/&/\&/g;
s/</\</g;
s/>/\>/g;
s/"/\"/g;
s/'/'"'"'/g;
s/“/\"/g;
s/”/\"/g
' |
tr ' ' '_' |
tr -d "?/!.,'"
`
ep_count=`wc -l <<< "$titles"`
if [ $ep_count -ne $# ]; then
>&2 echo "Invalid number of parameters, expected $ep_count"
exit 1
fi
length=`echo -n $ep_count | wc -m`
count=1
while read -r ep_name; do
base_name=`basename "$1"`
dir_name=`dirname "$1"`
extension=${base_name##*.}
prefix=`printf "%0$length""d\n" $count`
mv "$1" "$dir_name"/$prefix"_$ep_name.$extension"
shift
((++count))
done <<< "$titles"