scripts

shell scripts
git clone git://git.bitsmanent.org/scripts
Log | Files | Refs | README

scrapthumb (1741B)


      1 #!/bin/bash
      2 # Scrap Tumblr to get random images
      3 # Usage: feh --bg-fill --no-fehbg $(scrapthumb -run1) > /tmp/scrapthumb
      4 # TODO: handle network errors
      5 
      6 GETTER="wget -t1 -qO -"
      7 MAXIMGS=5
      8 RANDOMIZE=0
      9 WITHSOURCE=0
     10 
     11 # No trailing slash
     12 DEF_SOURCES=(
     13 	"http://tumblr.com"
     14 	"http://desktopwallpaperproject.tumblr.com"
     15 	"http://wallpapermag.tumblr.com"
     16 	"https://dsgwallpaper.tumblr.com"
     17 	"http://parallax-wallpapers.com"
     18 	"http://aerialwallpapers.tumblr.com"
     19 	"http://dressupyourtech.tumblr.com"
     20 	"http://allfullhdwallpapers.tumblr.com"
     21 )
     22 
     23 ##
     24 # Do not edit below.
     25 ##
     26 
     27 SOURCES=()
     28 IMGS=()
     29 SRCS=()
     30 
     31 preload() {
     32 	s=0
     33 	n=${#SOURCES[*]}
     34 	for i in $(seq 0 $(echo $MAXIMGS - 1|bc))
     35 	do
     36 		if [ $RANDOMIZE -ne 0 ]; then
     37 			s=$(echo ${RANDOM}%$n|bc)
     38 		else
     39 			s=$(echo ${RANDOM}%1|bc)
     40 		fi
     41 		src=${SOURCES[$s]}
     42 
     43 		SRCS[$i]="$src"
     44 		if [ "$src" = "http://tumblr.com" ]; then
     45 			IMGS[$i]="$($GETTER "$src" |grep -o 'id="fullscreen_post_image" src="[^ ]*"' |cut -b 32- |tr -d \")"
     46 		else
     47 			IMGS[$i]="$($GETTER "$src/random" |grep -o 'property="og:image" content="[^ ]*"' |cut -b 29- |tr -d \")"
     48 		fi
     49 	done
     50 }
     51 
     52 usage() {
     53 echo -n "Usage: $0 [-hr] [-n <num>] [-s <source>] ...
     54 -s 	Source(s) (can be specified multiple times)
     55 -n	Number of items
     56 -u	Also output the source (fmt: source|url)
     57 -r	Randomize sources
     58 -h	Show this help
     59 "
     60 
     61 exit 1
     62 }
     63 
     64 main() {
     65 	while getopts "hn:s:ru" opt; do
     66 		case $opt in
     67 			h) usage;;
     68 			n) MAXIMGS=$OPTARG;;
     69 			s)
     70 				i=${#SOURCES[*]}
     71 				SOURCES[$i]=$OPTARG;;
     72 			r) RANDOMIZE=1;;
     73 			u) WITHSOURCE=1;;
     74 		esac
     75 	done
     76 
     77 	[ ${#SOURCES[@]} -eq 0 ] && SOURCES=(${DEF_SOURCES[@]})
     78 	preload
     79 
     80 	for i in $(seq 0 $(echo ${#IMGS[*]} - 1|bc))
     81 	do
     82 		if [ $WITHSOURCE -eq 1 ]; then
     83 			echo -n "${SRCS[$i]}|"
     84 		fi
     85 
     86 		echo "${IMGS[$i]}"
     87 	done
     88 }
     89 
     90 main "$@"