pirokit-spmenu/pirokit
2023-06-04 19:07:20 +02:00

75 lines
2.2 KiB
Bash
Executable file

#!/usr/bin/env bash
# pirokit-spmenu
# Original script by Bugswriter
# Licensed under the MIT license.
mkdir -p $HOME/.cache/pirokit
if [ -z $1 ]; then
query=$(printf "" | ${RUNLAUNCHER:-spmenu} -p "Search 1337x" -g 1 -im -hit -g 1 -hmc)
else
query=$1
fi
[ -z "$query" ] && exit 0
baseurl="https://1337x.to"
cachedir="$HOME/.cache/pirokit"
original_query="$query"
query="$(sed 's/ /+/g' <<<$query)"
curl -s https://1337x.to/search/$query/1/ > $cachedir/tmp.html
# Get 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}')
if [ "$result_count" -lt 1 ]; then
printf "No result found matching '$original_query'.\n"
exit 1
fi
# Seeders and Leechers
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
# Size
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
# Links
grep -E '/torrent/' $cachedir/tmp.html |
sed -E 's#.*(/torrent/.*)/">.*/#\1#' |
sed 's/td>//g' > $cachedir/links.bw
# Clearning up some data to 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
# Getting the line number
LINE=$(paste -d\ $cachedir/size.bw $cachedir/seedleech.bw $cachedir/titles.bw |
${RUNLAUNCHER:-spmenu} -i -l 25 -g 1 -p "Result from query '$original_query'" |
cut -d\- -f1 |
awk '{$1=$1; print}')
[ -z "$LINE" ] && exit 0
url=$(head -n $LINE $cachedir/links.bw | tail -n +$LINE)
fullURL="${baseurl}${url}/"
# Requesting page for magnet link
curl -s $fullURL > $cachedir/tmp.html
[ ! -f "$cachedir/tmp.html" ] && exit 1
magnet=$(grep -Po "magnet:\?xt=urn:btih:[a-zA-Z0-9]*" $cachedir/tmp.html | head -n 1)
[ -n "$magnet" ] && \
printf "%s\n" "$magnet"