use strict;
use WWW::Mechanize;
use HTML::Form;
use HTTP::Cookies;
#use LWP;
my $mech = WWW::Mechanize->new( autocheck => 1 );
my $form_name="loginForm";
my $Fname;
####################################################################
# EDIT below
###################################################################
#
#
my $username = "username";
my $password = "pw123456789";
my @myFoods = (
'Cheese, natural, Cheddar or American type',
'Tortilla, whole wheat',
'Lettuce, raw',
'Chicken, ground',
'Cucumber, raw',
'Olives, black',
'Pepper, raw, NFS',
'Peppers, jalapeno, raw'
);
#
#
####################################################################
# From hereon it should work automatically -STOP EDITING!
# Goes to the fitday web site and logs in, deals with cookie
####################################################################
$mech->get("http://www.fitday.com/WebFit/Index.html");
die "Can't get to the Fitday Page: ", $mech->response->status_line
unless $mech->success;
print "Found www.Fitday.com\n" if $mech->success();
$mech->form($form_name);
die "Can't get the form: ", $mech->response->status_line
unless $mech->success;
my $cookie_jar = HTTP::Cookies->new(
file => "cookies.txt",
autosave => 1,
);
$mech->cookie_jar($cookie_jar);
$mech->field('LoginName',$username);
$mech->field('Password',$password);
print "Populated $form_name\n" if $mech->success();
my $results = $mech->submit();
print "Submitted $form_name\n" if $mech->success();
####################################################################
# After log in, find food
####################################################################
foreach $Fname (@myFoods)
{
print "\nLet's add $Fname\n";
$mech->get("http://www.fitday.com/WebFit/FoodSearch.asp");
$mech->success or die "Can't find the search page";
print "Found Foodsearch page\n" if $mech->success();
#submit food to search filed
$mech->field('Search',$Fname);
my $results = $mech->submit();
$mech->success or die "Can't post to the search page";
print "Found Food: $Fname.\n" if $mech->success();
#find code for specific food on web-page, and if found, add the food using the URL
my $page = $mech->content;
if ($page =~ /FoodToAdd=(.*)\">get($result);
$mech->success or die "Can't add food";
print "Added Food: $Fname.\n" if $mech->success();
}
}