Automatic weather tweets
Posted: Wednesday 29 January 2014 22:56
Hi all,
Got since today a bash script that runs every hour to tweet the weather at my place.
https://twitter.com/HetWeerInHem
Wind speed and direction are now in the script.
Here's the script:
tweet.sh
Twitter script is from borrowed (removed by GDPR request)
Only added the weather part.
Got since today a bash script that runs every hour to tweet the weather at my place.
https://twitter.com/HetWeerInHem
Wind speed and direction are now in the script.
Here's the script:
tweet.sh
Code: Select all
#!/bin/bash
thermo=`curl "http://10.0.1.109:8080/json.htm?type=devices&rid=156"`
echo $thermo > /home/pi/domoticz/scripts/weather.txt
temperature=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $13, $14}' | awk '{print $3}' | sed 's/\"//g'`
hygro=`curl "http://10.0.1.109:8080/json.htm?type=devices&rid=156"`
echo $hygro > /home/pi/domoticz/scripts/weather.txt
hygro=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $14, $15}' | awk '{print $3}' | sed 's/\"//g'`
rain=`curl "http://10.0.1.109:8080/json.htm?type=devices&rid=122"`
echo $rain > /home/pi/domoticz/scripts/weather.txt
rain=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $24, $25}' | awk '{print $3}' | sed 's/\"//g' | sed 's/,//g'`
wind=`curl "http://10.0.1.109:8080/json.htm?type=devices&rid=384"`
echo $wind > /home/pi/domoticz/scripts/weather.txt
dir=`cat /home/pi/domoticz/scripts/temperatur.txt | awk -F: '{print $11, $12}' | awk '{print $3}' | sed 's/\"//g' | sed 's/,//g'`
speed=`cat /home/pi/domoticz/scripts/temperatur.txt | awk -F: '{print $24, $25}' | awk '{print $3}' | sed 's/\"//g' | sed 's/,//g'`
tweet="Temperatuur in #Hem: $temperature°C
Luchtvochtigheid: $hygro%
Neerslag: $rain mm
Wind: $dir / $speed km/h"
rm /home/pi/domoticz/scripts/weather.txt
#REQUIRED PARAMS
username="[email protected]"
password="mbliek1986"
tweet="$tweet" #must be less than 140 chars
#EXTRA OPTIONS
uagent="Mozilla/5.0" #user agent (fake a browser)
sleeptime=0 #add pause between requests
if [ $(echo "$tweet" | wc -c) -gt 140 ]; then
echo "[FAIL] Tweet must not be longer than 140 chars!" && exit 1
elif [ "$tweet" == "" ]; then
echo "[FAIL] Nothing to tweet. Enter your text as argument." && exit 1
fi
touch "cookie.txt" #create a temp. cookie file
#GRAB LOGIN TOKENS
echo "[+] Fetching twitter.com..." && sleep $sleeptime
initpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/session/new")
token=$(echo "$initpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//')
#LOGIN
echo "[+] Submitting the login form..." && sleep $sleeptime
loginpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$token&username=$username&password=$password" "https://mobile.twitter.com/session")
#GRAB COMPOSE TWEET TOKENS
echo "[+] Getting compose tweet page..." && sleep $sleeptime
composepage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L -A "$uagent" "https://mobile.twitter.com/compose/tweet")
#TWEET
echo "[+] Posting a new tweet: $tweet..." && sleep $sleeptime
tweettoken=$(echo "$composepage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1)
update=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$tweettoken&tweet[text]=$tweet&tweet[display_coordinates]=false" "https://mobile.twitter.com/")
#GRAB LOGOUT TOKENS
logoutpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/account")
#LOGOUT
echo "[+] Logging out..." && sleep $sleeptime
logouttoken=$(echo "$logoutpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1)
logout=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$logouttoken" "https://mobile.twitter.com/session/destroy")
rm "cookie.txt"
Only added the weather part.