updateWallpaper.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. tags="scenic order:random width:1600"
  3. picdir=`realpath $(dirname "${BASH_SOURCE[0]}")`
  4. del="30m"
  5. sud=0
  6. lockfile=/tmp/updateWallpaper.lock
  7. kill_running=0
  8. for i in "$@"; do
  9. case $i in
  10. -s=*|--start-up-delay=*)
  11. sud="${i#*=}"
  12. shift
  13. ;;
  14. -d=*|--delay=*)
  15. del="${i#*=}"
  16. shift
  17. ;;
  18. -t=*|--tags=*)
  19. tags="${i#*=}"
  20. shift
  21. ;;
  22. -k|--kill*)
  23. kill_running=1
  24. ;;
  25. -h|--help*)
  26. echo "usage: $0 <options>"
  27. echo "Options:"
  28. echo " -s --start-up-delay=del Set time between start and first image request."
  29. echo " -d --delay=del Set delay between image requests."
  30. echo " -t --tags=tags The actual search string. Special fields allowed."
  31. echo " -k --kill Kill a previously running instance of the script."
  32. echo "Notes:"
  33. echo " All times need to be formatted for the sleep command."
  34. exit 1
  35. ;;
  36. *)
  37. # Unknown
  38. ;;
  39. esac
  40. done
  41. if [ -e "$lockfile" ]; then
  42. kill -0 $(cat $lockfile)
  43. if [ $? -eq 0 ]; then
  44. if [ $kill_running -eq 0 ]; then
  45. echo "Error: Lock file $lockfile exists!"
  46. echo " This most likely means another instance of the script is already running."
  47. echo " Use -k to kill the running process."
  48. exit 2;
  49. else
  50. echo "Warning: Killing old process..."
  51. kill $(cat $lockfile)
  52. fi
  53. fi
  54. fi
  55. echo $$ > "$lockfile"
  56. if [ "$sud" != "0" ]; then
  57. sleep "$sud";
  58. fi
  59. tags=`echo -n "$tags" | sed "s/ /%20/g"`
  60. while [ 0 ]; do
  61. json=`curl "https://konachan.com/post.json?tags=$tags&limit=1"`
  62. id=`echo $json | jq -r '.[0].id'`
  63. url=`echo $json | jq -r '.[0].file_url'`
  64. ext=`echo $url | sed "s/.*\.//g"`
  65. url="$url"
  66. local_url="$picdir/$id.$ext"
  67. echo "Updating wallpaper to $id..."
  68. if [ -f "$local_url" ]; then
  69. echo "Already downloaded."
  70. else
  71. echo "Downloading $url..."
  72. curl -o "$local_url" "$url"
  73. fi
  74. gsettings set org.gnome.desktop.background picture-uri "$local_url"
  75. sleep "$del"
  76. done