[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]

/prog/ - Programming

Programming board

Catalog

8chan Bitcoin address: 1NpQaXqmCBji6gfX8UgaQEmEstvVY7U32C
The next generation of Infinity is here (discussion) (contribute)
A message from @CodeMonkeyZ, 2ch lead developer: "How Hiroyuki Nishimura will sell 4chan data"
Name
Email
Subject
Comment *
File
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Embed
(replaces files and can be used instead)
Options
Password (For file and post deletion.)

Allowed file types:jpg, jpeg, gif, png, webm, mp4, pdf
Max filesize is 8 MB.
Max image dimensions are 10000 x 10000.
You may upload 1 per post.


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

98f2e6 No.3149

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.

98f2e6 No.3151

Just toss it on crontab like this.

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


98f2e6 No.3152

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


98f2e6 No.3153

>>3152

also /bin/echo


780ef8 No.3155

>>3149

>cron

>2015

>not using systemd timers


98f2e6 No.3165

>>3155

systemd sucks cock


4b843b No.3174

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


98f2e6 No.3177

>>3174

Solid code review. Thanks bro.

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


4b843b 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).


98f2e6 No.3187

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?


4b843b No.3195

>>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).


98f2e6 No.3197

>>3195

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




[Return][Go to top][Catalog][Post a Reply]
Delete Post [ ]
[]
[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]