Advertisement
pac1250

RSS Canal+

Dec 15th, 2013
230
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // v1.0.0
  3.  
  4. // params
  5. $force = array_key_exists('force', $_GET) ? true : false;
  6. $prog = $_GET['prog'];
  7.  
  8. $savefile = "config.$prog.json";
  9. if(!file_exists($savefile)) die("unknown prog '{$prog}'");
  10. $data = json_decode(file_get_contents($savefile), true) or die('failed reading data file');
  11.  
  12. // not from feedburner
  13. if(!$force
  14.     && (!array_key_exists('HTTP_USER_AGENT', $_SERVER) || strpos($_SERVER['HTTP_USER_AGENT'], 'FeedBurner') === FALSE)
  15.     && (array_key_exists('Feed', $data))
  16. ) {
  17.     header("Status: 301 Moved Permanently", false, 301);
  18.     header("Location: {$data['Feed']}");
  19.     die();
  20. }
  21.  
  22. $last_generation_time = filemtime($savefile) or die('failed reading data file age');
  23.  
  24. // file older than 6 hours ?
  25. if ($force || ((time() - $last_generation_time) > 21600)) {
  26.  
  27.     $url = $data['Page'];
  28.     $fetched = file_get_contents($url) or die('failed fetching feed');
  29.     $reg = '#<li id="video_(?<vid>\\d+)"#ims'; //#<li id="video_(?<vid>\d+)"#ims
  30.    
  31.     if(preg_match_all($reg, $fetched, $matches, PREG_SET_ORDER)) {
  32.    
  33.         $items = array();
  34.         foreach($matches as $match) {
  35.             $vid = (int)$match['vid'];
  36.             $title = sprintf("%s %s", $data['Title'], $vid);
  37.             $url = sprintf("%s?vid=%s", $data['Page'], $vid);
  38.            
  39.             if(strlen($title) == 0 || strpos($title, '<img') !== FALSE)
  40.                 continue;
  41.            
  42.             $items[$vid] = array(
  43.                 'VID' => $vid,
  44.                 'TITLE' => $title,
  45.                 'URL' => $url
  46.             );
  47.         }
  48.         krsort($items);
  49.     }
  50.    
  51.     if (count($items) > 0) {
  52.         $data['Items'] = array_values($items);
  53.         file_put_contents($savefile, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)) or die('failed saving data file');
  54.     }
  55.  
  56.     $last_generation_time = time();
  57. }
  58.  
  59. header('Content-Type: application/rss+xml; charset=utf-8');
  60. echo '<'.'?'.'xml version="1.0" encoding="utf-8"'.'?'.'>';
  61. ?>
  62.  
  63. <rss version="2.0">
  64.     <channel>
  65.         <title><?=htmlspecialchars($data['Title'])?></title>
  66.         <description><?=htmlspecialchars($data['Title'])?></description>
  67.         <lastBuildDate><?=date('r', $last_generation_time)?></lastBuildDate>
  68.         <link>http://www.canalplus.fr/</link>
  69. <?php foreach($data['Items'] as $item): ?>
  70.         <item>
  71.             <title><?=htmlspecialchars($item['TITLE'])?></title>
  72.             <link><?=htmlspecialchars($item['URL'])?></link>
  73.         </item>
  74. <?php endforeach; ?>
  75.     </channel>
  76. </rss>
Advertisement
RAW Paste Data Copied
Advertisement