Skip to content

Instantly share code, notes, and snippets.

@squeek502
Last active October 19, 2015 17:23
Show Gist options
  • Save squeek502/34aae08429081dbbadb6 to your computer and use it in GitHub Desktop.
Save squeek502/34aae08429081dbbadb6 to your computer and use it in GitHub Desktop.
Not Enough Mods JSON to RSS Script
<?php
define('RSS_URL', "http://www.ryanliptak.com/minecraft/nem-rss");
define('RSS_FILE', dirname(__FILE__) . DIRECTORY_SEPARATOR . "rss.xml");
buildrss();
function get_versions()
{
return json_decode(curl_get_file_contents("http://bot.notenoughmods.com/?json"));
}
function get_mods($version)
{
return json_decode(curl_get_file_contents("http://bot.notenoughmods.com/$version.json"));
}
function curl_get_file_contents($URL)
{
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
curl_setopt($c, CURLOPT_USERAGENT, 'NEM RSS');
$contents = curl_exec($c);
curl_close($c);
if ($contents)
return $contents;
else
return false;
}
function str_ends_with($haystack, $needle)
{
return ($length = strlen($needle)) == 0 ? true : substr($haystack, -$length) === $needle;
}
function buildrss()
{
$items = array();
$cutofftime = time() - 7 * 24 * 60 * 60; // 7 days
foreach(get_versions() as $version)
{
foreach(get_mods($version) as $mod)
{
if (!$mod->lastupdated || $mod->lastupdated === "null")
continue;
if ($mod->lastupdated < $cutofftime)
continue;
$devsuffix = "-only";
$devstring = "";
if (str_ends_with($mod->version, $devsuffix))
$devstring = substr($mod->version, 0, -strlen($devsuffix));
else if (str_ends_with($mod->prevversion, $devsuffix))
$devstring = substr($mod->prevversion, 0, -strlen($devsuffix));
$isdev = $devstring !== "";
$modversion = $isdev ? $mod->dev : $mod->version;
$version_change = $mod->added_at == $mod->lastupdated ? "added at version $modversion" : "updated to version $modversion";
$items[$mod->lastupdated.$mod->name] = array(
"title" => "[$version] $mod->name ".($isdev ? "($devstring) " : "") . $version_change,
"link" => $mod->longurl,
"content" => $mod->comment,
"author" => $mod->author,
"date" => $mod->lastupdated,
"guid" => $mod->name.$mod->lastupdated.$mod->prevversion.$mod->version.$mod->dev
);
}
}
krsort($items);
$now = gmdate(DATE_RSS);
$file= fopen(RSS_FILE, "w");
$_xml ="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
$_xml .="<rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\r\n";
$_xml .="<channel>\r\n";
$_xml .="\t<title>Not Enough Mods - Recent Changes</title>\r\n";
$_xml .="\t<link>http://notenoughmods.com</link>\r\n";
$_xml .="\t<description>Changes made to Not Enough Mods in the past week</description>\r\n";
$_xml .="\t<pubDate>$now</pubDate>\r\n";
$_xml .="\t<lastBuildDate>$now</lastBuildDate>\r\n";
$_xml .="\t<atom:link href=\"".RSS_URL."\" rel=\"self\" type=\"application/rss+xml\" />\r\n";
foreach ($items as $key => $val)
{
$_xml .="\t<item>\r\n";
$_xml .="\t\t<title>".htmlspecialchars($val["title"])."</title>\r\n";
$_xml .="\t\t<description>".htmlspecialchars($val["content"])."</description>\r\n";
$_xml .="\t\t<dc:creator>".htmlspecialchars($val["author"])."</dc:creator>\r\n";
$_xml .="\t\t<pubDate>".gmdate(DATE_RSS, intval($val["date"]))."</pubDate>\r\n";
$_xml .="\t\t<link>".htmlspecialchars($val["link"])."</link>\r\n";
$_xml .="\t\t<guid>".htmlspecialchars($val["guid"])."</guid>\r\n";
$_xml .="\t</item>\r\n";
}
$_xml .="</channel>\r\n";
$_xml .="</rss>\r\n";
fwrite($file, $_xml);
fclose($file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment