Twitter

From Mohidwiki

Jump to: navigation, search

Twitter is a social short messaging system from the web to the outside world. It can be used to send messages to friends. A practical utility is to have a bot-twitter-user that broadcasts messages from scheduled scripts. It could confirm the status of an ongoing process or job with an sms.

Contents

Using twitter with curl

curl -u mohid1:***** -d status="Having fun with curl" http://twitter.com/statuses/update.xml

Mohid Twitter project

Make sure you have accessible from your system path the following GNU utilities:

  1. tee
  2. grep
  3. tail
  4. curl

Save this code as a batchfile:

REM MohidTwitter.bat
REM Usage syntax:
REM > MohidWater.exe | tee Run1.log
REM > Call MohidTwitter.bat Run1.log
@echo off

REM Please edit the username and password value
set username=MyUsername
set password=MyPass

type %1 | grep MOHID | grep "[^-]$" | tail -n 1 > _tmp.txt
set /P mess=<_tmp.txt
echo %mess%
curl -u %username%:%password% -d status="%mess%" "http://twitter.com/statuses/update.xml"
del _tmp.txt

@echo on

Bot agents

Maretec agents

Channel Title: Twitter / weatherlisbon
weatherlisbon: Wed: Sunny, Temp: 27C/18C, Wind: w 13km/h, Humidity: 46%, UV risk [1-11]: 5, Daytime: 07:05/20:06
weatherlisbon: Tue: Sunny, Temp: 27C/18C, Wind: ssw 21km/h, Humidity: 51%, UV risk [1-11]: 6, Daytime: 07:05/20:08
weatherlisbon: Mon: Sunny, Temp: 38C/19C, Wind: n 5km/h, Humidity: 18%, UV risk [1-11]: 7, Daytime: 07:04/20:09
weatherlisbon: Sun: Sunny, Temp: 35C/17C, Wind: nnw 16km/h, Humidity: 33%, UV risk [1-11]: 7, Daytime: 07:03/20:11
weatherlisbon: Sat: Sunny, Temp: 30C/17C, Wind: nnw 23km/h, Humidity: 38%, UV risk [1-11]: 5, Daytime: 07:02/20:12
weatherlisbon: Fri: Sunny, Temp: 31C/16C, Wind: nnw 16km/h, Humidity: 34%, UV risk [1-11]: 6, Daytime: 07:01/20:14
weatherlisbon: Thu: Sunny, Temp: 30C/19C, Wind: w 11km/h, Humidity: 42%, UV risk [1-11]: 6, Daytime: 07:00/20:15
weatherlisbon: Wed: Sunny, Temp: 35C/18C, Wind: nnw 6km/h, Humidity: 35%, UV risk [1-11]: 8, Daytime: 06:59/20:17
weatherlisbon: Tue: Sunny, Temp: 30C/16C, Wind: nnw 14km/h, Humidity: 42%, UV risk [1-11]: 8, Daytime: 06:58/20:18
weatherlisbon: Mon: Sunny Intervals, Temp: 24C/19C, Wind: w 13km/h, Humidity: 64%, UV risk [1-11]: 5, Daytime: 06:57/20:20
weatherlisbon: Sun: Sunny, Temp: 28C/16C, Wind: nnw 18km/h, Humidity: 47%, UV risk [1-11]: 4, Daytime: 06:57/20:21
weatherlisbon: Sat: Sunny, Temp: 32C/17C, Wind: w 6km/h, Humidity: 37%, UV risk [1-11]: 7, Daytime: 06:56/20:23
weatherlisbon: Fri: Sunny, Temp: 34C/18C, Wind: n 11km/h, Humidity: 36%, UV risk [1-11]: 4, Daytime: 06:55/20:24
weatherlisbon: Thu: Sunny, Temp: 28C/17C, Wind: nnw 21km/h, Humidity: 51%, UV risk [1-11]: 6, Daytime: 06:54/20:25
weatherlisbon: Tue: Sunny, Temp: 32C/17C, Wind: nw 10km/h, Humidity: 38%, UV risk [1-11]: 6, Daytime: 06:52/20:28
weatherlisbon: Mon: Sunny, Temp: 30C/17C, Wind: nw 14km/h, Humidity: 39%, UV risk [1-11]: 7, Daytime: 06:51/20:29
weatherlisbon: Sun: Sunny, Temp: 31C/16C, Wind: wnw 10km/h, Humidity: 31%, UV risk [1-11]: 8, Daytime: 06:50/20:31
weatherlisbon: Sat: Sunny, Temp: 34C/20C, Wind: ne 10km/h, Humidity: 26%, UV risk [1-11]: 8, Daytime: 06:49/20:32
weatherlisbon: Fri: Sunny, Temp: 32C/18C, Wind: n 24km/h, Humidity: 33%, UV risk [1-11]: 8, Daytime: 06:48/20:33
weatherlisbon: Thu: Sunny, Temp: 26C/17C, Wind: nnw 31km/h, Humidity: 48%, UV risk [1-11]: 5, Daytime: 06:48/20:34

