6 Mar 2011

Twitter OAuth Perl Module

Since Twitter changed their authentication method for using their API to OpenAuth, sending an automated tweet has become a whole lot more difficult.
In response to this I decided to code my own PERL module for OAuth.

Limitations of the module


Just before I post the code below I will mention that all this module currently does is allow you to post tweets, it does not handle retrieving access tokens or any other OAuth jazz. This module is really for single user twitter apps, like RSS feeds and twitter bots, or other situations where you already have all the required access tokens. If you register an app on twitter as a single access app, twitter will give you all the OAuth tokens you need. Point your browser at http://twitter.com/apps/new and sign in as your twitter bot or RSS feed posting account.

The Code


package Jimtweet;
#####JimTweet 0.1 By James Januszka 2010
#####Email:jimjanuszka@gmail.com
#####Twitter:@jamesjanuszka
use strict;
use warnings;
use LWP;
use HTTP::Headers;
use URI::Encode qw(uri_encode);
use URI::Escape qw(uri_escape);
use Digest::HMAC_SHA1;

####Constructor####
sub new {
 my $self={};
 $self->{OAUTH_VERSION}=uri_escape("1.0");
 $self->{OAUTH_SIGNATURE_METHOD}=uri_escape("HMAC-SHA1");
 $self->{OAUTH_TIMESTAMP}=undef;
 $self->{OAUTH_NONCE}=undef;
 $self->{AGENT}="jimtweet/0.1";
 $self->{URL}="http://api.twitter.com/1/statuses/update.xml";
 $self->{BROWSER}=LWP::UserAgent->new(agent =>$self->{AGENT});
 $self->{CONSUMER_KEY}=undef;
 $self->{CONSUMER_SECRET}=undef;
 $self->{OAUTH_TOKEN}=undef;
 $self->{OAUTH_TOKEN_SECRET}=undef;
 $self->{STATUS}=undef;
 bless($self);
 return $self;
} 

sub consumer_key{
 my $self=shift;
        if (@_) { $self->{CONSUMER_KEY}=uri_escape(shift) }
        return $self->{CONSUMER_KEY};
}
sub consumer_secret{
 my $self = shift;
        if (@_) { $self->{CONSUMER_SECRET}=uri_escape(shift) }
        return $self->{CONSUMER_SECRET};
}
sub oauth_token{
 my $self = shift;
        if (@_) { $self->{OAUTH_TOKEN}=uri_escape(shift) }
        return $self->{OAUTH_TOKEN};
}
sub oauth_token_secret{
 my $self = shift;
        if (@_) { $self->{OAUTH_TOKEN_SECRET}=uri_escape(shift) }
        return $self->{OAUTH_TOKEN_SECRET};
}

sub update_status(@){
 sleep(2);
 my $self = shift;
        if (@_) { $self->{STATUS}=uri_escape(shift) }
        my $seconds = time(); 
        $self->{OAUTH_TIMESTAMP}=uri_escape($seconds);
        $self->{OAUTH_NONCE}=$self->{OAUTH_TIMESTAMP};
       
       my $query=qq(oauth_consumer_key=$self->{CONSUMER_KEY}&oauth_nonce=$self->{OAUTH_NONCE}&oauth_signature_method=$self->{OAUTH_SIGNATURE_METHOD}&oauth_timestamp=$self->{OAUTH_TIMESTAMP}&oauth_token=$self->{OAUTH_TOKEN}&oauth_version=$self->{OAUTH_VERSION}&status=$self->{STATUS});
       
       my $sig="POST&";
$sig .=uri_encode($self->{URL},1);
$sig .="&";
$sig .=uri_encode($query,1);


my $sig_key=$self->{CONSUMER_SECRET};
$sig_key .="&";
$sig_key .=$self->{OAUTH_TOKEN_SECRET};

my $hmac = Digest::HMAC_SHA1->new($sig_key);
$hmac->add($sig);
my $oauth_signature_base64=$hmac->b64digest;
$oauth_signature_base64 .="=";
my $utf8_oauth_signature_base64=uri_escape($oauth_signature_base64);

my $debug="-v";
my $curlpost=qq(status=$self->{STATUS});

my $header=qq(OAuth oauth_nonce="$self->{OAUTH_NONCE}", oauth_signature_method="$self->{OAUTH_SIGNATURE_METHOD}", oauth_timestamp="$self->{OAUTH_TIMESTAMP}", oauth_consumer_key="$self->{CONSUMER_KEY}", oauth_token="$self->{OAUTH_TOKEN}", oauth_signature="$utf8_oauth_signature_base64", oauth_version="$self->{OAUTH_VERSION}");
my $req=HTTP::Request->new(POST => $self->{URL});
$req->content_type('application/x-www-form-urlencoded');
$req->content($curlpost);

$req->header('Authorization' => $header);  # set

my $res = $self->{BROWSER}->request($req);
return 1
}

