-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.php
102 lines (87 loc) · 2.61 KB
/
rss.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
<?php
error_reporting( E_ALL );
ini_set( 'display_errors', false );
ini_set( 'log_errors', true );
ini_set( 'error_log', './php-error.log' );
define( 'CSV_FILE', './alerts-log.csv' );
/**
* Read from the CSV and format it as a fully-specced RSS 2.0 file
*/
$handle = fopen( CSV_FILE, 'r' );
if ( false === $handle ) {
error_log( 'Could not parse the csv' );
exit(1);
}
/**
* Escape strings for display
*
* @link https://stackoverflow.com/questions/3426090/how-do-you-make-strings-xml-safe
* @param string $string The string to escape
* @return string
*/
function escape( $string ) {
return htmlspecialchars( $string, ENT_XML1 );
}
/**
* Convert our feeds.csv into a number of <outline> elements
*
* @param resource $handle Pointer to ./feeds.csv
* @return void
*/
function iterate( $handle ) {
$iterator = 0;
$entries = [];
while ( ( $data = fgetcsv( $handle, 1000 ) ) !== false ) {
$iterator++;
// skip over the header row.
if ( 1 === $iterator ) {
continue;
}
error_log( var_export( $data, true ) );
/*
* 0 header
* 1 description
* 2 link_title
* 3 link_url
* 4 link_target
* 5 time timestamp that this was added to the scrape CSV
*/
$title = escape( sprintf(
'%1$s: %2$s',
$data[0] ?? '',
$data[1] ?? ''
) );
$link = escape( $data[3] ?? '' );
$description = '';
$description .= sprintf(
'<p><a href="%1$s">%2$s: %1$s</a></p>',
$data[3],
$data[2]
);
$description .= '<p>The publication date on this feed reflects the date that the item was scraped from cota.com, not the date that the item was published or pulled down.</p>';
$pubDate = date_format( DateTimeImmutable::createFromFormat( 'U', $data[5] ), DATE_RSS );
ob_start();
?>
<item>
<title><?php echo $title; ?></title>
<link><?php echo $link; ?></link>
<description><![CDATA[<?php echo $description; ?>]]></description>
<pubDate><?php echo $pubDate; ?></pubDate>
<guid><?php echo $link . '#' . rawurlencode( $pubDate ); ?></guid>
</item>
<?php
$entries[] = trim( ob_get_clean() );
} // endwhile
echo implode( "\n", array_reverse( $entries ) );
}
?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Unofficial Central Ohio Transit Authority Alerts Feed</title>
<link>https://github.com/benlk/cota-reroute-pdf-rss</link>
<description>COTA alert messages scraped from a COTA API; parsed and formatted as RSS</description>
<language>en-us</language>
<atom:link href="https://raw.githubusercontent.com/benlk/cota-reroute-pdf-rss/main/rss.xml" rel="self" type="application/rss+xml"/>
<?php iterate( $handle ); ?>
</channel>
</rss>