Wednesday, April 4, 2007

Update GTD-PHP from GMAIL

This Script Gets Action Items from Gmail and creates them in GTD-PHP.

copy and paste the code below into a text file called gmail2gtdphp.pl
you will need perl plus the Mail::Webmail::Gmail and WWW::Mechanize modules installed.


##########################################################################
# Name: gmail2gtdphp.pl
# About: This script scans you gmail account for actions that are marked
# as requiring action. It then populates those actions into
# gtd-php. if the email has 2 labels eg TBA and ProjectX
# then the action will be placed in the ProjectX project.
# Note the way I do this I think it will only work if the
# email is still in the inbox. if you have 3 labels on a email
# I will just use one at random and will eb unreliable.
# If an email has no label it will be stored inthe default
# project specified below.
#Usage:
# You will need to have projects in GTD-PHP that match label
# names in GMAIL at least for the ones you are likely to have
# actions against. if you have a label that is not in gtd-php
# it will just fail with an error.
#
# When you run the script it will get every message labelled
# as requiring action and import them into GTD-PHP it will then
# remove the action require label (except that part
# of WebMail::Gmail seems to be broken).
# You need to set some variables below
#
# Author: Michael Mueller
#
# Licence:
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself

use Mail::Webmail::Gmail;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();

#Set these following Variables to your requirements
#set the url of the php-gtd installation
#eg. my $url='http://www.example.net/gtd-php/'

my $url = '' ;
#gmail userid;
my $gmailUID = '';
#gmail password
my $gmailPass = '';
#Email requiring Action Label.
my $TBA = 'TBA';
#Default Project if no other label but TBA is present
my $defaultLabel = 'GMAIL';
#Contexts are just set to one value.
my $context = 'Computer';

#Shouldn't need to change anything past here.

my $gmail = Mail::Webmail::Gmail->new(
username => $gmailUID, password => $gmailPass,
);

my @labels = $gmail->get_labels();

my $messages = $gmail->get_messages( label => $labels[0] );
my $messages = $gmail->get_messages( label => $TBA );
foreach ( @{ $messages } ) {

my $id = $_->{id};
submit ($_);
$gmail->edit_labels( label => $TBA, action => 'remove', msgid => $id );


}

sub submit {
my( $this) = @_;

my $subject = $this->{'subject'};

my @labels = grep (!/$TBA/,@{$_->{'labels'}});
my $project = $labels[1];
if (!$labels[1]){$project = $defaultLabel}
$context = $context;
my $description = $this->{'blurb'};
my $type;

$type = '/item.php?type=a';
$mech->get( $url.$type );
$mech->submit_form(
form_number => 1,
fields => {
title => $subject,
description => $description,
projectId => $project,
contextId => $context,


}
);
}

No comments: