shell script: userfriendly.org comic strip viewer

IRIX/Nekoware development, porting and related topics.
Forum rules
Any posts concerning pirated software or offering to buy/sell/trade commercial software are subject to removal.
User avatar
jimmer
Donor
Donor
Posts: 482
Joined: Tue Oct 12, 2004 3:54 pm
Location: London, Ingerlund

shell script: userfriendly.org comic strip viewer

Unread postby jimmer » Sat Oct 21, 2017 8:35 pm

Hey All,

I threw this together just now for a little tutorial I'm giving tomorrow. Stuff like this has been done 10^6 times before, and better, but I thought i'd post it here anyway. It doesn't do any checking or sanitation of user input and it needs wget, but somebody might find it useful. Maybe.

Code: Select all

#!/bin/sh
#
# A primitive shell script to download and view the userfriendly.org comic strip

if [[ -n $1 ]]; then

   if [[ $1 == "today" ]]; then

      HTML="userfriendly-"`date +%Y%m%d`".html"

   else

      HTML="userfriendly-"$1".html"
   fi

   URL="http://ars.userfriendly.org/cartoons/?id="

   wget -q -O $HTML $URL$1;

   LINE=`cat $HTML | head -n 180 | tail -n 1"`

   SRC=`echo $LINE | awk '{ print $4 }' | awk -F'=' '{ print $2 }'`
   SRC="${SRC%\"}"
   SRC="${SRC#\"}"

   wget -q $SRC

   GIF=`echo $SRC | awk -F'/' '{ print $7 }'`

   imgview $GIF

   rm $HTML
   rm $GIF

else

   echo "Usage: userfriendly <some_date> (eg. 20050108 or 'today')";

fi
:Fuel: redbox 800Mhz 4Gb V12
:O2: bluebox 200Mhz 256Mb AV1+O2Cam

User avatar
duck
Donor
Donor
Posts: 742
Joined: Mon Oct 27, 2003 5:22 pm
Location: Jakobstad, Finland
Contact:

Re: shell script: userfriendly.org comic strip viewer

Unread postby duck » Sun Oct 22, 2017 12:48 am

I realize this is a throwaway script, but I can't help it...

awk is more than { print $1 }.

Scrape the URL from userfriendly's ancient markup:

Code: Select all

...

HTML=$(mktemp)

wget -q -O $HTML $URL # URL is defined from user input

SRC=$(awk '/ALT="Strip for/ { match($0, /src="[^"]*"/); url=substr($0, RSTART+5, RLENGTH-6); print url; }' < $HTML)

rm $HTML

GIF=$(mktemp);

wget -q -O $GIF $SRC

imgview $GIF

rm $GIF
...



Note: I just looked at the markup and wrote this down, whether it works or not is up to the reader to find out
Note2: gnu awk could make this neater, but IRIX does not ship with that
Note3: Use mkstemp if your platform supports it
:Octane: halo, octane Image knightrider, d i g i t a l AlphaPC164, pond, soekris net6501, misc cool stuff in a rack
N.B.: I tend to talk out of my ass. Do not take it too seriously.

User avatar
jimmer
Donor
Donor
Posts: 482
Joined: Tue Oct 12, 2004 3:54 pm
Location: London, Ingerlund

Re: shell script: userfriendly.org comic strip viewer

Unread postby jimmer » Sun Oct 22, 2017 1:02 am

Nice :) However, script will be shown to people who have never seen unix or 'code' before. was trying to keep it stupidly simple and thus pipes and single awk invocations seemed the 'right thing to do'.

:)

J
:Fuel: redbox 800Mhz 4Gb V12
:O2: bluebox 200Mhz 256Mb AV1+O2Cam

User avatar
duck
Donor
Donor
Posts: 742
Joined: Mon Oct 27, 2003 5:22 pm
Location: Jakobstad, Finland
Contact:

Re: shell script: userfriendly.org comic strip viewer

Unread postby duck » Sun Oct 22, 2017 1:38 am

Hmm, well... I don't know that I agree that you should teach antipatterns like using awk for only splitting lines just to keep it simple. I feel like I've seen too much of when people get taught one thing once and then goes on to blindly use it everywhere.

I'd argue that my more complex (not really) use of awk might even be more valuable because you CAN'T just copy paste it to pull the nth word out of a line without thinking about what it actually DOES. Anyone going wait, how the fuck does that work? would be introduced to piping in to commands (useless use of cat prevention), regular expressions, pattern-action syntax and of course that awk does more than just print $1;...

It also reduces the wall-of-code effect, which in my experience generally is a good thing for anyone dealing with code.

Am I on a thin branch sawing furiously? I don't know, I don't teach.
:Octane: halo, octane Image knightrider, d i g i t a l AlphaPC164, pond, soekris net6501, misc cool stuff in a rack
N.B.: I tend to talk out of my ass. Do not take it too seriously.

User avatar
jimmer
Donor
Donor
Posts: 482
Joined: Tue Oct 12, 2004 3:54 pm
Location: London, Ingerlund

Re: shell script: userfriendly.org comic strip viewer

Unread postby jimmer » Sun Oct 22, 2017 5:54 am

Fair enough - I'll show them both versions. I'll start with my braindead one and then show them yours and dive into 'pattern matching', 'there's more than one way to solve stuff', 'good and bad practice' and 'community'.

Should be a fun session :)
:Fuel: redbox 800Mhz 4Gb V12
:O2: bluebox 200Mhz 256Mb AV1+O2Cam

User avatar
duck
Donor
Donor
Posts: 742
Joined: Mon Oct 27, 2003 5:22 pm
Location: Jakobstad, Finland
Contact:

Re: shell script: userfriendly.org comic strip viewer

Unread postby duck » Sun Oct 22, 2017 6:25 am

Woho! I contributed! :-)
:Octane: halo, octane Image knightrider, d i g i t a l AlphaPC164, pond, soekris net6501, misc cool stuff in a rack
N.B.: I tend to talk out of my ass. Do not take it too seriously.

User avatar
jimmer
Donor
Donor
Posts: 482
Joined: Tue Oct 12, 2004 3:54 pm
Location: London, Ingerlund

Re: shell script: userfriendly.org comic strip viewer

Unread postby jimmer » Mon Oct 23, 2017 1:47 am

Went with this in the end, because:
1. IRIX only provides mktemp() as a C function, it's not directly available to /bin/sh
2. $() command substitution didn't work for me with /bin/sh, so stuck with ``

Code: Select all

#!/bin/sh

if [[ -n $1 ]]; then

   if [[ $1 == "today" ]]; then

      HTML="userfriendly-"`date +%Y%m%d`".html"

   else

      HTML="userfriendly-"$1".html"
   fi

   URL="http://ars.userfriendly.org/cartoons/?id="

   wget -q -O $HTML $URL$1;

   SRC=`awk '/ALT="Strip for/ { match($0, /src="[^"]*"/); url=substr($0, RSTART+5, RLENGTH-6); print url;}' < $HTML`

   wget -q $SRC

   GIF=`echo $SRC | awk -F'/' '{ print $7 }'`

   imgview $GIF

   rm $HTML
   rm $GIF

else

   echo "Usage: userfriendly <some_date> (eg. 20050108 or 'today')";

fi
:Fuel: redbox 800Mhz 4Gb V12
:O2: bluebox 200Mhz 256Mb AV1+O2Cam

User avatar
jirka
Donor
Donor
Posts: 210
Joined: Tue May 04, 2004 7:55 am
Location: Czech Republic
Contact:

Re: shell script: userfriendly.org comic strip viewer

Unread postby jirka » Mon Oct 23, 2017 11:14 am

The last version works for me (IRIX 6.5.27). Thanks!


Return to “SGI: Development”

Who is online

Users browsing this forum: No registered users and 1 guest