-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.php
123 lines (116 loc) · 4.03 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
// 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");
date_default_timezone_set('Etc/GMT+4'); // adjust for server timezone
// path to paper is base url +day of month + state + paper + .pdf
// https://cdn.freedomforum.org/dfp/pdf16/MA_BG.pdf
$news = array();
//$paper['prefix']="WSJ";$paper['style']="width:98%;margin:-70px 0px 0px -15px";array_push($news,$paper); // WSJ -broken 2021
$paper['prefix']="DC_WP";$paper['style']="width:108%;margin:-5% -5% 0px -5%";array_push($news,$paper); // Washington Post
$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:-28px 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']="CA_SFC";$paper['style']="width:96%;margin:-20px 0px 0px 0px";array_push($news,$paper); // SF 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
function getCounter() {
global $maxPapers;
$fp = fopen("counter.txt", "r");
if ($fp) {
$x= intval(fread($fp,1024));
fclose($fp);
} else {
$x = 0;
}
if ($x > $maxPapers) {
$x = 0;
}
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);
}
// 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";
$pngfile = "archive/" . $prefix . "_" . date('Ymd',strtotime("-" . $offset . " days")) . ".png";
$rootpath = getcwd() . "/";
// check if a jpg has already been created
// if not we start checking for the PDF and converting
if (!file_exists($pngfile)){
$file_headers = @get_headers($pathToPdf);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$ch = curl_init($pathToPdf);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$exists = true;
$result = file_put_contents($pdffile, $data);
}
if ($exists) {
$command = "convert -density 300 -background white -alpha remove '" . $rootpath . $pdffile .
"' -colorspace Gray -resize 1600 '" . $rootpath . $pngfile . "'";
exec($command, $output, $response);
}
} else {
$exists = true;
}
if ($exists) {
return $pngfile;
} else {
return false;
}
}
$currentIndex = getCounter();
if (isset($_REQUEST['index'])) {
$currentIndex = $_REQUEST['index'];
}
$imageresult = fetchPaper($news[$currentIndex]['prefix'],0); // Fetch today
if (empty($imageresult)) {
$imageresult = fetchPaper($news[$currentIndex]['prefix'],1); // yesterday
}
if (empty($imageresult)) {
$imageresult = fetchPaper($news[$currentIndex]['prefix'],2); // twesterday
}
?>
<!DOCTYPE html>
<html>
<head>
<style>
body { text-align:center; }
.paper {
background-color:white;
<?php echo $news[$currentIndex]['style'] ?>
}
</style>
</head>
<body>
<?php if (strlen($imageresult) < 1) {
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);
?>