#!/usr/bin/perl ## (C) 2007 Dan Check ## http://danathan.jloreview.com/ ## ## This script is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2, or (at your option) ## any later version. ## ## This script is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You may have received a copy of the GNU General Public License ## along with this script; see the file COPYING. If not, write to the ## Free Software Foundation, Inc., 59 Temple Place - Suite 330, ## Boston, MA 02111-1307, USA. use strict; use warnings; use LWP; use HTML::Form; use Date::Format; use MIME::Lite; use Crypt::SSLeay; ######## BEGIN CONFIGURATION ######## # From address for emails my $from_address = 'xword@example.com'; # Comma separated list of recipients. my @addresses = ('solver1@example.com', 'solver2@example.com' ); # NYTimes user name and password my $user = 'user'; my $pass = 'pass'; # Location to download files to, relative to the directory the script is run # from. If this is not blank, be sure to include a trailing slash on the # directory name. my $download_directory = ""; ######### END CONFIGURATION ######### # create a new browser my $browser = LWP::UserAgent->new; # Set browser headers $browser->default_headers->push_header('User-Agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6'); $browser->default_headers->push_header('Accept' => 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'); $browser->default_headers->push_header('Accept-Language' => 'en-us,en'); $browser->default_headers->push_header('Accept-Charset' => 'ISO-8859-1,utf-8'); # Get the URL for today's puzzle my $puzzle = time2str("%b%d%y", time) . ".puz"; my $loginurl = "http://select.nytimes.com/auth/login"; my $puzzleurl = "http://select.nytimes.com/premium/xword/" . $puzzle; # Create a cookie jar $browser->cookie_jar({}); # Get the login page my $response = $browser->get($puzzleurl); # Find the forms my @forms = HTML::Form->parse($response->decoded_content, $response->base); my $loginform; foreach (@forms) { #print "FORM!" . $form->action . "\n"; if ($_->action =~ /login/) { $loginform = $_; last; } } # Alter the form $loginform->param("USERID", $user); $loginform->param("PASSWORD", $pass); $response = $browser->request($loginform->click); if ($response->header('Location') ne '') { # Write the file to the download directory $response = $browser->get($response->header('Location'), ':content_file' => $download_directory . $puzzle ); } # Loop through the addresses for my $to_address (@addresses) { ### Create the multipart container my $msg = MIME::Lite->new ( From => $from_address, To => $to_address, Subject => 'Daily Crossword', Type =>'multipart/mixed' ) or die "Error creating multipart container: $!\n"; $msg->attach ( Type => 'application/x-puz', Path => $download_directory . $puzzle, Filename => $puzzle, Disposition => 'attachment' ) or die "Error adding $puzzle: $!\n"; #MIME::Lite->send('smtp', $mail_host, Timeout=>60); $msg->send or die ("Cannot send message: $!\n"); }