Daily delivery of an electronic newspaper

I subscribe to Investor’s Business Daily for insight and valuable education as I gradually learn how to effectively invest in the market. It comes out Monday-Friday and obviously is very time relevant. I initially signed up for the print version so that I could read it on the bus and have something to highlight. However, I ended up never getting it on time because it’d come in while I was at work and I’d end up not even looking at it when I got home. I switched to the electronic version, but then I’d end up not downloading and reading the thing for weeks at a time. I needed an ‘in-your-face’ solution, so I wrote up this little PHP script to automatically download the eIBD every day and a separate shell script to open the PDF at 9am on my computer every weekday morning. So, I essentially have it ‘delivered’ to my desktop when I log on my Mac in the morning!

Here’s an example script to show how I did it. I’ve removed the information specific to IBD. Keep in mind that this script, as presented, does not log in to anything and assumed a filename format like 020508.pdf. I wrote these scripts for Mac OS X 10.4 and used Cronnix to create the crontab entry. It could easily be done on a Windows machine as well using the utilities provided by that system.

<?php
//
// Provided as-is without warranty.
//


$ch = curl_init();
$today = date(mdy)

;if(date(w) == 0) {
  $monday = mktime(0, 0, 0, date(m), date(d)+1, date(y));
  $today = date(mdy, $monday);
}

if(date(w) ==  6) {
  $monday = mktime(0, 0, 0, date(m), date(d)+2, date(y));
  $today = date(mdy, $monday);
}

$pdf_url = http://www.example.com/pdf/ . $today.pdf;
$fp = fopen(/Users/username/path/to/pdf/archive/$today.pdf, w);

curl_setopt ($ch, CURLOPT_URL, $pdf_url);
curl_setopt ($ch, CURLOPT_FILE, $fp);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_exec ($ch);
curl_close ($ch);
fclose($fp);
?>

And the shell script to open the PDF…

DATE=`date ‘+%m%d%y’`;
FILE=~/path/to/pdf/archive/${DATE}.pdf;
if [ -e $FILE ]; then
  open /Applications/Preview.app $FILE;
fi

And the crontab used to download the PDF each day at 11pm and open the file for me at 9am.

30      23      *       *       *               php /path/to/script.php
0       9       *       *       1,2,3,4,5,7     sh /path/to/pdf_opener.sh

I might clean this up a bit, but I just wanted to share it in case anyone else had a similar need for daily download and view. Feel free to offer suggestions for other ways to achieve this.

Extending link area using DOM methods


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function linkTabs() {
  if(!document.getElementById("top_tabs")) return false;

  var topTabs = document.getElementById("top_tabs");
  var topTabsLi = topTabs.getElementsByTagName("li");

  for (var i = 0; i < topTabsLi.length; i++) {
    topTabsLi[i].onmouseover = function() {
      this.style.cursor = “pointer”;
    }
    topTabsLi[i].onclick = function() {
      return linkThis(this);
    }
  }
}
function linkThis(whichLink) {
  window.location = whichLink.getElementsByTagName(”a”)[0].getAttribute(”href”);
}

addLoadEvent(linkTabs);