Skip to content

Commit

Permalink
Merge branch 'main' of github.com:graiz/newsprint into main
Browse files Browse the repository at this point in the history
  • Loading branch information
graiz committed Apr 1, 2021
2 parents 5f41344 + 06d600d commit 7181f8e
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
// Don't cache this page.
// Don't cache this page.
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
Expand All @@ -14,14 +14,14 @@
$paper['prefix']="MA_BG";$paper['style']="width:98%;margin:5px 10px 0px 8px";array_push($news,$paper); // Boston Globe
$paper['prefix']="NY_NYT";$paper['style']="width:99%;margin:-88px 14px 0px 3px";array_push($news,$paper); // New York Times
$paper['prefix']="CA_LAT";$paper['style']="width:94%;margin:-2% 0px 0px 0px";array_push($news,$paper); // L.A. Times
$paper['prefix']="CAN_TS";$paper['style']="width:90%;margin:-70px 0px 0px 0px";array_push($news,$paper); // Toronto Star
$paper['prefix']="CAN_TS";$paper['style']="width:90%;margin:-70px 0px 0px 0px";array_push($news,$paper); // Toronto Star
$paper['prefix']="CA_SFC";$paper['style']="width:96%;margin:-20px 0px 0px 0px";array_push($news,$paper); // San Fran Chronical

$maxPapers = count($news) -1;

// Loop a counter without a DB.
// allows us to get a different newspaper
// each load by asking for the counter
// allows us to get a different newspaper
// each load by asking for the counter
function getCounter() {
global $maxPapers;
$fp = fopen("counter.txt", "r");
Expand All @@ -31,29 +31,29 @@ function getCounter() {
} else {
$x = 0;
}
if ($x > $maxPapers) {
if ($x > $maxPapers) {
$x = 0;
}
if (!empty($_REQUEST)) { // Override the counter if there
if (!empty($_REQUEST['index'])) { // Override the counter if there
$x = $_REQUEST['index']; // is a URL parameter for index
}
return($x);
}
function incrementCounter(int $counter){
// increment the paper for next time
$counter++;
$fp = fopen("counter.txt", "w");
fwrite($fp, $counter);
fclose($fp);
$fp = fopen("counter.txt", "w");
fwrite($fp, $counter);
fclose($fp);
}

// fetch a paper and cache in as a JPG. Return the path to the JPG if we found it.
// We can pass in an offset in days to get yesterday or two days ago
// fetch a paper and cache in as a JPG. Return the path to the JPG if we found it.
// We can pass in an offset in days to get yesterday or two days ago
function fetchPaper($prefix, $offset=0){
$pathToPdf = "https://cdn.freedomforum.org/dfp/pdf" . date('j',strtotime("-" . $offset . " days")) . "/" . $prefix . ".pdf";
$pdffile = "archive/" . $prefix . "_" . date('Ymd',strtotime("-" . $offset . " days")) . ".pdf";
$pathToPdf = "https://cdn.freedomforum.org/dfp/pdf" . date('j',strtotime("-" . $offset . " days")) . "/" . $prefix . ".pdf";
$pdffile = "archive/" . $prefix . "_" . date('Ymd',strtotime("-" . $offset . " days")) . ".pdf";
$jpgfile = "archive/" . $prefix . "_" . date('Ymd',strtotime("-" . $offset . " days")) . ".jpg";
$rootpath = getcwd() . "/";
$rootpath = getcwd() . "/";
// check if a jpg has already been created
// if not we start checking for the PDF and converting
if (!file_exists($jpgfile)){
Expand All @@ -67,13 +67,13 @@ function fetchPaper($prefix, $offset=0){
$data = curl_exec($ch);
curl_close($ch);
$exists = true;
$result = file_put_contents($pdffile, $data);
$result = file_put_contents($pdffile, $data);
}
if ($exists) { // convert a high-dpi image, force a white background and
// resize to 1600px wide at 95% jpg quality
$command = 'convert -density 300 -background white -alpha remove ' . $rootpath . $pdffile .
$command = 'convert -density 300 -background white -alpha remove ' . $rootpath . $pdffile .
' -colorspace RGB -resize 1600 -quality 95 ' . $rootpath . $jpgfile;
exec($command, $output, $response);
exec($command, $output, $response);
}
} else {
$exists = true;
Expand All @@ -86,11 +86,11 @@ function fetchPaper($prefix, $offset=0){
}

$currentIndex = getCounter();


$imageresult = fetchPaper($news[$currentIndex]['prefix'],0); // Fetch today
if (empty($imageresult)) {
$imageresult = fetchPaper($news[$currentIndex]['prefix'],1); // yesterday
$imageresult = fetchPaper($news[$currentIndex]['prefix'],1); // yesterday
}
if (empty($imageresult)) {
$imageresult = fetchPaper($news[$currentIndex]['prefix'],2); // twesterday
Expand All @@ -109,15 +109,14 @@ function fetchPaper($prefix, $offset=0){
</head>
<body>
<?php if (empty($imageresult)) {
echo "Newspaper File Not Found. " . $imageresult. " Will keep looking. Checking again in another hour.";
echo "Newspaper File Not Found. " . $imageresult. " Will keep looking. Checking again in another hour.";
} else {
echo "<img src='" . $imageresult . "' class='paper' >";
}
}
?>
</div>
</body>
</html>
<?php
incrementCounter($currentIndex);
?>

0 comments on commit 7181f8e

Please sign in to comment.