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: Wednesday: light showers, Temp: 19C/10C, Wind: NNW 13mph, Humidity: 76%,

UV: low,

Daytime: 07:05WET-17:34WET
weatherlisbon: Tuesday: sunny, Temp: 20C/15C, Wind: NNW 10mph, Humidity: 62%,

UV: low,

Daytime: 07:04WET-17:35WET
weatherlisbon: Monday: sunny, Temp: 20C/11C, Wind: N 14mph, Humidity: 52%,

UV: low,

Daytime: 07:03WET-17:36WET
weatherlisbon: Sunday: sunny intervals, Temp: 23C/13C, Wind: NW 7mph, Humidity: 78%,

UV: low,

Daytime: 07:02WET-17:37WET
weatherlisbon: Saturday: sunny, Temp: 25C/15C, Wind: N 6mph, Humidity: 67%,

UV: low,

Daytime: 07:01WET-17:38WET
weatherlisbon: Friday: drizzle, Temp: 23C/14C, Wind: WNW 2mph, Humidity: 85%,

UV: low,

Daytime: 07:00WET-17:39WET
weatherlisbon: Wednesday: sunny, Temp: 23C/19C, Wind: SSE 14mph, Humidity: 60%,

UV: low,

Daytime: 06:58WET-17:42WET
weatherlisbon: Wednesday: sunny, Temp: 23C/19C, Wind: SSE 14mph, Humidity: 60%,

UV: low,

Daytime: 06:58WET-17:42WET
weatherlisbon: Tuesday: sunny, Temp: 23C/19C, Wind: SE 9mph, Humidity: 60%,

UV: low,

Daytime: 06:57WET-17:43WET
weatherlisbon: Monday: sunny, Temp: 24C/17C, Wind: ESE 6mph, Humidity: 69%,

UV: low,

Daytime: 06:56WET-17:44WET
weatherlisbon: Sunday: sunny, Temp: 24C/16C, Wind: NNE 5mph, Humidity: 72%,

UV: low,

Daytime: 06:55WET-17:46WET
weatherlisbon: Saturday: drizzle, Temp: 22C/13C, Wind: NE 4mph, Humidity: 91%,

UV: low,

Daytime: 07:54WEST-18:47WEST
weatherlisbon: Friday: sunny, Temp: 22C/15C, Wind: SW 5mph, Humidity: 78%,

UV: low,

Daytime: 07:53WEST-18:48WEST
weatherlisbon: Thursday: sunny, Temp: 20C/14C, Wind: WNW 13mph, Humidity: 62%, UV: low, Daytime: 07:52WEST-18:50WEST
weatherlisbon: Wednesday: light showers, Temp: 18C/15C, Wind: W 14mph, Humidity: 65%, UV: low, Daytime: 07:51WEST-18:51WEST
weatherlisbon: Tuesday: heavy showers, Temp: 18C/12C, Wind: SW 15mph, Humidity: 94%, UV: low, Daytime: 07:50WEST-18:52WEST
weatherlisbon: Monday: foggy, Temp: 22C/15C, Wind: SW 5mph, Humidity: 65%,

UV: low,

Daytime: 07:49WEST-18:54WEST
weatherlisbon: Sunday: sunny, Temp: 25C/5C, Wind: NNE 4mph, Humidity: 21%,

UV: low,

Daytime: 07:48WEST-18:55WEST
weatherlisbon: Saturday: sunny, Temp: 26C/10C, Wind: ENE 3mph, Humidity: 51%,

UV: moderate,

Daytime: 07:47WEST-18:57WEST
weatherlisbon: Friday: sunny, Temp: 28C/15C, Wind: E 9mph, Humidity: 32%,

UV: moderate,

Daytime: 07:46WEST-18:58WEST

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