Skip to content

Commit 23964d0

Browse files
committedAug 30, 2022
Refactoring and add support for dmenu -it option
Signed-off-by: Mathieu-COSYNS <[email protected]>
1 parent b87a6b9 commit 23964d0

File tree

1 file changed

+95
-55
lines changed

1 file changed

+95
-55
lines changed
 

‎notflix

+95-55
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/bin/sh
22

3-
# Dependencies - peerflix, mpv
3+
# Dependencies:
4+
# - peerflix
5+
# - mpv
6+
# - https://github.com/Mathieu-COSYNS/dmenu
47

58
menu="dmenu -i -l 20"
69
baseurl="https://1337x.wtf"
@@ -13,61 +16,98 @@ seedleechfile="$cachedir/seedleech.txt"
1316
sizesfile="$cachedir/sizes.txt"
1417
linksfile="$cachedir/links.txt"
1518

16-
query=`(test -z "$@" && $menu -p "Search Torrent: " <&- || echo "$@") | sed 's/ /+/g'`
19+
read_query() {
20+
QUERY=`$menu -p "Search Torrent: " -it "$QUERY" <&-`
1721

18-
# Canceled
19-
test -z "$query" && exit 0
22+
# Canceled
23+
test -z "$QUERY" && return 1
24+
25+
echo "Searching for: $QUERY"
26+
27+
return 0
28+
}
29+
30+
get_torrents_list() {
31+
# Download search results
32+
curl -s "$baseurl/search/$(echo $QUERY | sed 's/ /+/g')/1/" > "$searchhtmlfile"
33+
34+
# Get Titles
35+
grep -o '<a href="/torrent/.*</a>' "$searchhtmlfile" |
36+
sed 's/<[^>]*>//g' |
37+
sed 's/\./ /g; s/\-/ /g' |
38+
sed 's/[^A-Za-z0-9 ]//g' |
39+
tr -s " " > "$titlefile"
40+
41+
result_count=$(wc -l "$titlefile" | awk '{print $1}')
42+
if [ "$result_count" -lt 1 ]; then
43+
notify-send "😔 No Result found. Try again 🔴"
44+
return 1
45+
fi
46+
47+
# Seeders and Leechers
48+
grep -o '<td class="coll-2 seeds.*</td>\|<td class="coll-3 leeches.*</td>' "$searchhtmlfile" |
49+
sed 's/<[^>]*>//g' |
50+
sed 'N;s/\n/ /' |
51+
awk '{print "[S:"$1 ", L:"$2"]" }' > "$seedleechfile"
52+
53+
# Size
54+
grep -o '<td class="coll-4 size.*</td>' "$searchhtmlfile" |
55+
sed 's/<span class="seeds">.*<\/span>//g' |
56+
sed -e 's/<[^>]*>//g' > "$sizesfile"
57+
58+
# Links
59+
grep -Eo "/torrent\/[0-9]{7}\/[a-zA-Z0-9?%-]*/" "$searchhtmlfile" > "$linksfile"
60+
61+
return 0
62+
}
63+
64+
select_torrent_url() {
65+
# Getting the line number
66+
LINE=$(expr "$(paste -d '@' "$sizesfile" "$seedleechfile" "$titlefile" |
67+
sed 's/@/ - /g' |
68+
$menu -r -ix)")
69+
70+
if [ -z "$LINE" ]; then
71+
return 1
72+
fi
73+
74+
LINE=$(echo "$LINE + 1" | bc)
75+
76+
movieurl="${baseurl}$(head -n $LINE "$linksfile" | tail -n +$LINE)"
77+
78+
return 0
79+
}
80+
81+
get_magnet_from_movieurl() {
82+
echo "Reading $movieurl"
83+
magnet=$(curl -s "$movieurl" | grep -Po "magnet:\?xt=urn:btih:[a-zA-Z0-9]*" | head -n 1)
84+
}
85+
86+
open_magnet_link() {
87+
if [ -z "$magnet" ]; then
88+
notify-send "🧲 Magnet not found!"
89+
return 1
90+
fi
91+
92+
notify-send "🧲 Magnet found! Loading..."
93+
94+
PEERFLIX_FLAGS="-l --mpv -- --fs"
95+
96+
if [ -t 0 ]; then
97+
peerflix "$magnet" $PEERFLIX_FLAGS
98+
else
99+
x-terminal-emulator -T Notflix -e bash -i -c "peerflix \"$magnet\" $PEERFLIX_FLAGS"
100+
fi
101+
102+
return 0
103+
}
104+
105+
# Read args
106+
QUERY="$@"
20107

21108
mkdir -p "$cachedir"
22109

23-
# Download search results
24-
curl -s "$baseurl/search/$query/1/" > "$searchhtmlfile"
25-
26-
# Get Titles
27-
grep -o '<a href="/torrent/.*</a>' "$searchhtmlfile" |
28-
sed 's/<[^>]*>//g' |
29-
sed 's/\./ /g; s/\-/ /g' |
30-
sed 's/[^A-Za-z0-9 ]//g' |
31-
tr -s " " > "$titlefile"
32-
33-
result_count=$(wc -l "$titlefile" | awk '{print $1}')
34-
if [ "$result_count" -lt 1 ]; then
35-
notify-send "😔 No Result found. Try again 🔴"
36-
exit 0
37-
fi
38-
39-
# Seeders and Leechers
40-
grep -o '<td class="coll-2 seeds.*</td>\|<td class="coll-3 leeches.*</td>' "$searchhtmlfile" |
41-
sed 's/<[^>]*>//g' |
42-
sed 'N;s/\n/ /' |
43-
awk '{print "[S:"$1 ", L:"$2"]" }' > "$seedleechfile"
44-
45-
# Size
46-
grep -o '<td class="coll-4 size.*</td>' "$searchhtmlfile" |
47-
sed 's/<span class="seeds">.*<\/span>//g' |
48-
sed -e 's/<[^>]*>//g' > "$sizesfile"
49-
50-
# Links
51-
grep -Eo "/torrent\/[0-9]{7}\/[a-zA-Z0-9?%-]*/" "$searchhtmlfile" > "$linksfile"
52-
53-
# Getting the line number
54-
LINE=$(expr "$(paste -d '@' "$sizesfile" "$seedleechfile" "$titlefile" |
55-
sed 's/@/ - /g' |
56-
$menu -r -ix) + 1" | bc)
57-
58-
movieurl="${baseurl}$(head -n $LINE "$linksfile" | tail -n +$LINE)"
59-
echo $movieurl
60-
magnet=$(curl -s "$movieurl" | grep -Po "magnet:\?xt=urn:btih:[a-zA-Z0-9]*" | head -n 1)
61-
62-
if [ -z "$magnet" ]; then
63-
notify-send "🧲 Magnet not found!"
64-
exit 1
65-
fi
66-
67-
notify-send "🧲 Magnet found! Loading..."
68-
69-
PEERFLIX_FLAGS="-l --mpv -- --fs"
70-
71-
test -t 0 && \
72-
peerflix "$magnet" $PEERFLIX_FLAGS || \
73-
x-terminal-emulator -T Notflix -e bash -i -c "peerflix \"$magnet\" $PEERFLIX_FLAGS"
110+
while :; do
111+
read_query || exit 0
112+
get_torrents_list && select_torrent_url && get_magnet_from_movieurl && open_magnet_link && exit 0
113+
done

0 commit comments

Comments
 (0)
Please sign in to comment.