####Footer####
1;  #so the require or use succeeds 


Usage


The first thing you need to do is include the module in your perl script, then you have to set up a new tweet object.

my $tweet=Jimtweet->new();

Next you have to specify your OAuth tokens

$tweet->consumer_key($consumer_key);
$tweet->consumer_secret($consumer_secret); 
$tweet->oauth_token($oauth_token);
$tweet->oauth_token_secret($oauth_token_secret);

Finally to send a tweet

$tweet->update_status($status);

The module sets up the nonce and timestamp for you as well as setting up the OAuth signature that many people have trouble with.

Well thats it I hope you all like it.

27 Mar 2009

Tutorial on Converting and Burning an AVI File onto DVD


This is going to be a quick tutorial on how to convert an avi movie so that it can be burned onto a DVD, all done via the terminal in Linux. For Extra credit I will also show you how you can to this on a remote computer.





For this you will need the following tools:



Optional: Setting up a remote connection

On my setup at home my Desktop PC has the tools and the avi on them, but my laptop is over by the comfy sofa. So what I do is ssh in to my PC from my laptop on the sofa. I also use a program called Screen.
This is an example of what I do.
screen ssh <ip of desktop of pc>

Convert the AVI into MPEG

As DVD films are more or less in the mpeg format and the film we want to burn is an avi. We need to convert it into a mpeg.
First we use the screen program this allows us to set a program running and then close the terminal window and the program continues to run.
screen

Now we need to use Tovid. This will convert the avi into the desired mpeg format. On my system a two hour film takes just over two hours to convert.
tovid -dvd -pal -in movie.avi -out movie
Don't forget this is going to take a long time.  I shall now explain the switches used.

The dvd switch basicly converts the avi into dvd screen resolution. The pal switch converts the avi in to the PAL format. Tovid defaults to NTSC. The in switch is the path to the avi, and the out switch is the prefix of the mpeg file we wish to create.

When this is done we will have a shiney new file called movie.mpg which we can convert into the DVD format.

Convert the mpeg into DVD file format

OK the last step took about two hours, but don't worry the next couple of steps are going to take 10 minutes tops.

Before we burn the mpeg file on to the DVD we need to create a DVD filesystem. We do this;
dvdauthor -t -v pal movie.mpg  -o movie/

The t switch gives the DVD a title. All DVD files need a tile so we give it a blank one. The v pal switch is just to make sure it remains in the PAL format. Then we have the name of the mpg file, finally the o switch is the path to the DVD filesytem.

Once we have done this we just run;
dvdauthor -T -o movie/

This just gives the the DVD a table of contents, which as far as I'm aware is just needed so we can burn the DVD.

Lets burn this baby to disc

Now we have the DVD filesytem on our PC we need to get it on to a coaster.
growisofs -dvd-video -Z /dev/dvd movie/
/dev/dvd is the path to your DVD writer and movie/ is the path to your movie DVD filesytem we created in the previous step.

In Summary

Well thats it we managed to burn an avi file onto a dvd just using the terminal, and we didn't even need to use a GUI. This process usually takes about 2.5 hours, so using screen comes in handy as we can just let it run in the background.

Now lets have all the commands we used in one place.
screen
tovid -dvd -pal -in movie.avi -out movie
dvdauthor -t -v pal movie.mpg  -o movie/
dvdauthor -T -o movie/
growisofs -dvd-video -Z /dev/dvd movie/

picture credit john_a_ward

6 Sept 2008

Getmail for twitter

Getmail a nice little Perl script that when setup with a cron job automatically scans your inbox on behalf of you or one of your twitter apps for emails from twitter and responds accordingly.

There are two types of email messages it responds to the first, is the email you get from twitter when someone sends you a direct message, the second is the one that twitter sends you when someone follows you.


How it works

The Getmail script works as an imap client and accesses the inbox of whichever twitter bot it is monitoring. It uses the Mail::IMAPClient perl module. Once it has successfully connected to the mail sever it selects a mail box, ie your inbox, or a specific mailbox of your choosing.

Once inside it looks for any recent mail. To put it another way it checks for any mail that has the imap RECENT flag attached to it. There is a slight problem with this method in that a message is only flagged as RECENT if no other imap clients have seen it, bare that in mind if you want to use this on your personal mail box, it will stop the script from seeing any mail.

When the script finds a message that is RECENT it checks the headers for the twitter custom headers (for example "X-Twitteremailtype"), and checks their value.

If the X-Twitteremailtype value is "is_following" then it calls a subroutine called follow. This subroutine doesn't do anything yet, it's up to you to fill it with commands. The most obvious would be to auto-follow.

If the  X-Twitteremailtype is "direct_message" then it calls an empty subroutine called directMsg, once again it's up to you the user of the script to define what that subroutine does.

Any other emails it finds in the inbox it just ignores, which is probably a good thing ;)


Possible Applications of this groovy script


You are free to do with this script what ever you wish, of course. This script will obviously be useful if you want to auto-follow people who follow you, or your twitter app. Some people may find use for the directMsg subroutine.

My reason for writting this script was so I co redesign echobot to auto-follow new users and to check direct messages without having to query twitter.


The Source Code

#!/usr/bin/perl

# Getmail.pl (For Twitter)
# By James Januszka, @jamesjanuszka, http://blog.jamesjanuszka.com
# Do as you will with this script but don't blame me if it f**k's up

use strict; # Spank me!
use warnings; # Stop or I'll shoot!
use Mail::IMAPClient;
my $host = "imap.smurfmail.net"; # Contact your email admin for this address
my $username = "pappa\@smurf.com"; # Don't forget to escape characters. Example \@
my $pass = "I <3 Smurfette"; # If you don't know this then, er ... Good Luck!
my $folder ="INBOX";# Which folder you want to look in. Example INBOX 
my $imap;

sub login{
 # login
  $imap = Mail::IMAPClient->new(
            Server => $host,
            User    => $username,
            Password=> $pass,
            )or die "Cannot connect to $host as $username: $@";
}

sub checkMail{
 # Check your mail
 $imap->select($folder) or die "Could not select: $@\n";
 my @recent = $imap->recent;# This checks for recent mail, if you check this mailbox with another client then run this script, it won't have any recent mail. Sorry thats the IMAP specification for you.
 foreach (@recent){
  my $messageType = $imap->get_header($_, "X-Twitteremailtype");
  my $messageFrom = $imap->get_header($_, "X-Twittersenderscreenname");
  my $messageString = $imap->body_string($_);
  if ($messageType eq "direct_message"){
   directMsg("$messageString");
  }
  if ($messageType eq "is_following"){
   follow($messageFrom);
  } 
 }
 
}

sub directMsg{
 # Do what you do with your direct messages here. 
 my @string = split(/\r/, "@_");# Split message email by returns, I used /r because /n was giving me problems
 print "$string[0] \n";
}

sub follow{
 # Do what you do when someone follows you. Ooh could this be a way of Auto-following someone ? ;)
 print "##########################FOLLOW @_#########################\n";
}


sub logout {
 #logout
 $imap->logout or die "Could not logout: $@\n";
}

# It's all very well writing pretty subroutines, but you need to remember to call them 
login;
checkMail;
logout;


picture credit gaetanlee
 

18 Jul 2008

Project Honeybot

Twitter is a great platform for expressing ideas and keeping up with what is going on in the world, but there is also a downside and thats spam. There seems to be alot of twitter users which are just spam bots they just keep following people hopeing to sell you viagra. I think I have found away to name and shame them.

Enter Honeybot

Honeybot is my counter defense to this, what it does is constantly posts to the public time line a warning not to follow it. Now I believe that spambots constantly scrape the public timeline and follow whoevers on there. So they should in theory follow Honeybot automaticly.

The second thing it does is keeps a record of its followers, allowing me to create a public blacklist of it's followers.

Creating a Blacklist of Spambots

What I intend to do is post the list of followers on my website where people can see if they are being followed by spammers. It's basicly a copy of the xml file of followers you can obtain from the twitter API, but I am going to parse it into a more readable format.

Source Code

below is a copy of the source code so you can see how it works:

#!/usr/bin/perl -w
## Honeybot - This bot attempts to attract spam bots and creates a spambot blacklist
## James Januszka, 2008, http://blog.jamesjanuszka.com
##
use strict;
use LWP;
use LWP::Simple;
use URI;
my $agent ='Honeybot/0.1';my $statusurl = 'http://twitter.com/statuses/update.xml';
my $followerurl = 'http://twitter.com/statuses/followers.xml?lite=true';
my $server ='twitter.com:80';
my $realm ="Twitter API";
my $username = "enteryourusername";
my $password ="enteryourpassword";
my $file="followers.xml";
my $string ="Please do not follow this bot as any followers will be added to the blacklist. See http://tinyurl.com/5bomg4 for more details.".int(rand(100000));
my $browser = LWP::UserAgent->new(agent =>$agent);
$browser->credentials($server,$realm,$username=>$password);
print "Content-type: text/html\n\n";

&updatestatus();
&getfollowers();

sub updatestatus {
my $response =$browser->post($statusurl,['status'=>$string]);
die "OMG, WTF: ",$response->header('WWW-Authenticate'), $response->status_line unless $response->is_success;
}

sub getfollowers {
system("wget --http-user=$username --http-passwd=$password -O $file -U $agent $followerurl");
}

I hope that this will be a useful service for twitter users, if you have any comments about how I can improve Honeybot let me know.

11 Jul 2008

Creative Commons and Flickr

Owl at Southend Airshow 2008 I have just opened an account at Flickr so I can upload some photos to it. One of the reasons I choose Flickr, is that you have the option of registering your images photos under a Creative Commons License(CC), instead of them being copyrighted by default.

Copyright v Creative Commons

Traditionally people have wanted to protect their ideas and artworks, this enables them to retain control of who does what with their property. Copyright works in this area, and is the norm in commercial circles. Nowadays people are writing blogs and uploading their photos to various social media websites. CC allows people to use other peoples media on their blogs, whilst still giving the owner of the media some rights on how their media is used.

Creative Commons Licenses

The thing with CC is it allows you to set various levels of restrictions on your work. Below is a brief summary of each license copy & pasted from http://creativecommons.org/about/license/.

Attribution (by)

This license lets others distribute, remix, tweak, and build upon your work, even commercially, as long as they credit you for the original creation. This is the most accommodating of licenses offered, in terms of what others can do with your works licensed under Attribution.

Attribution Share Alike (by-sa)
This license lets others remix, tweak, and build upon your work even for commercial reasons, as long as they credit you and license their new creations under the identical terms. This license is often compared to open source software licenses. All new works based on yours will carry the same license, so any derivatives will also allow commercial use.

Attribution No Derivatives (by-nd)
This license allows for redistribution, commercial and non-commercial, as long as it is passed along unchanged and in whole, with credit to you.

Attribution Non-commercial (by-nc)
This license lets others remix, tweak, and build upon your work non-commercially, and although their new works must also acknowledge you and be non-commercial, they don’t have to license their derivative works on the same terms.

Attribution Non-commercial Share Alike (by-nc-sa)
This license lets others remix, tweak, and build upon your work non-commercially, as long as they credit you and license their new creations under the identical terms. Others can download and redistribute your work just like the by-nc-nd license, but they can also translate, make remixes, and produce new stories based on your work. All new work based on yours will carry the same license, so any derivatives will also be non-commercial in nature.

Attribution Non-commercial No Derivatives (by-nc-nd)
This license is the most restrictive of our six main licenses, allowing redistribution. This license is often called the “free advertising” license because it allows others to download your works and share them with others as long as they mention you and link back to you, but they can’t change them in any way or use them commercially.

In summary I believe that if more people are aware of CC both media owners and media users, then this will have a positive effect as blog writers will use more good quality media in their blogs.

10 Jun 2008

TinyUrl Perl Script

It's been a while since my last post so I thought I'd write up a quick post about what I did today.

I've been using the TinyUrl service for some time now and I came across their API, so I though I'd knock up a quick 'n' dirty script to make my TinyUrling easier.

#!/usr/bin/perl -w
use strict;
use LWP;
my $input = $ARGV[0];
$input =~ s/^ht{2}p:\/{2}//;
my $url = 'http://tinyurl.com/api-create.php?url=http://'.$input;
my $browser = LWP::UserAgent->new;
my $response = $browser->get($url);
die "Can't get $url --", $response->status_line unless $response->is_success;
print $response->content."\n"


This script takes your input and prints out the TinyUrl of it, usage is as follows;
$ perl -w tinyurl.pl www.google.com

which gives an output of;
http://tinyurl.com/1c2

Not bad for about 5 minutes work, I hope you will agree.

7 May 2008

Echobot now living at Bobsbasement

I've had a word with my friend BOfH, and he has kindly agreed to host Echobot on his server at www.bobsbasement.co.uk. This now means that Echobot is available 24/7 and replies to it's messages every 10 minutes.

Now that Echobot has settled in, I can begin working on my other projects. Watch this space for project Tubeupdate and GMail Twitterfier.