scripts

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

fetchpic (999B)


      1 #!/bin/bash
      2 # Fetch a random pic from a random blog (of a given list).
      3 
      4 BASEURI="https://api.tumblr.com/v2/blog/"
      5 GETTER="wget -t1 -qO -"
      6 
      7 SOURCES=(
      8 	"desktopwallpaperproject.tumblr.com"
      9 	"wallpapermag.tumblr.com"
     10 	"dsgwallpaper.tumblr.com"
     11 	"parallax-wallpapers.com"
     12 	"aerialwallpapers.tumblr.com"
     13 	"dressupyourtech.tumblr.com"
     14 )
     15 
     16 die() {
     17 	echo "$@"
     18 	exit 1
     19 }
     20 
     21 info() {
     22 	blog="$1"
     23 	uri="${BASEURI}${blog}/info/?api_key=${APIKEY}"
     24 	$GETTER "$uri"
     25 }
     26 
     27 post() {
     28 	blog="$1"
     29 	offset="$2"
     30 	uri="${BASEURI}${blog}/posts/?api_key=${APIKEY}&offset=$offset&limit=1"
     31 	$GETTER "$uri"
     32 }
     33 
     34 rndblog() {
     35 	n=${#SOURCES[*]}
     36 	n="$(echo "${n}-1" | bc)"
     37 	n="$(shuf -i 0-"$n" -n1)"
     38 	echo "${SOURCES[$n]}"
     39 }
     40 
     41 main() {
     42 	[ -z "$APIKEY" ] && die "You need to set the APIKEY variable."
     43 	blog="$(rndblog)"
     44 	nposts="$(info "$blog" | grep -oE '"total_posts":([0-9]*)' |cut -d: -f2)"
     45 	n="$(shuf -i 1-"$nposts" -n1)"
     46 	pic="$(post "$blog" "$n" |grep -oE '"original_size":{"url":"([^"]*)' |cut -b25-)"
     47 	echo "$blog $pic"
     48 }
     49 
     50 main "$@"