dinsdag 28 april 2015

Twitter script for bash shell

For my Solget project I use some scripting I found on the internet. The original site does not exist anymore. However, I found out that I wasn't receive new tweets anymore. I did some homework and found the bugs. I also found out that the script could skip some code so I removed that.

Edit: since twitter made some changes again; script is also revised. (April 8 - 2015)
Edit2: another change; script revised (April 28 -2015)

Results:

#!/bin/bash

#REQUIRED PARAMS
username="TWITTER USERNAME"
password="TWITTER PASSWORD"
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
fi

if [ "$tweet" == "" ]; then
echo "[FAIL] Nothing to tweet. Enter your text as argument." && exit 1
fi

touch "cookie.txt" #create a temp. cookie file

#INITIAL PAGE
echo "[+] Fetching twitter.com..." && sleep $sleeptime
initpage=`curl -s -b "cookie.txt" -c "cookie.txt" -L --tlsv1 -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 --tlsv1 -A "$uagent" -d "authenticity_token=$token&username=$username&password=$password" "https://mobile.twitter.com/session"`

#TWEET
echo "[+] Posting a new tweet: ${tweet}..." && sleep $sleeptime
update=`curl -s -b "cookie.txt" -c "cookie.txt" -L --tlsv1 -A "$uagent" -d "authenticity_token=$token&tweet[text]=$tweet" "https://mobile.twitter.com/compose/tweet"`

#LOGOUT

echo "[+] Logging out..."
logout=`curl -s -b "cookie.txt" -c "cookie.txt" -L --tlsv1 -A "$uagent" "https://mobile.twitter.com/session/destroy"`

rm "cookie.txt"