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.

6 May 2008

Echobot has moved

I finally got my web host, Servage, to host Echobot. Now Echobot checks it's messages 24 hours a day 7 days a week. The only problem is instead of checking it's messages every 10 minutes, it now checks them hourly on the hour. I feel Echobot doesn't need real-time interactivity(whoa, Management speak 2.0!) so this should not be a problem.

25 Apr 2008

Echobot is now in testing

Echobot is a bot that I have written for twitter. It was written in PERL script. The way it works is, when you send it a direct message from twitter, it sends that message back to you. It might seem like a strange thing to do, but it has its uses.

The main reason I wanted to create such a bot was that I wanted to send messages from my PC to my mobile phone, for example one day I was looking for the address of my nearest branch of a particular building society. When I found it on the internet I wanted to text that information to my phone, so when I got to London I knew what the address was. I'm sure there could be other uses for Echobot but we will have to wait and see.

How to use Echobot

In order to use Echobot you have to follow it on twitter, and receive notifications from it on your mobile. Then I have to get it to follow you (I have yet to find a way to make it auto-follow people). Once that is setup all you have to do is sent it a direct message and it will send you it back.

For example you would type this in the textbox on the twitter website

d echobot Hello how are you doing?
Currently Echobot checks its messages every ten minutes, and I am running it on my PC as a crontab. So Echobot is only awake whilst my PC is on.

Here is the link for Echobots profile on twitter, www.twitter.com/echobot.
I am currently setting up a home page for Echobot at www.jamesjanuszka.com/content/echobot Where you can also download the source code.

I have also posted the code below
#!/usr/bin/perl -w
#
# Echobot - This bot echos back any tweets you send it
#
# James Januszka, 2008, http://echobot.trinician.com/ and
# http://jims-tech.blogspot.com
#
#    THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
#    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#    DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS
#    BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
#    TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
#    ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
#    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#    POSSIBILITY OF SUCH DAMAGE.
#
use strict;
use LWP;
use URI;

my $agent ='Echobot/0.1'; # Useragent
my $statusurl = 'http://twitter.com/statuses/update.xml'; # Url for sending updates to
my $repliesurl='http://twitter.com/direct_messages.xml'; # Url for direct message inbox
my $deleteurl='http://twitter.com/direct_messages/destroy/'; # Url to delete messages
my $server ='twitter.com:80'; # Twitter's Server name and port number for HTTP authentication
my $realm ="Twitter API"; # Twitter's Realm name for HTTP authentication
my $username = ""; # Your bots username for Twitter
my $password =""; # You bots password for Twitter
my $browser = LWP::UserAgent->new(agent =>$agent);
$browser->credentials($server,$realm,$username=>$password);

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

sub getreplies{
my $response = $browser->get($repliesurl);
die "OMG, WTF: Can't get $repliesurl --", $response->status_line unless $response->is_success;
if ($response->content =~m//) {
my @out = split(//, $response->content);
foreach (@out){
my $string = $1 if ($_=~ /(.*?)<\/text>/s);
my $sender =$1 if ($_=~ /(.*?)<\/sender_screen_name>/s);
my $id=$1 if ($_=~/(.*?)<\/id>/s); 
$string = "d ".$sender." ".$string; 
&directmessage ("$string");
&deletemessage($id);
}
} else {print "No more messages. \n";}
}

sub deletemessage{
my ($message)=@_;
my $url=$deleteurl.$message.".xml"; # $deleteurl + message id number + .xml
my $response = $browser->get($url);
die "OMG, WTF: Can't get $url --", $response->status_line unless $response->is_success;
}

getreplies();

21 Apr 2008

Image Rotator 1.0

I was working on the TCE Website today and the white space between the "nav bar" and the "contact us" section was looking empty.

I thought it would be nice if visitors could have a random image each time they loaded a page, this would require a nice little PHP script I wrote called Image Rotator.

The way this works is thus, firstly you setup a directory containing a pool of images, then you give them the same filename with a key after it. For example image0.png, image1.png, image2.png ... The script then displays one of those images at random whenever it is executed.

Now for the source


// Image Rotator 1.0
// James Januszka 2008
// http://jims-tech.blogspot.com

// Variables
$dir="images"; // Directory containing images
$files = scandir($dir);
$string = "sidepic"; // Prefix of each image
$extention ="jpg"; // Image Filetype
$alt = "Closeup of Food"; // Alt text for the image
$width = 180; // Width of image in pixels
$height = 250; // Height of image in pixels

foreach ($files as $file) // Loop through each file in the directory
{

if ($file=ereg($string, $file)) // Regular expression to see if the filename contains $string
{
$j =$j+1;

}

}
$count = $j -1; // Ugly Hack; the rand() function generates a number between 0 and $count

srand(time());
$random = (rand()%$count);
$image="$dir/$string$random.$extention"; // Generates a path to the image(directory+prefix+random+extention)
echo("*img src='$image' alt='$alt' width=$width height=$height*"); //Displays image ;) (*I had to remove the
angle brackets from this line so blogger would display it!)


It may look complicated at first, but its really simple. At the top of the code I define the variables used in the script, most of these are do to with the rendering of HTML code that displays the images. Next it scans through the image directory and builds an array containing the images, finally it displays an image at random.

One of the cool things about the script is this routine;

foreach ($files as $file) // Loop through each file in the directory
{

if ($file=ereg($string, $file)) // Regular expression to see if the filename contains $string
{
$j =$j+1;

}

}

It scans through the directory and uses a regular expression to find and count how many files are prefixed with $string, ignoring all other files in the directory, it needs to know how many pictures there are for the pseudo-random generator to work.

The final part of this script, shown below, generates a pseudo-random number and uses that to pick and display an image.

srand(time());
$random = (rand()%$count);
$image="$dir/$string$random.$extention"; // Generates a path to the image(directory+prefix+random+extention)
echo("*img src='$image' alt='$alt' width=$width height=$height*"); //Displays image ;) (*I had to remove the
angle brackets from this line so blogger would display it!)

Well thats the script, you are free to use it as you will, and you can make any changes that you desire, just as long as you credit me.

17 Apr 2008

Candle Printer

I just came across an interesting product from China. Candle Printer Model UN-SO-M01. It's a printer that can print your own designs onto candles. It connects to your PC via a USB cable.

This product is really going to appeal to the arts and crafts crowd, currently there are no importers of this or similar products in the West. Spotting a gap in the market, I approached Sinounic with a view to importing a few samples to flog on Ebay.

The response I got was enthusiastic, they showed be some specs, and I must say I thought I had something pretty hot here. Everything was going fine until we came to the price, 1600USD per unit. Even if I negotiated it would still be ludicrous.

Oh well I reckon the big boys are going to pick this up, and by Christmas it will be in all those crappy gadget booklets you get in the Sunday papers.

17 Mar 2008

TCE Website

I've just been asked to create a website for my fiancee's fathers catering business. I've got the first draft up on my server. It can be seen at www.thecafeexperience.com . I am going to be using HTML for the pages, but they will be saved as PHP files. This should give be a bit of flexibility, allowing me to include dynamic content, as well as logging visitors.

Her father has asked me to include his menus in the website. This gives me an excuse to play with XML. What I will do is store the menus as XML files and I can use PHP, with CSS formating ;-) to generate the web page.

Well heres a screeny showing the layout so far.