-
Notifications
You must be signed in to change notification settings - Fork 0
/
ani-local
executable file
·173 lines (147 loc) · 4.18 KB
/
ani-local
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
#!/bin/bash
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
WHITE_BOLD='\033[1;37m'
CYAN='\033[0;36m'
NC='\033[0m'
# Configuration file path
CONFIG_FILE="$HOME/.local/bin/ani-local.conf"
# Load configuration
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
else
echo -e "${RED}Configuration file not found: $CONFIG_FILE${NC}"
exit 1
fi
LOG_FILE="$(dirname "$0")/ani-local.log"
# Ensure the log file exists
touch "$LOG_FILE"
function show_logs() {
if [ -f "$LOG_FILE" ]; then
less "$LOG_FILE"
else
echo -e "${RED}Log file not found.${NC}"
fi
exit 0
}
if [ "$1" == "logs" ]; then
show_logs
fi
if [ ! -d "$SOURCE_DIR" ]; then
echo -e "${RED}Directory $SOURCE_DIR does not exist.${NC}"
exit 1
fi
function get_last_seen() {
local subdir="$1"
grep "$subdir" "$LOG_FILE" | tail -n 1 | awk -F '/' '{print $NF}' | sed 's/.mp4//'
}
function select_subdir() {
while true; do
clear
mapfile -t subdirs < <(find "$SOURCE_DIR" -mindepth 1 -maxdepth 1 -type d -printf "%f\n")
if [ ${#subdirs[@]} -eq 0 ]; then
echo -e "${RED}No subdirectories found in $SOURCE_DIR.${NC}"
exit 1
fi
subdirs+=("Exit")
echo -e "${CYAN}Select an anime series:${NC}"
PS3="Option: "
select subdir in "${subdirs[@]}"; do
if [[ "$subdir" == "Exit" ]]; then
echo -e "${YELLOW}Exiting...${NC}"
clear
exit 0
elif [ -n "$subdir" ]; then
select_file "$subdir"
break
else
echo -e "${RED}Invalid selection.${NC}"
fi
done
done
}
function select_file() {
local subdir="$1"
local last_seen=$(get_last_seen "$subdir")
local current_page=1
local per_page=10
local start_index=0
mapfile -t files < <(ls "$SOURCE_DIR/$subdir"/*.mp4 2>/dev/null | sort -V)
if [ -n "$last_seen" ]; then
for i in "${!files[@]}"; do
if [[ "${files[$i]}" == *"$last_seen.mp4" ]]; then
current_page=$(( (i / per_page) + 1 ))
start_index=$(( (current_page - 1) * per_page ))
break
fi
done
fi
while true; do
clear
local end_index=$(( start_index + per_page - 1 ))
if [ ${#files[@]} -eq 0 ]; then
echo -e "${RED}No .mp4 files found in $SOURCE_DIR/$subdir.${NC}"
return
fi
echo -e "${CYAN}Select an episode to watch (Page $current_page):${NC}"
for i in $(seq $start_index $end_index); do
if [ $i -ge ${#files[@]} ]; then
break
fi
file_name=$(basename "${files[$i]}" .mp4)
if [[ "$file_name" == "$last_seen" ]]; then
echo -e "${WHITE_BOLD}$((i + 1)))${NC} ${YELLOW}(Last seen) $file_name${NC}"
else
echo -e "${WHITE_BOLD}$((i + 1)))${NC} ${file_name}"
fi
done
read -rp "Option: " choice
case $choice in
[0-9]*)
if [ $choice -gt 0 ] && [ $choice -le ${#files[@]} ]; then
local selected_file="${files[$((choice - 1))]}"
local file_base=$(basename "${selected_file}" .mp4)
echo -e "${GREEN}Playing: $selected_file${NC}"
# Play the file
mpv --fs "$selected_file" > >(grep -E '^[0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2}' | tee -a "$LOG_FILE") 2>/dev/null
# Update log file with the current file
echo "$(date '+%d/%m/%Y - %I:%M%p') - $selected_file" >> "$LOG_FILE"
# Update last_seen dynamically after playing the video
last_seen=$(basename "$selected_file" .mp4)
else
echo -e "${RED}Invalid selection.${NC}"
fi
;;
n)
if [ $end_index -lt $((${#files[@]} - 1)) ]; then
current_page=$((current_page + 1))
start_index=$(( (current_page - 1) * per_page ))
else
echo -e "${YELLOW}You are already on the last page.${NC}"
fi
;;
p)
if [ $current_page -gt 1 ]; then
current_page=$((current_page - 1))
start_index=$(( (current_page - 1) * per_page ))
else
echo -e "${YELLOW}You are already on the first page.${NC}"
fi
;;
r)
return
;;
q)
echo -e "${YELLOW}Exiting...${NC}"
clear
exit 0
;;
*)
echo -e "${RED}Invalid selection.${NC}"
;;
esac
done
}
select_subdir