diff --git a/notflix b/notflix index c30a7cd..29ffa64 100755 --- a/notflix +++ b/notflix @@ -1,74 +1,89 @@ #!/bin/sh -# Dependencies - webtorrent, mpv - -mkdir -p $HOME/.cache/notflix - menu="dmenu -i -l 25" baseurl="https://1337x.wtf" cachedir="$HOME/.cache/notflix" - -if [ -z $1 ]; then - query=$(dmenu -p "Search Torrent: " <&-) -else - query=$1 -fi - -query="$(echo $query | sed 's/ /+/g')" - -#curl -s https://1337x.to/category-search/$query/Movies/1/ > $cachedir/tmp.html -curl -s $baseurl/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 | - $menu | - cut -d\- -f1 | - awk '{$1=$1; print}') - -if [ -z "$LINE" ]; then - notify-send "πŸ˜” No Result selected. Exiting... πŸ”΄" -i "NONE" - exit 0 -fi -notify-send "πŸ” Searching Magnet seeds 🧲" -i "NONE" -url=$(head -n $LINE $cachedir/links.bw | tail -n +$LINE) -fullURL="${baseurl}${url}/" - -# Requesting page for magnet link -curl -s $fullURL > $cachedir/tmp.html -magnet=$(grep -Po "magnet:\?xt=urn:btih:[a-zA-Z0-9]*" $cachedir/tmp.html | head -n 1) - -webtorrent "$magnet" --mpv - -# Simple notification -notify-send "πŸŽ₯ Enjoy Watching ☺️ " -i "NONE" +dependencies="dmenu notify-send curl mpv webtorrent" + +die() { + if tty -s; then + printf "notflix: %s\n" "$@" >&2 + else + notify-send "$@" -i "NONE" + fi + exit 1 +} + +check_dependencies() { + for dependency in $dependencies; do + command -v "$dependency" 2>/dev/null>&2 || + die "$dependency is required to run this script, but it is not installed" + done +} + +setup() { + mkdir -p "$cachedir" 2>/dev/null>&2 +} + +main() { + check_dependencies + setup + + [ $# -eq 0 ] && + query=$(dmenu -p "Search Torrent: " <&-) || + query=$1 + + # make it queryable + query="$(printf "%s" "$query" | tr '[:blank:]' '+')" + + # cache result + curl -s "$baseurl/search/$query/1/" -o "$cachedir/tmp.html" + + # get search result titles (grep exit code will be non-zero if no match is found) + grep -Po ' "$cachedir/titles.bw" || + die 'πŸ˜” No Result found. Try again πŸ”΄' + + # get seeders and leechers count + grep -Po '\K[0-9]+(?=)' "$cachedir/tmp.html" > "$cachedir/seeders.bw" + grep -Po '\K[0-9]+(?=)' "$cachedir/tmp.html" > "$cachedir/leechers.bw" + + # get torrent size + grep -Po '\K.*(?= "$cachedir/size.bw" + + # get links + grep -Po ' "$cachedir/links.bw" + + # make titles more readable and remove non alphanumeric characters + 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" + + # merge seeders and leechers results in the format [S: num_seeders, L: num_leechers] + paste -d ' ' "$cachedir/seeders.bw" "$cachedir/leechers.bw" | + awk '{ print "[S: "$1", L: "$2"]" }' > "$cachedir/seedleech.bw" + + # prefix the size of the torrent with the line number + awk '{print NR " - ["$0"]"}' "$cachedir/size.bw" > "$cachedir/tmp" && mv "$cachedir/tmp" "$cachedir/size.bw" + + # getting the selected line number + selected=$(paste -d ' ' "$cachedir/size.bw" "$cachedir/seedleech.bw" "$cachedir/titles.bw" | + $menu | + cut -d '-' -f 1 | + tr -d ' ') + + [ -z "$selected" ] && die "πŸ˜” No Result selected. Exiting... πŸ”΄" + + notify-send "πŸ” Searching Magnet seeds 🧲" -i "NONE" + relativeurl=$(head -n "$selected" "$cachedir/links.bw" | tail -n +"$selected") + fullurl="${baseurl}/${relativeurl}/" + + # requesting page for magnet link + curl -s "$fullurl" -o "$cachedir/tmp.html" + magnet=$(grep -Po "magnet:\?xt=urn:btih:[a-zA-Z0-9]*" "$cachedir/tmp.html" | head -n 1) + + # simple notification + notify-send "πŸŽ₯ Enjoy Watching ☺️ " -i "NONE" + + webtorrent "$magnet" --mpv +} + +main "$@" \ No newline at end of file