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(); } }

In reply to A FitDay example for Mechanize and those on a diet by blucap

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.