Skip to content

Instantly share code, notes, and snippets.

@bojanbabic
Created May 24, 2010 07:52
Show Gist options
  • Save bojanbabic/411635 to your computer and use it in GitHub Desktop.
Save bojanbabic/411635 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
#author Bojan Babic
#email:gbabun@gmail.com
use Time::HiRes qw /gettimeofday/;
use LWP::UserAgent;
use LWP::Simple;
use HTTP::Request;
use HTTP::Response;
use strict;
use XML::Simple;
use Data::Dumper;
my $ua=new LWP::UserAgent;
$ua->agent('Mozilla/5.0');
my $gruvr_feed_url="http://gruvr.com/feed/?geo=";
#$location="Athens,%20\(null\)";
my $location="Athens,GR";
my $feed_content="";
my $g_proxy="";
my $g_email='testEmail@gmail.com';
my $g_pass='testPasswd';
my $g_service='cl';
my $g_clientLogin_url='https://www.google.com/accounts/ClientLogin';
my $g_source='soultions-gcMashup-1';
my $g_proxy_string_curl='';
my $g_Authentication=();
my $g_full_calendar='http://www.google.com/calendar/feeds/default/allcalendars/full';
my $g_owned_calendar='http://www.google.com/calendar/feeds/default/owncalendars/full';
my $g_default_calendar='http://www.google.com/calendar/feeds/default/private/full';
my $xmlParser = new XML::Simple;
my $feed_file_name='gruvr'.gettimeofday().'.xml';
my $feed_size=0;
my $feed_content=();
my @items;
my $g_all_calendar_data=();
my @g_calendar_list=();
my $g_target_calendar_id=1;
my @error_list = ();
my $MISSING_CALENDAR=0;
my $FEED_CONNECT_ERROR=2;
my $target_calendar = ();
my $target_id = ();
my $calendar_file='new_events.xml';
my $calendar_file_cp='new_events_cp.xml';
my $calendar_entries=();
#authenticate on google API
sub gc_ClientLoggin_curl{
if ($g_email eq "" || $g_pass eq ""){
print "Correct user data need to be set!\n";
return;
}
my $result=`curl $g_proxy_string_curl -d Email=$g_email -d Passwd=$g_pass -d source=$g_source -d service=$g_service $g_clientLogin_url`;
($g_Authentication)=($result=~ m/(Auth\=.*)/);
#print "Local value:\n".$g_Authentication."\n";
}
#get feed data for location
sub get_feed_gruvr{
my $url=$gruvr_feed_url.$location;
print "\nTrying to connect to:".$url."\n";
#$req=HTTP::Request->new(GET => $url);
my $req=new HTTP::Request GET => $url;
my $response=$ua->request($req);
if ($response->is_error()){
print "\nCan't connect ".$response->status_line."\n";
push(@error_list,$FEED_CONNECT_ERROR);
exit;
}
$feed_content=$response->content();
writeFeedToFile($feed_content);
}
sub manipulateFeedData{
my $data=$xmlParser->XMLin($feed_file_name);
# print Dumper($data);
print $data->{channel}->{item}[0]->{title};
#@items=$data->{channel}->{item};
foreach my $child (@ {$data->{channel}->{item}}){
print $child->{title}."\n";
my $title = $child->{title};
my $date = $child->{pubDate};
if ( $calendar_entries !~ m/$title/ ){
add_quick_entry($title);
#add_gc_entry($title,$date);
}
}
}
sub retrieve_calendars{
my $gc_feed=shift();
my $res=`curl --silent --header "Authorization: GoogleLogin $g_Authentication" $g_owned_calendar $g_proxy_string_curl`;
#print $res;
if ($res=~ m/http/ ){
#redirect
print "Redirecting to:";
my ($redirect_url)=($res =~ m/(http.*)\"/ );
print $redirect_url;
my $new_res=`curl --header "Authorization: GoogleLogin $g_Authentication" $redirect_url $g_proxy_string_curl`;
#print "\n$new_res\n";
$g_all_calendar_data=$xmlParser->XMLin($new_res);
}else {
print "Unexpected behavior!\n";
}
}
sub init_quick_entries{
$calendar_entries.="<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gCal='http://schemas.google.com/gCal/2005'>\n";
}
sub close_quick_entries{
$calendar_entries.="</entry>\n";
}
sub add_quick_entry{
my $content=shift;
$calendar_entries.="<content type=\"html\">".$content."</content>\n";
$calendar_entries.="<gCal:quickadd value=\"true\"/>\n";
}
sub add_gc_event{
my $title=shift;
my $date=shift;
}
sub write_entries_to_file{
open(CALENDAR_ENTRY,">$calendar_file");
print CALENDAR_ENTRY $calendar_entries ;
close(CALENDAR_ENTRY);
}
sub extract_calendars{
#print Dumper($g_all_calendars);
foreach my $key (keys %{$g_all_calendar_data->{entry}}){
#print "Calendars:$key\n";
push(@g_calendar_list,$key);
}
}
sub get_target_calendar{
my $size=@g_calendar_list=@g_calendar_list;
if ($size == 0){
print "ERROR CODE:0:missing target calendar";
push(@error_list,$MISSING_CALENDAR);
return;
}
$target_calendar=@g_calendar_list[$g_target_calendar_id];
($target_id)=($target_calendar =~ m/.*\/(.*)/);
}
sub put_entries{
my $target_calendar='http://www.google.com/calendar/feeds/default/private/full';
my $res=`curl --request POST --data '\@$calendar_file' --silent --header "Content-Type: application/atom+xml" --header "Authorization: GoogleLogin $g_Authentication" $target_calendar $g_proxy_string_curl`;
#print $res;
if ($res=~ m/http/ ){
#redirect
print "Redirecting during posting calendar entries to:";
my ($redirect_url)=($res =~ m/(http.*)\"/ );
print "$redirect_url\n\n";
my $new_res=`curl --request POST --data '\@$calendar_file' --silent --header "Content-Type: application/atom+xml" --header "Authorization: GoogleLogin $g_Authentication" $redirect_url $g_proxy_string_curl`;
#print "\n$new_res\n";
print "$new_res\n";
}else {
print "Unexpected behavior!\n";
print $res;
}
}
sub writeFeedToFile{
my $content=shift;
open ( FEED_HANDLE, ">$feed_file_name");
print FEED_HANDLE $content;
close FEED_HANDLE;
}
#main
if ($#ARGV +1 == 0){
print "using no proxy...\n";
$g_proxy_string_curl = '';
} elsif ($#ARGV +1 == 1 && $ARGV[0] eq 'no_proxy'){
print "using no proxy...\n";
$g_proxy_string_curl = '';
} elsif ($#ARGV +1 == 1 ) {
print "using proxy:$ARGV[0]\n";
$g_proxy = $ARGV[0];
$g_proxy_string_curl = "-x $ARGV[0]";
$ua->proxy(['http']=>"$g_proxy");
} else {
print "Wrong number of params.\n";
usage();
exit;
}
#usage
sub usage{
print "usage: $0 (no_proxy|\$real_proxy_address)?\n";
print "where \$real_proxy_address is your company or prefered proxy\n\n";
}
#authenticate on Google
gc_ClientLoggin_curl;
if ($g_Authentication !~ m/Auth/ ){
print "Cant continue if not Authorized on Google Account;\n";
exit;
}else {
$g_Authentication=~ s/Auth/auth/;
}
#print "\n\n".$g_Authentication."\n\n";
#writeFeedToFile("test");
#exit;
#get data from gruvr
get_feed_gruvr;
#create quickcalendar entries from data
init_quick_entries;
manipulateFeedData;
close_quick_entries;
#write it to file
write_entries_to_file;
print "\n\nCREATED QUICK CALENDAR ENTRIES for feed: $gruvr_feed_url\n\n";
print $calendar_entries;
print "Retrieve calendars\n";
retrieve_calendars($g_owned_calendar);
#print Dumper($g_all_calendars);
extract_calendars;
get_target_calendar;
print "\n\nTarget calendar:$target_calendar\n";
print "\n\nTarget id:$target_id\n";
put_entries;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment