Created
May 1, 2017 12:28
-
-
Save lithiumhead/6ebd8e9a7b9025a0fbef22ecc660f961 to your computer and use it in GitHub Desktop.
Script to generate a screenshot of Elon Musk's latest tweet and set it as your background wallpaper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#sudo apt-get install jq imagemagick pngcrush nkf | |
#https://askubuntu.com/questions/403918/setting-cron-to-run-a-shell-script-random-wallpaper-from-a-webpage | |
# export DBUS_SESSION_BUS_ADDRESS environment variable | |
PID=$(pgrep gnome-session) | |
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-) | |
mkdir -p $HOME/Pictures/Elon_Tweets | |
PAGES2IMAGE_API_KEY="945ee343434e7358" | |
TWEET_JSON=$(bash $HOME/tweet.sh search -q "from:elonmusk" -c 1) | |
TWEET_ID_STR=$(echo $TWEET_JSON | jq .statuses[0].id_str | sed 's/"//g') | |
TWEET_URL="http://twitter.com/elonmusk/status/"$TWEET_ID_STR | |
echo "Latest Tweet ID is $TWEET_ID_STR" | |
logger "Latest Tweet ID is $TWEET_ID_STR" | |
if [ -e "$HOME/Pictures/Elon_Tweets/$TWEET_ID_STR.png" ] | |
then | |
echo "Scheenshot of tweet $TWEET_ID_STR exists, exiting.." | |
logger "Scheenshot of tweet $TWEET_ID_STR exists, exiting.." | |
else | |
PAGES2IMAGE_RESPONSE_JSON=$(curl -s -X GET -G \ | |
'http://api.page2images.com/restfullink' \ | |
-d p2i_device=6 \ | |
-d p2i_key=$PAGES2IMAGE_API_KEY \ | |
-d p2i_size=1024x768 \ | |
-d p2i_screen=1024x768 \ | |
-d p2i_url=$TWEET_URL) | |
PAGES2IMAGE_STATUS=$(echo $PAGES2IMAGE_RESPONSE_JSON | jq .status | sed 's/^.\(.*\).$/\1/') | |
if [ "$PAGES2IMAGE_STATUS" = "processing" ] | |
then | |
PAGES2IMAGE_WAIT_TIME=$(echo $PAGES2IMAGE_RESPONSE_JSON | jq .estimated_need_time) | |
echo "Screenshot job submitted for Tweet ID $TWEET_ID_STR, Wait Time: $PAGES2IMAGE_WAIT_TIME Seconds" | |
logger "Screenshot job submitted for Tweet ID $TWEET_ID_STR, Wait Time: $PAGES2IMAGE_WAIT_TIME Seconds" | |
elif [ "$PAGES2IMAGE_STATUS" = "finished" ] | |
then | |
PAGES2IMAGE_IMAGE_URL=$(echo $PAGES2IMAGE_RESPONSE_JSON | jq .image_url | sed 's/^.\(.*\).$/\1/') | |
echo "Screenshot job complete, fetching file $TWEET_ID_STR.png" | |
logger "Screenshot job complete, fetching file $TWEET_ID_STR.png" | |
## Download the Screenshot and save it in appropriate folder | |
wget -O $HOME/Pictures/Elon_Tweets/$TWEET_ID_STR.png $PAGES2IMAGE_IMAGE_URL | |
## Crop the Screenshot using ImageMagick | |
convert $HOME/Pictures/Elon_Tweets/$TWEET_ID_STR.png -crop 640x750+185+30 ~/Pictures/Elon_Tweets/$TWEET_ID_STR.png | |
## Set it as desktop background | |
gsettings set org.gnome.desktop.background picture-options 'centered' | |
gsettings set org.gnome.desktop.background picture-uri file://$HOME/Pictures/Elon_Tweets/$TWEET_ID_STR.png | |
else | |
echo "Screenshot job complete, Status = $PAGES2IMAGE_STATUS" | |
logger "Screenshot job complete, Status = $PAGES2IMAGE_STATUS" | |
echo $PAGES2IMAGE_RESPONSE_JSON | |
logger $PAGES2IMAGE_RESPONSE_JSON | |
fi | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment