#!/bin/sh
baseurl="https://1337x.to"
cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/1337x"
query=$@
#Make temp dir
mkdir -p $cachedir
#Prompt user if no query given
[ -z "$query" ] && query=$(printf "" | dmenu -p "Search 1337x:")
#exit if input is empty
[ -z "$query" ] && exit 1
query="$(echo $query | sed 's/ /+/g')"
#curl -s https://1337x.to/category-search/$query/Movies/1/ > $cachedir/tmp.html
curl -s "https://1337x.to/search/$query/1/" > $cachedir/tmp.html
# Get Titles
grep -o '' $cachedir/tmp.html |
sed 's/<[^>]*>//g' | sed 'N;s/\n/ /' > $cachedir/seedleech.bw
# Size
grep -o '
.*<\/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 |
dmenu -i -l 25 |
cut -d\- -f1)
[ -z "$LINE" ] && exit 1
for line in $LINE; do
url=$(head -n $line $cachedir/links.bw | tail -n +$line)
# Requesting page for magnet link
curl -s "${baseurl}${url}/" > $cachedir/tmp.html
magnet=$(grep -Po "magnet:\?xt=urn:btih:[a-zA-Z0-9]*" $cachedir/tmp.html | head -n 1)
case "$(printf "mpv\\ntransadd\\ncopy" | dmenu -i -p "Current: $line ")" in
mpv) setsid mpv "$magnet" >/dev/null 2>&1 & ;;
transadd) transadd "$magnet" >/dev/null 2>&1 & ;;
copy) echo "$magnet" | xclip -selection clipboard ;;
esac
done |