From bfbcd72cb21c78fb2cf2eb72e4d989ca9f842b59 Mon Sep 17 00:00:00 2001 From: Konrad Wojas Date: Mon, 31 Mar 2014 00:06:08 +0800 Subject: [PATCH] Only send posts that have not been sent before; ID of last post is saved in $lastfile; HTML fixes --- README.md | 16 +++++++++++---- config.example.php | 2 ++ xkcd-mailer.php | 49 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 255bba9..462b7f4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # xkcd-Mailer # -Takes the first/latest item from the [xkcd](http://xkcd.com/) atom-feed and mails the image and punchline to a specified email address. +Takes the first/latest item from the [xkcd](http://xkcd.com/) atom-feed and mails the image and punchline to a specified email address, if it has not been sent before. ## configuration ## @@ -20,20 +20,28 @@ date_default_timezone_set("Europe/Helsinki"); // Your destination $mail = "your@email.com"; $from = "xkcd mailer "; + +// File to write ID of last post to +$lastfile = "last.txt"; ``` ## crontab example ## -15 minutes over 7am on monday, wednesday and friday. +Run every hour. - 15 7 * * 1,3,5 /usr/bin/php /full/path/to/xkcd-mailer.php + 0 * * * * /usr/bin/php /full/path/to/xkcd-mailer.php +This version will check if the last post was already emailed and will only send the post +if it has not been emailed yet. ## caveats ## -- Script doesn't check has the feed been updated, possibly causing old strip delivery +Make sure to set $lastfile to a path that you have write access to. +## changes ## + +- 2014-03-30 @wojas added check to see if a post already sent ## contributing ## diff --git a/config.example.php b/config.example.php index cce6213..9396fd6 100644 --- a/config.example.php +++ b/config.example.php @@ -13,3 +13,5 @@ date_default_timezone_set("Europe/Helsinki"); $mail = "your@email.com"; $from = "xkcd mailer "; +// File to write ID of last post to +$lastfile = "last.txt"; diff --git a/xkcd-mailer.php b/xkcd-mailer.php index d912eaa..8fa3cf3 100644 --- a/xkcd-mailer.php +++ b/xkcd-mailer.php @@ -12,6 +12,8 @@ **/ // Use config.example.php as base for your configurations. + $lastfile = "last.txt"; + $here = dirname( __FILE__ ); if( !is_readable($here . '/config.php') ) { die("Please configure me. I don't know where I should sent the comic. (Config file {$here}/config.php missing.)"); @@ -39,20 +41,41 @@ } $item = $data->entry[0]; - $date = date("Y-m-d", strtotime($item->updated)); - preg_match("#title=\"(.+)\"#iU", $item->summary, $t); - // To send HTML mail, the Content-type header must be set - $headers = 'MIME-Version: 1.0' . "\r\n"; - $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n"; - $headers .= 'From: '. $from . "\r\n"; + if (file_exists($lastfile)) { + $f = fopen($lastfile, 'r'); + $last = (int) fread($f, 1024); + fclose($f); + } else { + $last = 0; + } + $parts = explode('/', $item->id); + $current = (int) $parts[3]; + if ($current > $last) { + $date = date("Y-m-d", strtotime($item->updated)); + preg_match("#title=\"(.+)\"#iU", $item->summary, $t); - $subject = "xkcd {$date}: {$item->title}"; - $punchline = $t[1]; + // To send HTML mail, the Content-type header must be set + //$headers = 'MIME-Version: 1.0' . "\r\n"; + $headers = 'Content-type: text/html; charset=UTF-8' . "\r\n"; + $headers .= 'From: '. $from . "\r\n"; - $msg = "

id}\">{$item->title}

\n" - . "Posted {$date}
\n" - . $item->summary."
\n" - . "

{$punchline}

\n"; + $subject = "xkcd {$date}: {$item->title}"; + $punchline = $t[1]; - mail($mail, $subject, $msg, $headers); + $msg = "

id}\">{$item->title}

\n" + . "Posted {$date}
\n" + . $item->summary."
\n" + . "

{$punchline}

\n"; + + mail($mail, $subject, $msg, $headers); + + $f = fopen($lastfile, 'w'); + fwrite($f, $current); + fclose($f); + + echo "New last is $current (was $last)\n"; + } else { + echo "No new XKCD: last=$last current=$current\n"; + } +?>