Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

A FitDay example for Mechanize and those on a diet

by blucap (Initiate)
on Feb 05, 2008 at 21:51 UTC ( [id://666411]=CUFP: print w/replies, xml ) Need Help??

Wanna lose weight comfortably? Here’s a script that I wrote to i) test the WWW::Mechanize module with forms and to ii) make it easy for me to update entries in www.fitday.com. Note that this script adds complex food that you eat regularly to your daily food recording; in this case it is a chicken wrap. You will have to add the ingredients yourself first through the FitDay search pages. Then you can add them to the array in the code below. After running the script change the amounts you consumed.
 
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 (Archbishop) on Feb 06, 2008 at 14:35 UTC
    my @myFoods = ( 'Cheetos', 'Cheezits', 'Spam', 'eggs', 'bologna', 'potato chips, BBQ', 'vodka , Brita filtered', 'Metamucil' #my health food :-) );
    I was watching the business news yesterday, and they read a new report out ot the Netherlands, saying that the governments of the world are misguided thinking they will save society money by promoting healthy eating/lifestyles. From memory, it was something like caring for an obese person till death was $300,000...... a drinker 350,000, and a smoker 400,000. If you live past those ailments into old age and Alzheimer's.... it costs over 1,000,000. So eat, drink and be merry, and save your fellow citizens some money. :-)

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      my @myfoods = { 'roti', 'curried vegetables', 'lentil soup', 'rice', 'Coke Zero', 'beans on toast', 'pizza', # I know... 'Cheez-its. # I know but they're so good... 'Pop Tarts', # so much for vegetarianism being healthy # ...and it just goes further downhill from here. };

      Too bad it doesn't really fit into the poll format but it would be fun to know what other monks eat.

      --
      જલધર

Re: A FitDay example for Mechanize and those on a diet
by blucap (Initiate) on Feb 09, 2008 at 16:37 UTC
    Thanks guys for your comments! Obviously it is a doddle to have this code started from VBA-Excel, that is, record your food in a spreadsheet, port it to a CSV file and than call perl for the rest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://666411]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-19 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found