Advertisement
chrisjrob

XKCD

Mar 21st, 2011
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.15 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # XKCD Desktop
  4. # © Christopher Roberts
  5.  
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10.  
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15.  
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19. usage()
  20. {
  21.   echo "Usage: $0 -r -v -w -u http://xkcd.com/ -d '~/.xkcd'";
  22.   echo " -r : Random"
  23.   echo " -v : Verbose"
  24.   echo " -w : Set as wallpaper using feh (e.g. openbox and pekwm)"
  25.   echo " -u : Set alternative URL"
  26.   echo " -d : Set working directory";
  27.   exit 1;
  28. }
  29.  
  30. # Set default options
  31. DIR='~/.xkcd'
  32. URL="http://m.xkcd.com/"
  33. VERBOSE="0"
  34. RANDOM="0"
  35. WALLPAPER="0"
  36.  
  37. while getopts "vwru:d:" opt; do
  38.   case "$opt" in
  39.     v) VERBOSE="1";;
  40.     w) WALLPAPER="1";;
  41.     r) RANDOM="1";;
  42.     u) URL="$OPTARG";;
  43.     d) DIR="$OPTARG";;
  44.     [?]) usage;;
  45.   esac
  46. done
  47.  
  48. if [ ! -d "$DIR" ]; then
  49.   echo "Directory $DIR does not exist"
  50.   exit
  51. fi
  52.  
  53. if [ "$RANDOM" = "1" ]; then
  54.   URL="http://dynamic.xkcd.com/random/mobile_comic/"
  55. fi
  56.  
  57. # I'm sure this could probably be halved in length somehow...
  58. MATCH=`curl -LSs "$URL" | grep -Po "src=\"http:\/\/imgs.xkcd.com\/comics\/.*\.png\" title=\".*?\"" | perl -pe 's/src=\"(http:\/\/imgs.xkcd.com\/comics\/.*\.png)\" title=\"([^"]*)\"/\1%\2/'`
  59. PNG=`echo "$MATCH" | cut -d "%" -f 1`
  60. TITLE=`echo "$MATCH" | cut -d "%" -f 2 | perl -pe 's/&#(\d+)\;/’/g' | fold -s`
  61.  
  62. if [ "$VERBOSE" = "1" ]; then
  63.   echo "$MATCH"
  64.   echo "$PNG"
  65.   echo "$TITLE"
  66. fi
  67.  
  68. wget -q "$PNG" -O "$DIR/xkcd-src.png" || exit;
  69. convert "$DIR/xkcd-src.png" -font "$DIR/Humor-Sans.ttf" -gravity Center -background Black -fill White label:"$TITLE" -gravity Center -append "$DIR/xkcd.png"
  70.  
  71. if [ "$WALLPAPER" = "1" ];then
  72.   feh --bg-center "$DIR/xkcd.png"
  73. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement