Automatic weather tweets

Others (MiLight, Hue, Toon etc...)

Moderator: leecollings

User avatar
mbliek
Posts: 194
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Automatic weather tweets

Post by mbliek »

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

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"
Twitter script is from borrowed (removed by GDPR request)
Only added the weather part.
antwan
Posts: 116
Joined: Saturday 13 July 2013 13:28
Target OS: Linux
Domoticz version: Latest
Location: Hilversum, The Netherlands
Contact:

Re: Automatic weather tweets

Post by antwan »

Could you add this to the wiki?
Rasp Pi 2B: 3x DS18S20 (1-wire), BME280 (i2c)
Cubietruck 1: Aeotec Z-Wave+, RFXtrx433XL+P1, RFXtrx868, TE923 display, SolarEdge API
Cubietruck 2: Aeotec Z-Wave, Voltcraft CO-20
User avatar
mbliek
Posts: 194
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Automatic weather tweets

Post by mbliek »

Will do
User avatar
mbliek
Posts: 194
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Automatic weather tweets

Post by mbliek »

antwan
Posts: 116
Joined: Saturday 13 July 2013 13:28
Target OS: Linux
Domoticz version: Latest
Location: Hilversum, The Netherlands
Contact:

Re: Automatic weather tweets

Post by antwan »

Perfect!
Rasp Pi 2B: 3x DS18S20 (1-wire), BME280 (i2c)
Cubietruck 1: Aeotec Z-Wave+, RFXtrx433XL+P1, RFXtrx868, TE923 display, SolarEdge API
Cubietruck 2: Aeotec Z-Wave, Voltcraft CO-20
User avatar
mbliek
Posts: 194
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Automatic weather tweets

Post by mbliek »

Added wind to the script, wiki and first post are updated
User avatar
elythomaslumber
Posts: 56
Joined: Friday 12 July 2013 13:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Solingen; Germany
Contact:

Re: Automatic weather tweets

Post by elythomaslumber »

Hi,

unfortunately something will not work and maybe someone has an idea.
The script is running well with all the successfully messages as shown in the wiki.
But I cannot see any tweet. Then I changed the password in the script and that doesn't change anything. All the sucess messages again...

Regards

Hardy
pi@raspberrypi ~/domoticz/scripts $ ./tweet.sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1032 100 1032 0 0 14226 0 --:--:-- --:--:-- --:--:-- 19846
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1032 100 1032 0 0 14475 0 --:--:-- --:--:-- --:--:-- 19846
[+] Fetching twitter.com...
[+] Submitting the login form...
[+] Getting compose tweet page...
[+] Posting a new tweet: Temperatur in #Solingen: 3.5°C Humidity: 68%...
[+] Logging out...
pi@raspberrypi ~/domoticz/scripts $
User avatar
mbliek
Posts: 194
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Automatic weather tweets

Post by mbliek »

Are you using the correct username (singed up email address) and password?

Could you post your script here?
User avatar
elythomaslumber
Posts: 56
Joined: Friday 12 July 2013 13:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Solingen; Germany
Contact:

Re: Automatic weather tweets

Post by elythomaslumber »

Here's my code and yes the correct eMail and password (I tried with 2 accounts):

#

Code: Select all

!/bin/bash

thermo=`curl "http://192.168.1.18:8080/json.htm?type=devices&rid=24"`
echo $thermo > /home/pi/domoticz/scripts/weather-temp.txt
temperature=`cat /home/pi/domoticz/scripts/weather-temp.txt | awk -F: '{print $13, $14}' | awk '{print $3}' | sed 's/\"//g'`

hygro=`curl "http://192.168.1.18:8080/json.htm?type=devices&rid=24"`
echo $hygro > /home/pi/domoticz/scripts/weather-temp.txt
hygro=`cat /home/pi/domoticz/scripts/weather-temp.txt | awk -F: '{print $14, $15}' | awk '{print $3}' | sed 's/\"//g'`

#rain=`curl "http://api_username:api_password@domoticz_server:domoticz_port/json.htm?type=devices&rid=your_id"`
#echo $rain > /home/pi/domoticz/scripts/weather-temp.txt
#rain=`cat /home/pi/domoticz/scripts/weather-temp.txt | awk -F: '{print $24, $25}' | awk '{print $3}' | sed 's/\"//g' | sed 's/,//g'`

#wind=`curl "http://api_username:api_password@domoticz_server:domoticz_port/json.htm?type=devices&rid=your_id"`
#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="Temperatur in #Solingen: $temperature°C Humidity: $hygro%"          #change to what you want but keep "$hygro"

#rm /home/pi/domoticz/scripts/weather.txt            #will remove the weather.txt file

#REQUIRED PARAMS
username="myemailadress"     #fill in your twitter e-mail address
password="mypass"      #fill in your twitter password
tweet="$tweet" #must be less than 140 chars

#EXTRA OPTIONS
uagent="Mozilla/5.0" #user agent (fake a browser)
sleeptime=2 #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 "/home/pi/domoticz/scripts/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 "/home/pi/domoticz/scripts/cookie.txt"
User avatar
elythomaslumber
Posts: 56
Joined: Friday 12 July 2013 13:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Solingen; Germany
Contact:

Re: Automatic weather tweets

Post by elythomaslumber »

cookie: (some values are changed by me for secirity reason:

#

Code: Select all

 Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

mobile.twitter.com	FALSE	/	FALSE	1422641919	d	32
mobile.twitter.com	FALSE	/	FALSE	1393697919	req_country	Germany
mobile.twitter.com	FALSE	/	FALSE	1393697919	req_country_code	DE
mobile.twitter.com	FALSE	/	FALSE	1393697919	req_ip	xx.yyy.ccc.aaa
mobile.twitter.com	FALSE	/	FALSE	1394939058	_mobile_sess	BAh7BjoLcmVwX2lkSSIpZGRhODcwYTAtNmMwOC0wMTMxLTBlNmUtMDA4Y2Zh%0AMDI3ODQ4BjoGRVQuuuuuu--15f798496316149c133f019242c54083rrrrrrrr
.twitter.com	TRUE	/	FALSE	1454177919	guest_id	v1%3A139110591931bbbbbb
mobile.twitter.com	FALSE	/	FALSE	1	oauth_token	
mobile.twitter.com	FALSE	/	FALSE	1	u	
mobile.twitter.com	FALSE	/	FALSE	1	m5_csrf_tkn	
mobile.twitter.com	FALSE	/	FALSE	1	html5	
User avatar
mbliek
Posts: 194
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Automatic weather tweets

Post by mbliek »

Just copied and paste your code and filled in my credentials.
I just got a tweet on my twitter page, so the script must be correct.
User avatar
mbliek
Posts: 194
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Automatic weather tweets

Post by mbliek »

User avatar
elythomaslumber
Posts: 56
Joined: Friday 12 July 2013 13:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Solingen; Germany
Contact:

Re: Automatic weather tweets

Post by elythomaslumber »

Thank for feedback!

Is there maybe any additional debian package necessary on my Raspi?

and...can you check your cookie?...
User avatar
mbliek
Posts: 194
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Automatic weather tweets

Post by mbliek »

try to add -k in the curl command when the script is trying to login to twitter etc.
Do it for every step.

curl -k -s -b "cookie.txt" -c "cookie.txt"
User avatar
elythomaslumber
Posts: 56
Joined: Friday 12 July 2013 13:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Solingen; Germany
Contact:

Re: Automatic weather tweets

Post by elythomaslumber »

No success...sorry.
Meanwhile I tried on a second Rapsi and the same again.

I will do the changes on the second and try again.
User avatar
mbliek
Posts: 194
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Automatic weather tweets

Post by mbliek »

Here is my cookie btw:

Code: Select all

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

mobile.twitter.com	FALSE	/	FALSE	1422644074	d	32
mobile.twitter.com	FALSE	/	FALSE	1393700074	req_country	The+Netherlands
mobile.twitter.com	FALSE	/	FALSE	1393700074	req_country_code	NL
mobile.twitter.com	FALSE	/	FALSE	1393700074	req_ip	00.000.000.000
mobile.twitter.com	FALSE	/	FALSE	1394939813	_mobile_sess	BAh7BjoLcmVwX2lkSSIpZTIyYjYyOTAtNmMwZC0wMTMxLTg3YjMtMTAxZjc0%0AMzuuuuuuuuuu--c87a32d95fadcca8bf11be963tttttttttttt
.twitter.com	TRUE	/	FALSE	1454180074	guest_id	v1%3A1391108yyyyyyyyyy
mobile.twitter.com	FALSE	/	FALSE	1	oauth_token	
mobile.twitter.com	FALSE	/	FALSE	1	u	
mobile.twitter.com	FALSE	/	FALSE	1	guest_timeline_users	
mobile.twitter.com	FALSE	/	FALSE	1	m5_csrf_tkn	
mobile.twitter.com	FALSE	/	FALSE	1	html5	
User avatar
elythomaslumber
Posts: 56
Joined: Friday 12 July 2013 13:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Solingen; Germany
Contact:

Re: Automatic weather tweets

Post by elythomaslumber »

OK, thanks. I tried with the -k also on my second Raspi without success.

The only difference I see in the cookie is the last line. Does not tell me anything but looks ok for me.

The strange thing is as I said is that using a wrong password does not have any effect on the messages I get in Putty...


Think for today it's enough.... I will try again tomorrow . Maybe someone else will give feedback if it works for him or has the same problems like me.

Thanks a lot!

Regards

Hardy
User avatar
elythomaslumber
Posts: 56
Joined: Friday 12 July 2013 13:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Solingen; Germany
Contact:

Re: Automatic weather tweets

Post by elythomaslumber »

Ok, did one more try:

I used the first URL from the script: https://mobile.twitter.com/session and I landed on a login page which was "locked" with "CAPTCHA" (We want to get sure that you are human...)
Could that be a problem? It's written in German language. Are the Twitter login pages ...mobile... different from those in the Netherlands and it's not possible to login from a script?
User avatar
mbliek
Posts: 194
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Automatic weather tweets

Post by mbliek »

I don't get CAPTCHA when going to that page. Just a login form.
User avatar
elythomaslumber
Posts: 56
Joined: Friday 12 July 2013 13:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Solingen; Germany
Contact:

Re: Automatic weather tweets

Post by elythomaslumber »

Then I think this is the problem...so no twittering by a machine in Germany :cry:

Thanks for your help!

regards

Hardy
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest