forked from CamilleScholtz/bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neet
executable file
·216 lines (180 loc) · 5.55 KB
/
neet
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
#!/bin/fish
#
# neet - A script to easily play and manage your anime/kdrama/etc.
# onodera, https://github.com/onodera-punpun
## CONFIGURATION
# Set media and config dir locations
set configdir $HOME/.neet
set mediadir $HOME/media/videos
# Pause mpd on play (requires mpc)
set pausempd true
# mpv launch options
set options --fullscreen
## FUNCTIONS
# This function gets basic media info, like the active show name, episode count and total episodes
function info
# Get the active show (+)
set active (grep -B 1 + $configdir/list)
# Get the show name, current episode and total episode count
set -g name (echo $active[-2])
if test -z $current
set -g current (echo $active[2] | cut -d / -f 1 | cut -c 3-)
end
set -g total (echo $active[2] | cut -d / -f 2)
end
# This function tries to fuzzy find the right directory
function directory
# Check if the show already has an alias
if test -n (grep "^$name=" $configdir/alias) -a $realias = false ^ /dev/null
set -g path (grep "^$name=" $configdir/alias | cut -d = -f 2)
return
end
# Lists all directories in mediadir
set directories (ls -d $mediadir/* | rev | cut -d / -f -2 | rev)
# This is the actual fuzzy logic part, it compares the directory names with the active show name
set matches (
for directory in $directories
for word in (echo $name | tr \ \n)
echo $directory | grep -i $word
end
end | cut -d / -f 2 | uniq -c | sort | sed s/\ \ \ \*//g
)
# This greps the output wiith the most matches
# TODO Fix highest with slices
set highest (echo $matches | head -c 1)
set -g path (echo $matches[-1] | cut -d \ -f 2-)
# Count results
set count (echo $path | wc -l)
# Create alias
if test $count -eq 1
echo "Show initialized. (alias set)"
echo $name=$path > $configdir/alias
# TODO: Remove this?
else if test $count -ge 2
echo -e "There are $count directories that match "(set_color yellow)"$name"(set_color normal)":\n"
echo -e "(echo $path | uniq | nl | sed s/\i/\ /g)\n"
read -p "Which directory would you like to use? [1-$count] " answer
if test (seq $count | grep "^$answer\$")
echo -e "\nShow initialized. (alias set)\n"
echo $name=(echo $path | cut -d \$'\n' -f $answer) > $configdir/alias
set -g path (echo $path | cut -d \$'\n' -f $answer)
else
echo "$answer is not a valid answer."
exit 1
end
# Throw an error if there are no directory matches
else
echo "No "(set_color yellow)"directory"(set_color normal)" match found."
exit 1
end
end
# This function tries to get the right file name
function episode
# TODO: Is this needed?
set IFS
# Lists all video files in the directory
# TODO: simplify regex
set episodes (find $mediadir/$path -type f -regex ".*\.\(avi\|mkv\|mp4\|avi.part\|mkv.part\|mp4.part\)" | rev | cut -d / -f -1 | rev)
# Calculates the zeros in a file name (for example two in 002 and one in 089)
#set zerocount (math (echo $total | wc -c) - (echo $current | wc -c) + 1)
#set zero (seq -s 0 $zerocount | tr -d [1-9])
# Greps the right file (hopefully)
# E01
set -g file (echo $episodes | grep -i "E[0]*$current"[^0-9])
# EP01
if test $status -ge 1
set -g file (echo $episodes | grep -i "EP[0]*$current"[^0-9])
# 01
if test $status -ge 1
set -g file (echo $episodes | grep -i " [0]*$current"[^0-9])
# Last resort (just the ep number
if test $status -ge 1
set -g file (echo $episodes | grep -i "$current"[^0-9])
end
end
end
# Throw error if no file is found
if test -z $file
echo "No "(set_color yellow)"episode"(set_color normal)" match found."
exit 1
end
end
# This function pauses mpd, and starts mpv
function play
echo "Playing episode $current/$total, "(set_color yellow)"$name"(set_color normal)"."
if test (echo $file | grep ".part\$")
echo "This episode has not finished downloading yet."
echo "Playback might be buggy or not work at all!"
end
# Pause mpd
if test $pausempd = true
mpc -q pause
end
# Start mpv
mpv $options $mediadir/$path/$file
end
## EXECUTE
set realias false
if test (count $argv) -eq 1
switch $argv[1]
case -h --help
echo -e "Usage: neet [options] [-/+]\n"
echo "options:"
echo " -e, --edit edit list"
echo " -n, --no-resume disable resume playback"
echo " -r, --realias forgets current alias"
echo " -h, --help print help and exit"
exit 0
case -e --edit
eval $EDITOR $configdir/list
exit 0
case -n --no-resume
info
set options $options --no-resume-playback
case -r --realias
info
set realias true
case "+*"
# Fail if there is a non + character in argv
if test (echo $argv[1] | sed s/+//g)
echo "Invalid option, use -h for help."
exit 1
end
info
# Get current episode and increment it by one
set amount (echo $argv[1] | wc -m)
set increment (math $current - 1 + $amount)
if test $increment -le $total
sed -i "s/+ $current\/$total/+ $increment\/$total/" "$configdir/list"
set current $increment
else
echo "Episode $total is the last episode."
exit 1
end
case "-*"
# Fail if there is a non - character in argv
if test (echo $argv[1] | sed s/-//g)
echo "Invalid option, use -h for help."
exit 1
end
info
# Get current episode and decrement it by one
set amount (echo $argv[1] | wc -m)
set decrement (math $current + 1 - $amount)
if test $decrement -ge 1
sed -i "s/+ $current\/$total/+ $decrement\/$total/" $configdir/list
set current $decrement
else
echo "There isn't an episode 0."
exit 1
end
case "*"
echo "Invalid option, use -h for help."
exit 1
end
else
info
end
directory
episode
play