Skip to content

Instantly share code, notes, and snippets.

@Shiryou
Last active June 27, 2016 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shiryou/977f799942b68d6d1f054220b0370241 to your computer and use it in GitHub Desktop.
Save Shiryou/977f799942b68d6d1f054220b0370241 to your computer and use it in GitHub Desktop.
MaGog to RSS
<?php
/**
* MaGog to RSS script for game update notifications.
*
* A script to scrape results from the
* {@link [http://www.an-ovel.com/pages/magog.php] [<MaGog GOG search engine>]} and convert them
* into an RSS feed.
*
* @author Kiran Welle
*/
/**
* Specify the MaGog snapshot GUID to use.
*/
if( $_REQUEST['snap'] !== null ) {
$MAGOG_SNAPSHOT = $_REQUEST['snap'];
} else {
die("Please specify a MaGog snapshot to check.");
}
/**
* Show games updated within this time period.
*/
$DAYS_SINCE_UPDATE = 1;
/**
* Replace special characters to clean up the game titles.
*/
function clean_game_title( $title ) {
$title = str_replace("®", "&#169;", htmlspecialchars($title));
return mb_convert_encoding( $title, "UTF-8" );
}
/*
function guid_from_url( $url ) {
$guid = split("/",$url);
return $guid[count($guid)-1];
}
*/
/**
* Encase the game description in a CDATA block to keep feed readers from complaining.
*/
function clean_description( $description ) {
$innerHTML= '';
foreach ($description->childNodes as $child) {
$innerHTML .= $child->ownerDocument->saveXML( $child );
}
return "<![CDATA[".$innerHTML."]]>";
}
/**
* Clean things up for the XML import. This should probably be replaced by JSON at some point.
*/
function clean_xml_document( $xml ) {
$p = $xml->getElementsByTagName('p');
if( $p->length > 0 )
foreach( $p as $node ) {
if( $node->getAttribute('class') == "nogames" )
return false;
}
$table = $xml->getElementsByTagName('span');
foreach($table as $node) {
if( $node->getAttribute("class") == "dl_match" ) {
$node->setAttribute("style","font-weight: bold; color: red;");
$node->removeAttribute("class");
$parent = $node->parentNode;
if( $parent->tagName == "small" ) {
if( $node->nextSibling->nodeName == "#text" && $node->nextSibling->nodeValue == "; " )
$parent->removeChild($node->nextSibling);
$element = $xml->createElement('b');
$para = $xml->createElement('p');
$para->appendChild($element);
$parent->replaceChild($para, $node);
$element->appendChild($node);
}
}
}
$table = $xml->getElementsByTagName('tr');
$table->item(0)->parentNode->removeChild($table->item(0));
$table->item($table->length - 1)->parentNode->removeChild($table->item($table->length - 1));
return $table;
}
/**
* Format the MaGog URL and retrieve the results from MaGog.
*/
function setup_xml_document( &$url ) {
global $DAYS_SINCE_UPDATE;
if( $DAYS_SINCE_UPDATE > 0 )
$date = date('dmY', strtotime('-'.$DAYS_SINCE_UPDATE.' days'));
else
$date = date('dmY');
$url = preg_replace( '/flt=Dsa~([0-9]{8})/i', "flt=Dsa~$date", $url );
// Create a new DOM Document to hold our webpage structure
$xml = new DOMDocument();
// Load the url's contents into the DOM
if( $xml->loadHTMLFile($url) === false ) {
http_response_code(404);
die( "Retrieval of <b>$url<b> failed." );
}
return clean_xml_document($xml);
}
/**
* Set up the headers for the file.
*/
function setup_headers() {
header("Content-Type: application/rss+xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'."\n";
}
/**
* Set up the meta data for the RSS feed.
*/
function get_channel( $url ) {
?>
<channel>
<title>GOG Game Updates via MaGog</title>
<description>Recent game updates on GOG.com made available by MaGog.</description>
<link><?php echo str_replace("&", "&amp;", $url) ?></link>
<atom:link href="http://<?php echo $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]; ?>" rel="self" type="application/rss+xml" />
<?php
}
/**
* Retrieve and format a game.
*/
function get_item( $node ) {
?>
<item>
<title><?php echo clean_game_title($node->childNodes->item(1)->nodeValue) ?></title>
<description><?php echo clean_description($node->nextSibling->firstChild); ?></description>
<link><?php echo $node->childNodes->item(1)->firstChild->getAttribute("href"); ?></link>
<guid><?php echo $node->childNodes->item(1)->firstChild->getAttribute("href"); ?></guid>
</item>
<?php
}
/**
* Run the thing. Maybe this will all become a class at some point?
*/
$url = 'http://www.an-ovel.com/cgi-bin/magog.cgi?ver=422&scp=gdsp&dsp=fD&ord=&flt=Dsa~01012000~3an~owned%2C~&opt=d&myf='.$MAGOG_SNAPSHOT;
$table = setup_xml_document($url);
setup_headers();
get_channel($url);
if( $table !== false ) {
$i = 0;
foreach($table as $node) {
$i++;
if( $i % 2 == 0 ) continue;
get_item( $node );
}
}
?>
</channel>
</rss>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment