From 50189f29c2bd6bf4d3dce19fe9187041142e810d Mon Sep 17 00:00:00 2001 From: alpheratz0 Date: Sat, 1 Jan 2022 20:30:36 -0300 Subject: [PATCH 01/11] feat: check dependencies before use --- notflix | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/notflix b/notflix index 3d380f7..8a00fa1 100755 --- a/notflix +++ b/notflix @@ -1,6 +1,24 @@ #!/bin/sh -# Dependencies - webtorrent, mpv +dependencies="dmenu notify-send curl mpv webtorrent" + +die() { + printf "notflix: %s\n" "$@" >&2 + exit 1 +} + +is_installed() { + which "$1" 2>/dev/null>&2 +} + +check_dependencies() { + for dependency in $dependencies; do + is_installed "$dependency" || + die "$dependency is required to run this script, but it is not installed" + done +} + +check_dependencies mkdir -p $HOME/.cache/notflix From c425ed204f13200ed13688587f11c0ea28389709 Mon Sep 17 00:00:00 2001 From: alpheratz0 Date: Sat, 1 Jan 2022 20:37:35 -0300 Subject: [PATCH 02/11] refactor: move directory and environment setup to its own function --- notflix | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/notflix b/notflix index 8a00fa1..996666c 100755 --- a/notflix +++ b/notflix @@ -1,5 +1,8 @@ #!/bin/sh +menu="dmenu -i -l 25" +baseurl="https://1337x.wtf" +cachedir="$HOME/.cache/notflix" dependencies="dmenu notify-send curl mpv webtorrent" die() { @@ -18,14 +21,12 @@ check_dependencies() { done } -check_dependencies - -mkdir -p $HOME/.cache/notflix +setup() { + mkdir -p "$cachedir" 2>/dev/null>&2 +} -menu="fzf" -menu="dmenu -i -l 25" -baseurl="https://1337x.wtf" -cachedir="$HOME/.cache/notflix" +check_dependencies +setup if [ -z $1 ]; then query=$(dmenu -p "Search Torrent: " <&-) From bc02db8c8f8677a1067384a9724d8fa7db7d7492 Mon Sep 17 00:00:00 2001 From: alpheratz0 Date: Sat, 1 Jan 2022 20:44:50 -0300 Subject: [PATCH 03/11] refactor: move logic to main function --- notflix | 129 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 66 insertions(+), 63 deletions(-) diff --git a/notflix b/notflix index 996666c..7fb13e8 100755 --- a/notflix +++ b/notflix @@ -25,67 +25,70 @@ setup() { mkdir -p "$cachedir" 2>/dev/null>&2 } -check_dependencies -setup - -if [ -z $1 ]; then - query=$(dmenu -p "Search Torrent: " <&-) -else - query=$1 -fi - -query="$(sed 's/ /+/g' <<<$query)" - -#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}') - - -notify-send "πŸ” Searching Magnet seeds 🧲" -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 +main() { + check_dependencies + setup + + if [ -z $1 ]; then + query=$(dmenu -p "Search Torrent: " <&-) + else + query=$1 + fi + + query="$(sed 's/ /+/g' <<<$query)" + + #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}') + + notify-send "πŸ” Searching Magnet seeds 🧲" + 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 ☺️ " +} -# Simple notification -notify-send "πŸŽ₯ Enjoy Watching ☺️ " +main "$@" From 7d5d3fb7d6c031540cb232062adeabb0844fc1fa Mon Sep 17 00:00:00 2001 From: alpheratz0 Date: Sat, 1 Jan 2022 20:59:40 -0300 Subject: [PATCH 04/11] refactor: replace spaces in query with tr instead of sed --- notflix | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/notflix b/notflix index 7fb13e8..257dcbe 100755 --- a/notflix +++ b/notflix @@ -29,13 +29,12 @@ main() { check_dependencies setup - if [ -z $1 ]; then - query=$(dmenu -p "Search Torrent: " <&-) - else + [ $# -eq 0 ] && + query=$(dmenu -p "Search Torrent: " <&-) || query=$1 - fi - query="$(sed 's/ /+/g' <<<$query)" + # make it queryable + query="$(printf "%s" "$query" | tr '[:blank:]' '+')" #curl -s https://1337x.to/category-search/$query/Movies/1/ > $cachedir/tmp.html curl -s $baseurl/search/$query/1/ > $cachedir/tmp.html From 64c9179c961a1c38f5dd9aa33423ed21247ef953 Mon Sep 17 00:00:00 2001 From: alpheratz0 Date: Sat, 1 Jan 2022 21:04:38 -0300 Subject: [PATCH 05/11] refactor: use curl -o flag instead of redirection --- notflix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/notflix b/notflix index 257dcbe..f02ca30 100755 --- a/notflix +++ b/notflix @@ -36,8 +36,8 @@ main() { # make it queryable query="$(printf "%s" "$query" | tr '[:blank:]' '+')" - #curl -s https://1337x.to/category-search/$query/Movies/1/ > $cachedir/tmp.html - curl -s $baseurl/search/$query/1/ > $cachedir/tmp.html + # cache result + curl -s "$baseurl/search/$query/1/" -o "$cachedir/tmp.html" # Get Titles grep -o ' $cachedir/tmp.html + curl -s "$fullURL" -o "$cachedir/tmp.html" magnet=$(grep -Po "magnet:\?xt=urn:btih:[a-zA-Z0-9]*" $cachedir/tmp.html | head -n 1) webtorrent "$magnet" --mpv From 943ca0165649d6d071524072944be9a13477838c Mon Sep 17 00:00:00 2001 From: alpheratz0 Date: Sat, 1 Jan 2022 22:09:06 -0300 Subject: [PATCH 06/11] perf: use perl regex features instead of grep+sed --- notflix | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/notflix b/notflix index f02ca30..1155f4a 100755 --- a/notflix +++ b/notflix @@ -39,15 +39,9 @@ main() { # cache result curl -s "$baseurl/search/$query/1/" -o "$cachedir/tmp.html" - # Get Titles - grep -o ' "$cachedir/titles.bw" || + die 'πŸ˜” No Result found. Try again πŸ”΄' # Seeders and Leechers grep -o '' $cachedir/tmp.html | From f686b8b497f1d9a4370a1fa004f3a59b250fc0d4 Mon Sep 17 00:00:00 2001 From: alpheratz0 Date: Sat, 1 Jan 2022 22:18:13 -0300 Subject: [PATCH 07/11] feat: send a notification using notify-send if running outside a tty --- notflix | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/notflix b/notflix index 1155f4a..20e7f92 100755 --- a/notflix +++ b/notflix @@ -6,7 +6,9 @@ cachedir="$HOME/.cache/notflix" dependencies="dmenu notify-send curl mpv webtorrent" die() { - printf "notflix: %s\n" "$@" >&2 + tty -s && + printf "notflix: %s\n" "$@" >&2 || + notify-send "$@" exit 1 } From 53968db9bcc211439c4526304ab457c8c6bf6462 Mon Sep 17 00:00:00 2001 From: alpheratz0 Date: Sun, 2 Jan 2022 00:06:21 -0300 Subject: [PATCH 08/11] refactor: im not good at commit messages --- notflix | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/notflix b/notflix index 20e7f92..dae771f 100755 --- a/notflix +++ b/notflix @@ -45,44 +45,44 @@ main() { grep -Po ' "$cachedir/titles.bw" || die 'πŸ˜” No Result found. Try again πŸ”΄' - # Seeders and Leechers - grep -o '' $cachedir/tmp.html | - sed 's/<[^>]*>//g' | sed 'N;s/\n/ /' > $cachedir/seedleech.bw + # 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" - # Size - grep -o '.*<\/span>//g' | - sed -e 's/<[^>]*>//g' > $cachedir/size.bw + # get torrent size + grep -Po '\K.*(?= "$cachedir/size.bw" - # Links - grep -E '/torrent/' $cachedir/tmp.html | - sed -E 's#.*(/torrent/.*)/">.*/#\1#' | - sed 's/td>//g' > $cachedir/links.bw + # get links + grep -Po ' "$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 + # 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" - 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 + # 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" - # Getting the line number - LINE=$(paste -d\ $cachedir/size.bw $cachedir/seedleech.bw $cachedir/titles.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\- -f1 | - awk '{$1=$1; print}') + cut -d '-' -f 1 | + tr -d ' ') notify-send "πŸ” Searching Magnet seeds 🧲" - url=$(head -n $LINE $cachedir/links.bw | tail -n +$LINE) - fullURL="${baseurl}${url}/" + 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) + # 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) webtorrent "$magnet" --mpv - # Simple notification + # simple notification notify-send "πŸŽ₯ Enjoy Watching ☺️ " } From 5035659e374883a0e792ae75426e2714a2a9b664 Mon Sep 17 00:00:00 2001 From: alpheratz0 Date: Sun, 2 Jan 2022 00:07:38 -0300 Subject: [PATCH 09/11] fix: show enjoy watching notification before watching, not after --- notflix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notflix b/notflix index dae771f..22d23ee 100755 --- a/notflix +++ b/notflix @@ -80,10 +80,10 @@ main() { curl -s "$fullurl" -o "$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 ☺️ " + + webtorrent "$magnet" --mpv } main "$@" From ab2bd73ef272f642373ce0549db101579d368e27 Mon Sep 17 00:00:00 2001 From: alpheratz0 Date: Sun, 2 Jan 2022 00:12:20 -0300 Subject: [PATCH 10/11] fix: use if-else in die function to prevent edge case where tty -s exits succesfully but printf doesnt --- notflix | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/notflix b/notflix index 22d23ee..7b552c4 100755 --- a/notflix +++ b/notflix @@ -6,9 +6,11 @@ cachedir="$HOME/.cache/notflix" dependencies="dmenu notify-send curl mpv webtorrent" die() { - tty -s && - printf "notflix: %s\n" "$@" >&2 || + if tty -s; then + printf "notflix: %s\n" "$@" >&2 + else notify-send "$@" + fi exit 1 } From a45e0b123e0ec22a9790aba0cf9ffd8e41d20332 Mon Sep 17 00:00:00 2001 From: alpheratz0 Date: Sun, 2 Jan 2022 00:34:45 -0300 Subject: [PATCH 11/11] fix: use command instead of which to check if a program is installed --- notflix | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/notflix b/notflix index 7b552c4..87e4b36 100755 --- a/notflix +++ b/notflix @@ -14,13 +14,9 @@ die() { exit 1 } -is_installed() { - which "$1" 2>/dev/null>&2 -} - check_dependencies() { for dependency in $dependencies; do - is_installed "$dependency" || + command -v "$dependency" 2>/dev/null>&2 || die "$dependency is required to run this script, but it is not installed" done }