Bugswriter / notflix

Notflix is a shell script to search and stream torrent.
GNU General Public License v3.0
1.9k stars 175 forks source link

README BEFORE MERGING #59

Closed ghost closed 2 years ago

ghost commented 2 years ago

More Tips on Writing Portable Shell Scripts

One way to easily check if your script is portable to other UNIX systems is to bookmark OpenBSD's manpage search. On Firefox, right click on the search bar and select Add a Keyword for this Search…, then enter man on the keyword field and save. Voila, now if you press CTRL+L and type man man you can read OpenBSD's man page for man.

I remember checking OpenBSD's manual for grep and I found there was no -P option, so that's obviously not POSIX. It's also good practice to support XDG directory specification whenever it's possible. (eg. $XDG_CACHE_HOME).

Since I know you like BSD content, you might also want to check out OpenBSD's policy. It's a very dense article explaining copyrights and software licenses. You'll learn a lot, I promise.

How I Went Refactoring Notflix from Scratch

People often say: “You shouldn't use comments, unless you're explaining something that the code can't tell you” – Well, I don't use comments at all. Instead I organize the program into small functions, and I have to say that has really paid off as a habit. Functions are awesome, they limit you in a way that you only have to think about one thing at the time.

searching() {
curl -s "https://1337x.wtf/search/$QUERY/1/" | grep -E '/torrent/' |
    sed -E 's#.*(/torrent/.*)/">.*/#\1#;s#td>##g' | tee "$CACHE/links"
}

The only thing this function does is searching for links. It's compact and descriptive. You should also notice, because the tee command writes both to $CACHE/links and /dev/stdout this function outputs a text stream!

selecting() {
searching | sed 's/^.*\///;s/-/ /g' |
    dmenu -p notflix -l 20 | sed 's/ /-/g'
}

This second function uses the first function to let the use select between the various titles. Notice that since the title name is in the link I don't have to fetch titles separately. First develop the foundation then expand.

getmagnet() {
curl -s "https://1337x.wtf$(grep "$(selecting)" "$CACHE/links")/" |
    grep -Eo "magnet:\?xt=urn:btih:[a-zA-Z0-9]*" | head -n1
}

The third uses the second in a nested subshell for “grepping” out the chosen link from the previously “tee-ed” cache file.

CACHE=${XDG_CACHE_HOME:-$HOME/.cache}/notflix && mkdir -p "$CACHE" >&-
QUERY=$(dmenu -p search <&- | tr ' ' '+')

command -v webtorrent &&
    webtorrent --mpv "$(getmagnet)" 2>&- ||
    transmission-cli "$(getmagnet)" 2>&- || helpmeuse

This is actually what gets executed first – It's like the main() function in C. If you don't have the programs installed it's going to default back until help.

How was that? Cool, isn't it! That's how I write shell scripts. If you made it this far I encourage you to check my scripts on GitHub. It's been a while since the last update but I'm working on it. I'm also going to post this conversation on my blog, but that might take even longer since I'm moving to OpenBSD.

Loved your New Year's livestream, keep going <3