Twitter bot script

bash example

Here's a sample bash script that uses curl to extract a weather forecast from an rss feed and twit it:

[bash,Y]

#! /bin/sh

#This line is important if you are to put this script as a cronjob
#Goto the folder where the bash script exists.
here=/home/[user]/scripts
cd $here

#BBC Lisbon weather id
id=0048

#BBC weather RSS feed address
feed="http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/${id}.xml"

#City
city=lisbon

#temporary file
file="weather${city}.txt"

#Weather twitter bot
twitbot=weatherlisbon:w*****r

#Read the RSS feed and filter it
curl $feed 2>&1 | grep 'title' | tail -n 1 | perl -wlne'm/title>(.*)<\/title/i && print $1' | 
sed -e  "s/\&\#xB0;//g" > $file
# use this line instead, to filter out the Fahrenheit degrees, as well as the leading "the forecast for"
#sed -e "s/\&\#xB0;//g" | sed -e "s/ [\(][0-9]*F[\)]//g" | sed -e "s/space:\+/ /g" | cut -d ' ' -f4-21 > $file

#Read the forecast into a weather variable
read weather < $file

#Twit the weather variable away
curl --basic --user $twitbot --data status="$weather" http://twitter.com/statuses/update.xml

Batch example

Here's the marvin_bot agent script in batch. However, previously, you are required to have cygwin installed and the %CYGWIN%\bin path added to your %PATH% environment variable.

@ECHO off
REM MARVIN BOT script

REM Goto here
set here="D:\Users\Guillaume\guillaume\biscates\bots"
cd %here%

REM Marvin feed address
set feed="marvinquotes_srtd.txt"

REM temporary file
set file="marvin_bot"

REM Marvin twitter bot
set user=marvin_bot
set pass=****

REM Read the feed and randomize it
cat %feed% | shuf | head -n 1 > %file%.txt

REM Read the message into a weather variable
set /P mess=<%file%.txt
echo %mess% >> %file%.log

REM Timestamp the log file
echo -------------------------------------------->> %file%.log
echo %DATE% %TIME% >> %file%.log

REM Twit the weather variable away
wget --http-user=%user% --http-pass=%pass% --post-data="status=%mess%" -O %file%.log http:// twitter.com/statuses/update.xml
@ECHO on

Check if message is new

This code in bash or batch checks for new messages to be twittered.

bash

touch "mess.log"
if [ `diff mess.tmp mess.log | grep -e "^<" | wc -l` -eq 1 ]
then

 #Uncomment one the following couple of lines
 #cat mess.tmp >> mess.log #keeps historical record of all messages
 #cat mess.tmp > mess.log #keeps only last message

 #...

fi

batch

NOTE: you need to find windows Xp binaries of grep, diff, sed, (g)awk, tail, head, cat, tac, sort and install them in your PATH.

touch "mess.old"
diff mess.tmp mess.old | grep -e "^<" | wc -l > diff.tmp
set /P diff=<diff.tmp
if %diff% neq 1 goto end
goto mess

:mess
REM >> keeps historical record
REM > keeps last mess
REM Uncomment one the following couple of lines
REM type mess.tmp>>mess.old
REM type mess.tmp>mess.old

:end

Sample food

> cat Novembro_SAASUTL.htm | \
perl -ne 'm/>([^<>]*?)<\// && print$1."\n"' | \
sed -f sasutl.sed -e '/^$/d' | \
awk '/29-11-2007/,/30-11-2007/'
  1. Get http://www.sas.utl.pt/alim/Ementas/View/View.aspx to \\guillaume\temp
  2. Transfer via winscp to point.pt -p 2996

Status update

External References

Personal tools