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 th +e food using the URL my $page = $mech->content; if ($page =~ /FoodToAdd=(.*)\"><im/) { my $result = $1; $result=~ s/^\s*//; $result=~ s/\s*$//; #Add food via URL: $result= "http://www.fitday.com/WebFit/DayFoodsTab.asp?F +oodToAdd=$result"; $mech->get($result); $mech->success or die "Can't add food"; print "Added Food: $Fname.\n" if $mech->success(); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A FitDay for those on a diet Eat. drink and be merry.
by zentara (Cardinal) on Feb 06, 2008 at 14:35 UTC | |
by jaldhar (Vicar) on Feb 06, 2008 at 21:41 UTC | |
|
Re: A FitDay example for Mechanize and those on a diet
by blucap (Initiate) on Feb 09, 2008 at 16:37 UTC |