-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththumb_test
More file actions
executable file
·362 lines (292 loc) · 11.3 KB
/
thumb_test
File metadata and controls
executable file
·362 lines (292 loc) · 11.3 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/bin/sh
# JioSaavn Thumbnail Viewer App - Kitty Protocol Version
# Simple app to search JioSaavn and display thumbnails using Kitty's native image protocol
app_name="JioSaavn Thumbnail Viewer"
version="2.0.0 (Kitty Edition)"
agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0"
# Colors
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
MAGENTA='\033[1;35m'
CYAN='\033[1;36m'
WHITE='\033[1;37m'
NC='\033[0m' # No Color
# Banner
show_banner() {
clear
printf "${CYAN}╔══════════════════════════════════════════════╗${NC}\n"
printf "${CYAN}║ 🎵 JioSaavn Thumbnail Viewer (Kitty) ║${NC}\n"
printf "${CYAN}║ v%s ║${NC}\n" "$version"
printf "${CYAN}╚══════════════════════════════════════════════╝${NC}\n\n"
}
# Check if running in Kitty terminal
check_kitty() {
if [ "$TERM" != "xterm-kitty" ] && [ -z "$KITTY_WINDOW_ID" ]; then
printf "${YELLOW}⚠️ Warning: This app is optimized for Kitty terminal${NC}\n"
printf "${YELLOW}Current terminal: %s${NC}\n" "${TERM:-unknown}"
printf "${YELLOW}Images may not display properly in other terminals${NC}\n"
printf "${BLUE}Press Enter to continue anyway... ${NC}"
read -r
else
printf "${GREEN}✓ Kitty terminal detected - images will display properly${NC}\n"
fi
}
# Check dependencies
check_deps() {
missing_deps=""
for dep in curl jq fzf base64; do
if ! command -v "$dep" >/dev/null 2>&1; then
missing_deps="$missing_deps $dep"
fi
done
if [ -n "$missing_deps" ]; then
printf "${RED}Missing dependencies:${NC}%s\n" "$missing_deps"
printf "${YELLOW}Please install the missing dependencies:\n"
printf " curl jq fzf base64 - available in most package managers${NC}\n"
exit 1
fi
}
# Search JioSaavn
search_jiosaavn() {
query="$1"
if [ -z "$query" ]; then
printf "${RED}No search query provided!${NC}\n"
return 1
fi
printf "${BLUE}🔍 Searching JioSaavn for: ${WHITE}%s${NC}\n" "$query"
# URL encode the query
encoded_query=$(printf '%s' "$query" | sed 's/ /+/g')
# Search JioSaavn API
response=$(curl -s "https://www.jiosaavn.com/api.php?p=1&q=$encoded_query&_format=json&_marker=0&api_version=4&ctx=web6dot0&n=15&__call=search.getResults")
if [ -z "$response" ] || [ "$response" = "null" ]; then
printf "${RED}No results found!${NC}\n"
return 1
fi
# Parse results with thumbnails
printf '%s' "$response" | jq -r '.results[]? |
[.id, .title, (.more_info.artistMap.primary_artists[0].name // "Unknown Artist"),
(.more_info.album // "Unknown Album"),
(.image // .more_info.image // ""),
(.more_info.duration // "0")] | @tsv' 2>/dev/null
}
# Get high quality thumbnail URL
get_hq_thumbnail() {
thumbnail_url="$1"
if [ -z "$thumbnail_url" ] || [ "$thumbnail_url" = "null" ]; then
return 1
fi
# Convert to higher resolution (500x500 instead of 150x150)
hq_url=$(printf '%s' "$thumbnail_url" | sed 's/150x150/500x500/g')
# Try other common size patterns if no 150x150 found
if [ "$hq_url" = "$thumbnail_url" ]; then
hq_url=$(printf '%s' "$thumbnail_url" | sed 's/50x50/500x500/g')
fi
printf '%s' "$hq_url"
}
# Display thumbnail using Kitty protocol
show_thumbnail_kitty() {
url="$1"
title="$2"
artist="$3"
if [ -z "$url" ]; then
printf "${YELLOW}No thumbnail URL available${NC}\n"
return 1
fi
# Create temp directory
temp_dir="/tmp/jiosaavn-kitty-thumbnails"
mkdir -p "$temp_dir"
# Generate filename from URL hash
thumb_file="$temp_dir/$(printf '%s' "$url" | md5sum | cut -d' ' -f1).jpg"
printf "${BLUE}📥 Downloading thumbnail from: ${NC}\n${CYAN}%s${NC}\n" "$url"
# Download thumbnail
if ! curl -L --max-time 15 --user-agent "$agent" "$url" -o "$thumb_file"; then
printf "${RED}Failed to download thumbnail!${NC}\n"
return 1
fi
# Check file size
file_size=$(wc -c < "$thumb_file" 2>/dev/null || echo "0")
if [ "$file_size" -lt 1000 ]; then
printf "${RED}Downloaded file too small (%s bytes)${NC}\n" "$file_size"
rm -f "$thumb_file"
return 1
fi
# Verify it's an image
file_type=$(file "$thumb_file" 2>/dev/null || echo "unknown")
if ! printf '%s' "$file_type" | grep -E "(JPEG|PNG|GIF|WebP)" >/dev/null; then
printf "${RED}Invalid image file: %s${NC}\n" "$file_type"
rm -f "$thumb_file"
return 1
fi
printf "${GREEN}✓ Image downloaded successfully (%s bytes)${NC}\n" "$file_size"
# Clear screen for display
printf "\033[2J\033[H"
# Show header
printf "${CYAN}╔══════════════════════════════════════════════╗${NC}\n"
printf "${CYAN}║ 🎵 Now Playing ║${NC}\n"
printf "${CYAN}╚══════════════════════════════════════════════╝${NC}\n\n"
# Display using Kitty protocol - Method 1: kitten icat (if available)
if command -v "kitten" >/dev/null 2>&1; then
printf "${BLUE}📺 Displaying with kitten icat...${NC}\n"
kitten icat --align=left --place=35x20@0x6 "$thumb_file" 2>/dev/null || {
printf "${YELLOW}kitten icat failed, trying direct protocol...${NC}\n"
display_kitty_direct "$thumb_file"
}
else
printf "${BLUE}📺 Using direct Kitty protocol...${NC}\n"
display_kitty_direct "$thumb_file"
fi
# Position cursor after image and show song info
printf "\033[27;1H" # Move cursor to line 27, column 1
printf "${GREEN}🎵 Song Details:${NC}\n"
printf "${WHITE} ♫ Title: %s${NC}\n" "$title"
printf "${WHITE} 🎤 Artist: %s${NC}\n" "$artist"
printf "${BLUE} 🔗 Image: %s${NC}\n\n" "$url"
printf "${YELLOW}Controls: [Enter] Continue [s] Search again [q] Quit${NC}\n"
printf "${CYAN}Choice: ${NC}"
# Store image file path for cleanup
echo "$thumb_file" > "$temp_dir/current_image"
}
# Direct Kitty protocol display
display_kitty_direct() {
image_file="$1"
width="${2:-35}"
height="${3:-20}"
if [ ! -f "$image_file" ]; then
printf "${RED}Image file not found for display${NC}\n"
return 1
fi
# Move cursor to display position
printf "\033[6;1H"
# Display using Kitty's inline image protocol
printf '\033]1337;File=inline=1;width=%dcells;height=%dcells:' "$width" "$height"
base64 "$image_file" | tr -d '\n'
printf '\a'
printf "\n${GREEN}✓ Image displayed using Kitty protocol${NC}\n"
}
# Hide/clear thumbnail
hide_thumbnail() {
temp_dir="/tmp/jiosaavn-kitty-thumbnails"
# Clear screen
printf "\033[2J\033[H"
# Clean up temp files
if [ -f "$temp_dir/current_image" ]; then
current_image=$(cat "$temp_dir/current_image")
rm -f "$current_image" 2>/dev/null
rm -f "$temp_dir/current_image"
fi
}
# FZF selection interface
select_song() {
results="$1"
if [ -z "$results" ]; then
return 1
fi
# Format results for fzf display
formatted_results=$(printf '%s\n' "$results" | while IFS=$'\t' read -r id title artist album thumb_url duration; do
# Convert duration to readable format
if [ "$duration" != "0" ] && [ -n "$duration" ]; then
minutes=$((duration / 60))
seconds=$((duration % 60))
duration_str=$(printf "%d:%02d" "$minutes" "$seconds")
else
duration_str="--:--"
fi
printf "%-40s | %-25s | %-20s | %s\n" "$title" "$artist" "$album" "$duration_str"
done)
# Use fzf to select with better preview
selection=$(printf '%s\n' "$formatted_results" | \
fzf --height=60% --border --prompt="🎵 Select song: " \
--header="Title | Artist | Album | Duration" \
--preview-window=hidden --cycle --reverse \
--color=16 --margin=1 --padding=1)
if [ -z "$selection" ]; then
return 1
fi
# Extract the selected line number
line_num=$(printf '%s\n' "$formatted_results" | grep -n "^$selection$" | head -1 | cut -d: -f1)
if [ -n "$line_num" ]; then
# Return the corresponding line from original results
printf '%s\n' "$results" | sed -n "${line_num}p"
fi
}
# Main application loop
main_loop() {
show_banner
check_kitty
while true; do
# Get search query
printf "${CYAN}🔍 Enter search query (or 'quit' to exit): ${NC}"
read -r query
case "$query" in
"quit"|"q"|"exit")
hide_thumbnail
printf "${GREEN}Thanks for using JioSaavn Kitty Thumbnail Viewer!${NC}\n"
exit 0
;;
"")
printf "${YELLOW}Please enter a search query.${NC}\n"
continue
;;
esac
# Search JioSaavn
results=$(search_jiosaavn "$query")
if [ -z "$results" ]; then
printf "${RED}No results found for '%s'. Try a different query.${NC}\n" "$query"
continue
fi
result_count=$(printf '%s\n' "$results" | wc -l)
printf "${GREEN}✓ Found %d results! Select a song to view thumbnail:${NC}\n\n" "$result_count"
# Let user select a song
selected=$(select_song "$results")
if [ -z "$selected" ]; then
printf "${YELLOW}No selection made.${NC}\n"
continue
fi
# Parse selected song
IFS=$'\t' read -r song_id title artist album thumb_url duration <<< "$selected"
# Get high quality thumbnail
hq_thumb_url=$(get_hq_thumbnail "$thumb_url")
# Display thumbnail using Kitty protocol
if [ -n "$hq_thumb_url" ]; then
show_thumbnail_kitty "$hq_thumb_url" "$title" "$artist"
else
printf "${RED}No thumbnail available for this song.${NC}\n"
printf "${YELLOW}Press Enter to continue...${NC} "
read -r
continue
fi
# Wait for user input
read -r action
hide_thumbnail
case "$action" in
"q"|"quit")
printf "${GREEN}Goodbye! 👋${NC}\n"
exit 0
;;
"s"|"search")
show_banner
continue
;;
*)
show_banner
continue
;;
esac
done
}
# Cleanup function
cleanup() {
hide_thumbnail
printf "\n${GREEN}Cleaned up and exiting... 🧹${NC}\n"
exit 0
}
# Set trap for cleanup
trap cleanup INT TERM EXIT
# Start the app
printf "${BLUE}🔧 Checking dependencies...${NC}\n"
check_deps
printf "${GREEN}✓ All dependencies found!${NC}\n"
sleep 1
main_loop