#!/usr/bin/perl
# Chris Hubing - cjh@psu.edu
# 2/11/2007
# This script will update your twitter status
# rss part part by
# Creator: Stephen K. Anthony (http://stephenanthony.ca)
# Date: December 19, 2004 modified by brian greene 2007 for beta blogger
# glue by brian greene http://briangreene.com
# date 18/March 2007
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ get cmd line args - turned off
=for
if (!$ARGV[0]) {
print "Usage: $0 status message\n";
exit (-1);
}
=cut
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SETTINGS
#$status="@ARGV";
#$status="EIDW 181900Z 27021KT 9999 FEW010 FEW018CB 01/M01 Q0998 NOSIG_";
$login="TWITTER_USERNAME";
$password="TWITTER_PASSWORD";
$url="http://twitter.com/login";
require LWP::UserAgent;
require HTTP::Request;
use HTTP::Request::Common;
require Crypt::SSLeay;
use base 'HTTP::Message';
use HTTP::Cookies;
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Get the METAR DATA from XML
use LWP::UserAgent;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
my($ua2) = LWP::UserAgent->new;
# Accessing Atom/RSS feed
# http://weather.aero/dataserver/metars/MetarExamples.php
my($req) = HTTP::Request->new(GET => 'http://weather.aero/dataserver1_1/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=EIDW&hoursBeforeNow=1');
my($res) = $ua2->request($req);
my $xml = $res->content;
my $line=0;
# WX data looks like this EIDW 181900Z 27021KT 9999 FEW010 FEW018CB 01/M01 Q0998 NOSIG
while($xml =~ m/(.*?)<\/raw_text>/g) {
if($line lt 1){$status = "$1"; # just take the first one.
$line++;
}
}
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Go POST it at TWITTER
$ua = LWP::UserAgent->new;
# turn on cookies
$ua->cookie_jar( HTTP::Cookies->new(
'file' => 'cookies.lwp',
# where to read/write cookies
'autosave' => 1,
# save it to disk when done
));
# the response for the request
# do the auth POST to webaccess
$response = $ua->request(POST "$url", [ 'username_or_email'=>$login, 'password'=>$password, 'Commit'=>'Sign In']);
if ($response->is_redirect) {
# we must have auth'ed successfully
$header=$response->header("location");
if (($response->header("location")) eq "http://twitter.com/home") {
$url="http://twitter.com/status/update";
print "login successful\n";
$response = $ua->request(POST "$url", [ 'status'=>$status]);
if (($response->header("location")) eq "http://twitter.com/home") {
print "update successful\n";
#uncomment the next line if you want to see the response text
#print $response->content;
} else {
print "update failed\n";
exit(-1);
}
}
} else {
print "redirect failed - most likely password incorrect\n";
exit(-1);
}
exit(0);