-
Notifications
You must be signed in to change notification settings - Fork 0
/
flix-cli
executable file
·115 lines (98 loc) · 3.99 KB
/
flix-cli
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
#!/usr/bin/env bash
# Dependencies - webtorrent-cli, mpv, fzf, curl
# Variables
baseurl="https://1337x.to" # https is neccesary
cachedir="$HOME/.cache/flix-cli"
# Create cache directories
mkdir -p "$cachedir"
# Print styled text
print_style() {
if [ "$2" == "info" ]; then
COLOR="96m"
elif [ "$2" == "success" ]; then
COLOR="92m"
elif [ "$2" == "warning" ]; then
COLOR="93m"
elif [ "$2" == "danger" ]; then
COLOR="91m"
else
COLOR="0m"
fi
STARTCOLOR="\e[$COLOR"
ENDCOLOR="\e[0m"
printf "$STARTCOLOR%b$ENDCOLOR" "$1"
}
# Check dependencies
[ ! -x "$(command -v webtorrent)" ] && print_style "webtorrent is not installed, please install it using npm\n" "danger" && exit 1
[ ! -x "$(command -v mpv)" ] && print_style "mpv is not installed, please install it using your package manager\n" "danger" && exit 1
[ ! -x "$(command -v fzf)" ] && print_style "fzf is not installed, please install it using your package manager\n" "danger" && exit 1
[ ! -x "$(command -v curl)" ] && print_style "curl is not installed, please install it using your package manager\n" "danger" && exit 1
# Get user input
if [ -z "$1" ]; then
print_style "Search:\n> " "success"
read -r query
else
query=$*
fi
query="$(echo -e "$query" | sed 's/ /+/g')"
# Perform search
curl -s $baseurl/search/"$query"/1/ > "$cachedir/tmp.html"
# Extract titles
grep -o '<a href="/torrent/.*</a>' "$cachedir/tmp.html" | sed 's/<[^>]*>//g' > "$cachedir/titles.bw"
result_count=$(wc -l "$cachedir/titles.bw" | awk '{print $1}')
# Check for results
if [ "$result_count" -lt 1 ]; then
print_style "No results found. Please try again...\n" "danger"
exit 1
fi
# Extract additional information
grep -o '<td class="coll-2 seeds.*</td>\|<td class="coll-3 leeches.*</td>' "$cachedir/tmp.html" |
sed 's/<[^>]*>//g' | sed 'N;s/\n/ /' > "$cachedir/seedleech.bw"
grep -o '<td class="coll-4 size.*</td>' "$cachedir/tmp.html" |
sed 's/<span class="seeds">.*<\/span>//g' |
sed -e 's/<[^>]*>//g' > "$cachedir/size.bw"
grep -E '/torrent/' "$cachedir/tmp.html" |
sed -E 's#.*(/torrent/.*)/">.*/#\1#' |
sed 's/td>//g' > "$cachedir/links.bw"
# Format for display
sed 's/\./ /g; s/\-/ /g' "$cachedir/titles.bw" | sed 's/[^A-Za-z0-9 ]//g' | tr -s " " > "$cachedir/tmp" && mv "$cachedir/tmp" "$cachedir/titles.bw"
awk '{print NR " - ["$0"]"}' "$cachedir/size.bw" > "$cachedir/tmp" && mv "$cachedir/tmp" "$cachedir/size.bw"
awk '{print "[S:"$1 ", L:"$2"]" }' "$cachedir/seedleech.bw" > "$cachedir/tmp" && mv "$cachedir/tmp" "$cachedir/seedleech.bw"
# Select a torrent
selected=$(paste -d\ "$cachedir/size.bw" "$cachedir/titles.bw" "$cachedir/seedleech.bw" | fzf --height=30% --reverse --ansi)
if [ -z "$selected" ]; then
print_style "No results selected. Exiting...\n" "danger"
exit 1
fi
LINE=$(echo "$selected" | awk '{print $1}')
# Get the URL and construct the full URL
url=$(head -n "$LINE" "$cachedir/links.bw" | tail -n 1 | tr -d '\r')
fullURL="${baseurl}${url}/"
# Fetch torrent page
curl -s "$fullURL" > "$cachedir/tmp.html"
# Extract magnet link
magnet=$(grep -Eo 'magnet:[^" ]*' "$cachedir/tmp.html" | head -n 1)
if [ -z "$magnet" ]; then
print_style "Failed to retrieve magnet link. Please check the URL or website structure.\n" "danger"
exit 1
fi
# Ask user whether to stream or download
print_style "Stream or download? (s/d): " "info"
read -r action
if [[ "$action" == "d" ]]; then
# Ask user for download directory
print_style "Select download directory:\n" "success"
download_dir=$(find "$HOME" -type d | fzf --height=30% --reverse --ansi)
if [ -z "$download_dir" ]; then
print_style "No directory selected. Exiting...\n" "danger"
exit 1
fi
print_style "Downloading to $download_dir...\n" "info"
webtorrent download "$magnet" --mpv -o "$download_dir"
else
print_style "Streaming with mpv...\n" "info"
webtorrent "$magnet" --mpv
fi
# Cleanup
function cleanup { rm -rf "$cachedir" && rm -rf /tmp/webtorrent; }
trap cleanup EXIT INT SIGINT SIGTERM