Advertisement
Guest User

magog2rss.php

a guest
Apr 18th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.57 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * MaGog to RSS script for game update notifications.
  5.  *
  6.  * A script to scrape results from the
  7.  * {@link [http://www.an-ovel.com/pages/magog.php] [<MaGog GOG search engine>]} and convert them
  8.  * into an RSS feed.
  9.  *
  10.  * @author Kiran Welle
  11.  */
  12.  
  13. /**
  14.  * Specify the MaGog snapshot GUID to use.
  15.  */
  16. if( $_REQUEST['snap'] !== null ) {
  17.     $MAGOG_SNAPSHOT = $_REQUEST['snap'];
  18. } else {
  19.     die("Please specify a MaGog snapshot to check.");
  20. }
  21.  
  22. /**
  23.  * Show games updated within this time period.
  24.  */
  25. $DAYS_SINCE_UPDATE = 1;
  26.  
  27. /**
  28.  * Replace special characters to clean up the game titles.
  29.  */
  30. function clean_game_title( $title ) {
  31.     $title = str_replace("®", "&#169;", htmlspecialchars($title));
  32.     return mb_convert_encoding( $title, "UTF-8" );
  33. }
  34.  
  35. /*
  36. function guid_from_url( $url ) {
  37.     $guid = split("/",$url);
  38.     return $guid[count($guid)-1];
  39. }
  40. */
  41.  
  42. /**
  43.  * Encase the game description in a CDATA block to keep feed readers from complaining.
  44.  */
  45. function clean_description( $description ) {
  46.     $innerHTML= '';
  47.     foreach ($description->childNodes as $child) {
  48.         $innerHTML .= $child->ownerDocument->saveXML( $child );
  49.     }
  50.     return "<![CDATA[".$innerHTML."]]>";
  51. }
  52.  
  53. /**
  54.  * Clean things up for the XML import. This should probably be replaced by JSON at some point.
  55.  */
  56. function clean_xml_document( $xml ) {
  57.     $p = $xml->getElementsByTagName('p');
  58.     if( $p->length > 0 )
  59.         foreach( $p as $node ) {
  60.             if( $node->getAttribute('class') == "nogames" )
  61.                 return false;
  62.         }
  63.    
  64.     $table = $xml->getElementsByTagName('span');
  65.     foreach($table as $node) {
  66.         if( $node->getAttribute("class") == "dl_match" ) {
  67.             $node->setAttribute("style","font-weight: bold; color: red;");
  68.             $node->removeAttribute("class");
  69.            
  70.             $parent = $node->parentNode;
  71.             if( $parent->tagName == "small" ) {
  72.                 if( $node->nextSibling->nodeName == "#text" && $node->nextSibling->nodeValue == "; " )
  73.                     $parent->removeChild($node->nextSibling);
  74.                 $element = $xml->createElement('b');
  75.                 $para = $xml->createElement('p');
  76.                 $para->appendChild($element);
  77.                 $parent->replaceChild($para, $node);
  78.                 $element->appendChild($node);
  79.             }
  80.         }
  81.     }
  82.     $table = $xml->getElementsByTagName('tr');
  83.     $table->item(0)->parentNode->removeChild($table->item(0));
  84.     $table->item($table->length - 1)->parentNode->removeChild($table->item($table->length - 1));
  85.     return $table;
  86. }
  87.  
  88. /**
  89.  * Format the MaGog URL and retrieve the results from MaGog.
  90.  */
  91. function setup_xml_document( &$url ) {
  92.     global $DAYS_SINCE_UPDATE;
  93.     if( $DAYS_SINCE_UPDATE > 0 )
  94.         $date = date('dmY', strtotime('-'.$DAYS_SINCE_UPDATE.' days'));
  95.     else
  96.         $date = date('dmY');
  97.     $url = preg_replace( '/flt=Dsa~([0-9]{8})/i', "flt=Dsa~$date", $url );
  98.  
  99.     // Create a new DOM Document to hold our webpage structure
  100.     $xml = new DOMDocument();
  101.  
  102.     // Load the url's contents into the DOM
  103.     if( $xml->loadHTMLFile($url) === false ) {
  104.         http_response_code(404);
  105.         die( "Retrieval of <b>$url<b> failed." );
  106.     }
  107.  
  108.     return clean_xml_document($xml);
  109. }
  110.  
  111. /**
  112.  * Set up the headers for the file.
  113.  */
  114. function setup_headers() {
  115.     header("Content-Type: application/rss+xml");
  116.     echo '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
  117.     echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'."\n";
  118. }
  119.  
  120. /**
  121.  * Set up the meta data for the RSS feed.
  122.  */
  123. function get_channel( $url ) {
  124. ?>
  125.     <channel>
  126.         <title>GOG Game Updates via MaGog</title>
  127.         <description>Recent game updates on GOG.com made available by MaGog.</description>
  128.         <link><?php echo str_replace("&", "&amp;", $url) ?></link>
  129.         <atom:link href="http://<?php echo $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]; ?>" rel="self" type="application/rss+xml" />
  130. <?php
  131. }
  132.  
  133. /**
  134.  * Retrieve and format a game.
  135.  */
  136. function get_item( $node ) {
  137. ?>
  138.         <item>
  139.             <title><?php echo clean_game_title($node->childNodes->item(1)->nodeValue) ?></title>
  140.             <description><?php echo clean_description($node->nextSibling->firstChild); ?></description>
  141.             <link><?php echo $node->childNodes->item(1)->firstChild->getAttribute("href"); ?></link>
  142.             <guid><?php echo $node->childNodes->item(1)->firstChild->getAttribute("href"); ?></guid>
  143.         </item>
  144. <?php
  145. }
  146.  
  147. /**
  148.  * Run the thing. Maybe this will all become a class at some point?
  149.  */
  150.     $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;
  151.     $table = setup_xml_document($url);
  152.     setup_headers();
  153.     get_channel($url);
  154.    
  155.     if( $table !== false ) {
  156.         $i = 0;
  157.         foreach($table as $node) {
  158.             $i++;
  159.             if( $i % 2 == 0 ) continue;
  160.             get_item( $node );
  161.         }
  162.     }
  163. ?>
  164.     </channel>
  165. </rss>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement