Skip to content

Instantly share code, notes, and snippets.

@arolson101
Last active December 28, 2015 10:49
Show Gist options
  • Select an option

  • Save arolson101/7489311 to your computer and use it in GitHub Desktop.

Select an option

Save arolson101/7489311 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
use strict;
use warnings;
my $csv = 'Weights.csv';
my $script = 'myfitnesspalscript.html';
my $header = <<HEADER_END;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://www.myfitnesspal.com/" />
<title>myfitnesspalscript</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">myfitnesspalscript</td></tr>
</thead><tbody>
HEADER_END
my $footer = <<FOOTER_END;
</tbody></table>
</body>
</html>
FOOTER_END
sub entry
{
my ($month, $day, $year, $weight) = @_;
$month = (
qw/ January February March
April May June
July August September
October November December /
)[$month - 1];
$day = $day + 0; # remove any prepended 0s
my $text = <<ENTRY_END;
<!-- $month $day, $year - $weight -->
<tr>
<td>select</td>
<td>id=measurement_entry_date_2i</td>
<td>label=$month</td>
</tr>
<tr>
<td>select</td>
<td>id=measurement_entry_date_3i</td>
<td>label=$day</td>
</tr>
<tr>
<td>select</td>
<td>id=measurement_entry_date_1i</td>
<td>label=$year</td>
</tr>
<tr>
<td>type</td>
<td>id=measurement_display_value</td>
<td>$weight</td>
</tr>
<tr>
<td>clickAndWait</td>
<td>//input[\@value='Add New Entry']</td>
<td></td>
</tr>
ENTRY_END
return $text;
}
sub main()
{
my $text = $header;
open my $hin, $csv or die "Can't open $csv for reading: $!";
while(my $line = <$hin>)
{
if($line =~ m["(\d+)/(\d+)/(\d{4})","(\d{3}.\d)"])
{
my ($month, $day, $year, $weight) = ($1, $2, $3, $4);
$text .= entry($month, $day, $year, $weight);
}
}
$text .= $footer;
close $hin;
open my $hout, ">$script" or die "Can't open $script for writing: $!";
print $hout $text;
close $hout;
}
main();
@arolson101
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment