-
Notifications
You must be signed in to change notification settings - Fork 0
/
series.sh
executable file
·281 lines (257 loc) · 10.2 KB
/
series.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/bash
################################################################################
# Series: the program #
# #
# A simple script to automate the visualisation of digital media series #
# #
# Change History #
# XX/XX/2017 Pau Juan-Garcia Original code version 0.0.1 for Linux Mint #
# 11/04/2020 Pau Juan-Garcia Original code version 0.1.0 for Linux Mint #
# 18/04/2020 Pau Juan-Garcia Original code version 0.1.1 for Linux Mint #
# 07/08/2021 Pau Juan-Garcia Original code version 0.1.2 for Linux Mint #
# #
# #
################################################################################
# Copyright (C) 2017, 2021 Pau Juan-Garcia #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 3 of the License, or #
# (at your option) any later version. Available at: #
# https://www.gnu.org/licenses/gpl-3.0.html #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
################################################################################
################################################################################
# Configuration #
################################################################################
SERIESFOLDER="/home/paupau/Videos/series/"
################################################################################
# Help #
################################################################################
Help()
{
# Display Help
echo "Description of the script functions:"
echo
echo "Syntax: series [-s:f:e:r|p|l|L|i:|I:|a:|d|P|n:|N|u|D:|h]"
echo "options:"
echo "s Series name."
echo "f Filetype."
echo "e Number new episodes."
echo "r Play on repeat."
echo "p Watch current episody."
echo "l List available episodes."
echo "L List all media folders."
echo "i Provide details about a specific series."
echo "I Provide online information about a specific series."
echo "a Option to add series by folder name"
echo "d Directory where the new series is"
echo "P Create a marker for a previous episode"
echo "n Watch next episode. (or move forward any given number)"
echo "N Watch next episode and delete previous."
echo "u Update episode folder by a certain number of episodes."
echo "D Delete all episodes of a specific series."
echo "h Print this Help."
echo
}
################################################################################
# Instructions
################################################################################
# To adjust file permissions: chmod +x series.sh
# To add scripts folder to PATH: PATH=$PATH:/Path/To/Script/Folder
# To use as a function in .bashrc:
# series () {
# bash /home/paupau/Scripts/series.sh "$@"
# }
# Or, alternatively, if the function is defined in another file:
# source /home/paupau/Scripts/functions.sh
# To use the repeat mode it is necessary to adjust permissions
################################################################################
# Main program #
################################################################################
# Functions
close_open_vlc () {
# Delete any current VLC sessions
session=$(pgrep vlc)
if [[ ! -z "$session" ]]; then
pgrep vlc | xargs -n1 kill -9
fi
}
# Check there is at least one argument
if [ $# -eq 0 ]; then
echo "No arguments provided"
Help
exit 1
fi
# Process the input options with getops (remember to use colons to indicate input arguments)
while getopts ":s:f:e:a:d:n:pPrlLi:I:NuD:h" option; do
case $option in
s) # Get series name
SERIESNAME="$OPTARG" ;;
f) # Get filetype if any
FILETYPE=$OPTARG ;;
e) # Get number of new episodes if any
NUMBER=$OPTARG ;;
a) # Option to add new series
SERIESNAME="$OPTARG"
ACTION="a" ;;
d) # Directory with new series
DIRECTORY="$OPTARG" ;;
n) # Watch next episode (or move forward any given number)
ACTION="n"
if [[ ! -z $OPTARG ]]; then
NUMBER=$OPTARG
fi
;;
p) # Watch current episody
ACTION="p" ;;
P) # Create marker for previous episode
ACTION="P" ;;
r) # Get series name to play on repeat
REPEAT=true ;;
l) # List available episodes
ACTION="l" ;;
L) # List all media folders
ACTION="L" ;;
i) # Provide details about a specific series
SERIESNAME="$OPTARG"
ACTION="i" ;;
I) # Provide online information about a specific series
SERIESNAME="$OPTARG"
ACTION="I" ;;
N) # Watch next episode and delete previous
ACTION="N" ;;
u) # Update episode folder by a certain number of episodes
ACTION="u" ;;
D) # Delete all episodes of a specific series
SERIESNAME="$OPTARG"
ACTION="D" ;;
h) # Display Help
Help
exit ;;
\?) # incorrect option
echo "Error: Invalid option"
exit ;;
esac
done
# Discard remaining arguments if any
shift $(( $OPTIND - 1 ))
# Set defaults
ACTION=${ACTION:-'n'}
NUMBER=${NUMBER:-1}
# Change to series folder
cd $SERIESFOLDER
# Check the folder exists and exit with error message otherwise
if [[ ! -z "$SERIESNAME" ]] && [[ $ACTION != "a" ]]; then
if [[ ! -d "$SERIESNAME" ]]; then
# TODO This does not seem to be working
echo "Specified series $SERIESNAME does not exist"
exit 1
fi
fi
## Process all options
# Option to list all Video folder
if [[ $ACTION = L ]]; then
cd ..
# List all but text files
ls --color=always *[^.txt] | less -r
# Option to provide list of episodes available
elif [[ $ACTION = l ]]; then
ls --color=always */*[^.txt] | tr '\n' '\0' | xargs -0 -n 1 basename | less -r
# Option to checkout specific series
elif [[ $ACTION = i ]]; then
cd "$SERIESNAME"
# Get txt filename
TXT_FILE=$(ls *.txt)
# Read .txt file
cat < $TXT_FILE | nl -v 0
# Get count of remaining episodes (minus current)
REMAINING=$(cat $TXT_FILE | wc -l)
REMAINING=$(( $REMAINING - 1 ))
echo "There are $REMAINING episodes remaining"
# Option to checkout specific series online
elif [[ $ACTION = I ]]; then
bash ~/Scripts/movies.sh "$SERIESNAME"
firefox --new-tab --search "$SERIESNAME" &
# Option for previous episode (default)
elif [[ $ACTION = p ]]; then
cd "$SERIESNAME"
# Select previous episode
EPISODE=$(head -1 "${SERIESNAME}.txt")
# Start vlc player and notify the user
vlc -f "$EPISODE" &
notify-send "Currently playing:" "$EPISODE"
# Option for previous episode (default)
elif [[ $ACTION = P ]]; then
cd "$SERIESNAME"
# Select previous episode
sed -i "1i --- Line placeholder ---" "${SERIESNAME}.txt"
echo "Created placeholder for previous episode in $SERIESNAME"
# Option for next episode and update list
elif [[ $ACTION = n ]] || [[ $ACTION = N ]]; then
cd "$SERIESNAME"
# Get next episode
EPISODE=$(head -$(($NUMBER + 1)) "${SERIESNAME}.txt" | tail -1)
PREV_EPISODE=$(head -1 "${SERIESNAME}.txt")
# Delete previous epidode if desired
if [[ $ACTION = N ]]; then
rm $PREV_EPISODE
fi
# Delete previous episode from list
tail -n +$(($NUMBER + 1)) "${SERIESNAME}.txt" > "${SERIESNAME}.tmp" && mv "${SERIESNAME}.tmp" "${SERIESNAME}.txt"
# Delete any current VLC sessions
close_open_vlc
# Start vlc player
vlc -f "$EPISODE" &
notify-send "Currently playing:" "$EPISODE"
# Process repeat option
if [[ ! -z "$REPEAT" ]]; then
DURATION=$(ffprobe -i $EPISODE -show_entries format=duration -v quiet | grep duration | sed 's/.*=//')
sleep $(($DURATION + 3))
# Launch script recursively
"$0" -s $* "${SERIESNAME}" -r
fi
# Option to add series by folder name (automatically create .txt)
elif [[ $ACTION = a ]]; then
# Check filetype is set or use default
if [[ -z "$FILETYPE" ]]; then
FILETYPE="mkv"
echo "File extension not provided. Assuming .mkv filetype"
fi
if [[ ! -z "$DIRECTORY" ]]; then
# Create directory if does not exist yet
if [[ ! -d "$SERIESNAME" ]]; then
mkdir -p $SERIESNAME
fi
# Move all files to new directory
mv "${DIRECTORY}"/*.${FILETYPE} ./"${SERIESNAME}"
fi
cd "$SERIESNAME"
# Rename files to get rid of spaces if any
find -type f | rename 's/ /_/g'
# Create tracker file and report
ls *.$FILETYPE > "${SERIESNAME}.txt"
# Add initial line for first watch
sed -i "1i --- Initial run ---" "${SERIESNAME}.txt"
echo "Added $SERIESNAME to list of tracked series"
# Launch script as info
"$0" -i $* "${SERIESNAME}"
# Option to update new episodes
elif [[ $ACTION = u ]]; then
cd "$SERIESNAME"
ls *.$FILETYPE | tail -$NUMBER >> "${SERIESNAME}.txt"
# Option to delete all episodes in a folder
elif [[ $ACTION = D ]]; then
cd "$SERIESNAME"
rm -I *[^.txt]
fi
# IDEAS:
# Try to build autocomplete capabilities with list of series
## https://www.tldp.org/LDP/abs/html/tabexpansion.html
# Add a clean (-c) option, that updates and removes already seen episodes
# Use long options with: https://stackoverflow.com/questions/12022592/how-can-i-use-long-options-with-the-bash-getopts-builtin/30026641