Created
February 27, 2016 23:58
-
-
Save ilevantis/94a304e0cd977b28fbb3 to your computer and use it in GitHub Desktop.
Turn EvolDir categories into RSS feeds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: application/rss+xml; charset=UTF-8'); | |
$list_name = htmlspecialchars($_GET['lname']); | |
$list_url = 'http://life.mcmaster.ca/cgi-bin/my_wrap/brian/evoldir/'.$list_name.'/'; | |
$month_num = array ( | |
"Jan" => 1, | |
"Feb" => 2, | |
"Mar" => 3, | |
"Apr" => 4, | |
"May" => 5, | |
"Jun" => 6, | |
"Jul" => 7, | |
"Aug" => 8, | |
"Sep" => 9, | |
"Oct" => 10, | |
"Nov" => 11, | |
"Dec" => 12 | |
); | |
function nl2p($string) { | |
$paragraphs = ''; | |
foreach (explode(PHP_EOL.PHP_EOL, $string) as $block) { | |
$paragraphs .= '<p>' . $block . ' </p>'; | |
} | |
return $paragraphs; | |
} | |
$html = file_get_contents($list_url); | |
$pre_block = explode( '<pre>', $html ); $pre_block = explode( PHP_EOL.'</pre>', $pre_block[1] ); $pre_block = $pre_block[0]; | |
$lines = explode(PHP_EOL, $pre_block); $lines = array_slice($lines, 0, 20); | |
$prev_post_year = date('Y'); //now | |
$prev_post_month = date('n'); //now | |
foreach ($lines as $line) { | |
$post_url = 'http://life.mcmaster.ca'.preg_replace('/<a href="(.*)">.*/', '$1', $line); | |
$post_title = preg_replace('/<a href=".*">(.*)<\/a>.*/', '$1', $line); $post_title = preg_replace('/\./', ' ', $post_title); | |
$post_content = file_get_contents($post_url); $post_content = preg_replace('/[^\PC\s]/u', '', $post_content); $post_content = utf8_encode($post_content); | |
$post_time_str = preg_replace('/.*<\/a>\s+\d+ \w+\s+(\w+)\s{1,2}(\d+) (\d\d:\d\d)/', '$1 $2 $3', $line); // Month Day Time | |
$post_month = $month_num[explode(' ', $post_time_str )[0]]; | |
if ($post_month > $prev_post_month) { | |
$post_year = $prev_post_year - 1; | |
} else { | |
$post_year = $prev_post_year; | |
} | |
$post_pubdate_int = strtotime($post_time_str . $post_year); | |
$posts[] = array( | |
'post_url' => $post_url, | |
'post_title' => $post_title, | |
'post_desc' => substr($post_content, 0, 300), | |
'post_content' => nl2p($post_content), | |
'post_pubdate' => date('r', $post_pubdate_int) | |
); | |
$prev_post_year = $post_year; | |
$prev_post_month = $post_month; | |
} | |
?> | |
<?xml version="1.0" encoding="UTF-8"?> | |
<rss version="2.0" | |
xmlns:content="http://purl.org/rss/1.0/modules/content/" | |
> | |
<channel> | |
<title><?php echo 'EvolDir: '.$list_name; ?></title> | |
<link><?php echo $list_url; ?></link> | |
<description><?php echo 'RSS feed for EvolDir: '.$list_name; ?></description> | |
<?php | |
// item loop stuff goes here | |
foreach ($posts as $post) { | |
echo "<item>\n"; | |
echo "<title>"; | |
echo $post['post_title']; | |
echo "</title>\n"; | |
echo "<link>"; | |
echo $post['post_url']; | |
echo "</link>\n"; | |
echo "<description>"; | |
echo '<![CDATA['. $post['post_desc'] .']]>'; | |
echo "</description>\n"; | |
echo "<content:encoded>"; | |
echo '<![CDATA['. $post['post_content'] .']]>'; | |
echo "</content:encoded>\n"; | |
echo "<pubDate>"; | |
echo $post['post_pubdate']; | |
echo "</pubDate>\n"; | |
echo "</item>\n"; | |
} | |
?> | |
</channel> | |
</rss> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment