[ / / / / / / / / / ] [ dir / ask / dcaco / fur / hypno / o / sl / ttgg / u ][Options][ watchlist ]

/prog/ - Programming

Programming board

Catalog

You can now write text to your AI-generated image at https://aiproto.com It is currently free to use for Proto members.
Name
Email
Subject
Comment *
File
Select/drop/paste files here
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Expand all images

File (hide): 1440822372808.png (92.24 KB, 1256x618, 628:309, Screen Shot 2015-08-29 at ….png) (h) (u)

[–]

5eb139 (8) No.3149>>3155 [Watch Thread][Show All Posts]

I made a simple auto-notification script that you can run on your *nix machine. Just toss this script in your crontab, or run it in a loop on screen and you will start getting notifications for your favorite boards.

You can call this script like this to get notifications from /b/ and /prog/.


./8chan.sh b prog

Add as many boards or as few as you'd like.


#!/bin/bash
for board in "$@"
do
if [ ! -f /tmp/$board.hash ]
then
curl --silent http://8ch.net/$board/0.json | md5 > /tmp/$board.hash
else
oldHash=`cat /tmp/$board.hash`
newHash=`curl --silent http://8ch.net/$board/0.json | md5`
if [[ ! $oldHash == $newHash ]]
then
echo $newHash > /tmp/$board.hash
say "$board has a new post"
fi
fi
done

Note: I believe

say
is only available on mac os, so you might need to change that to whatever your OS uses.

Cheers.

5eb139 (8) No.3151

Just toss it on crontab like this.

* * * * * /bin/bash /Users/admin/scripts/8chan.sh r9k v pol tech


5eb139 (8) No.3152>>3153

Might need to use absolute paths for it to work correctly in crontab:

#!/bin/bash
for board in "$@"
do
if [ ! -f /tmp/$board.hash ]
then
/usr/bin/curl --silent http://8ch.net/$board/0.json | /sbin/md5 > /tmp/$board.hash
else
oldHash=`/bin/cat /tmp/$board.hash`
newHash=`/usr/bin/curl --silent http://8ch.net/$board/0.json | /sbin/md5`
if [[ ! $oldHash == $newHash ]]
then
echo $newHash > /tmp/$board.hash
/usr/bin/say "$board has a new post"
fi
fi
done


5eb139 (8) No.3153

>>3152

also /bin/echo


da1732 (1) No.3155>>3165

>>3149 (OP)

>cron

>2015

>not using systemd timers


5eb139 (8) No.3165

>>3155

systemd sucks cock


85982d (3) No.3174>>3177

Use $* instead of "$@" when your arguments will not have spaces.

Instead of

! -f
in the if condition, use
-f
and swap the then and else blocks.

Use != instead of the ! == thing.

Use /bin/sh instead of bash if you can (and you absolutely can here).

Backticks are bad form, and aren't really nestable. Use proper command substitution.

You probably should not use absolute paths, instead opting to fix your cron environment so that PATH is set right, in case somebody has curl somewhere else.

getopts is always nice, and every program as a baseline should have some sort of usage message.

GNU indent style is gross.

This is a bit better (works in Linux with -c md5sum and -n notify-send, when they are installed. You may need to add your dbus export stuff to get notify-send to work right. You can add options to the notify-program, to eg. get notify-send to persist the notification with its own options):


#!/bin/sh

NOTIFY=say
CHECKSUM=md5
USAGE="$0 { -c [hash program] } { -n [notify program] }"

while getopts 'c:hn:' opt; do
case "$opt" in
c)
CHECKSUM="$OPTARG"
;;
n)
NOTIFY="$OPTARG"
;;
h)
echo "$USAGE"
exit 0
;;
?)
echo "$USAGE"
exit 1
;;
esac
done

shift $(($OPTIND - 1))

for board in $*; do
HASH="$(curl --silent http://8ch.net/$board/0.json | $CHECKSUM)"
TMPFILE="/tmp/${board}.hash"

if [ -f "$TMPFILE" ]; then
oldHash="$(cat $TMPFILE)"

if [ "x$oldHash" != "x$HASH" ]; then
echo $HASH > "$TMPFILE"
$NOTIFY "$board has a new post"
fi
else
echo "$HASH" > "$TMPFILE"
fi
done


5eb139 (8) No.3177>>3179

>>3174

Solid code review. Thanks bro.

Whats wrong with gnu indent style though? Im really curious; not being facetious.


85982d (3) No.3179

>>3177

The literal extent of my complaint is just that I don't like it. I like my opening and closing braces (or whatever grouping identifiers they are) to sit even with the scope in which they reside. I typically use Allman indent. I use indent level to easily identify scope exactly, and GNU style makes one scope change look like two to my brain.

Indent style never matters in the real world beyond the basic rule of "always use the style that the rest of the project uses."

And most of my notes pertain to portability. If it doesn't cost extra lines of code, extra functionality, or readability, always err toward portability (POSIX in this case).


5eb139 (8) No.3187>>3195

hey guys ive been using this code for a few days now and I found a bug.

when you close your laptop and open it, there is a few seconds without internet while you connect to your lan. if the script runs during these few seconds, then it will register the 'curl' as a fail and the md5 hash will be NULL and sent as an empty string to $TMPFILE. how can this bug be prevented?


85982d (3) No.3195>>3197

>>3187

Check the return value of curl. It's pretty easy even with command substitution.

Based on my last code snippet, replace the following line:


HASH="$(curl --silent http://8ch.net/$board/0.json | $CHECKSUM)"

With


HASH="$(curl --silent http://8ch.net/$board/0.json | $CHECKSUM)" || { $NOTIFY "Could not curl /$board/"; exit 1; }

Or if you're using different code, adjust it accordingly. A 0 exit code is success, and anything else is failure in shell. If you look at the curl manpage, you can see an exit status breakdown.

There are a lot of other ways to do it too, like using the special $? variable, which shows the exit status of the previous command (as a string, so you'll have to do a string comparison if you want to use it).


5eb139 (8) No.3197

>>3195

good idea using the double pipe. easy to implement and understand




[Return][Go to top][Catalog][Screencap][Update] ( Scroll to new posts) ( Auto) 5
11 replies | 0 images | 3 UIDs | Page ?
[Post a Reply]
[ / / / / / / / / / ] [ dir / ask / dcaco / fur / hypno / o / sl / ttgg / u ][ watchlist ]