PHP   28
ga rss
Guest on 27th August 2023 06:02:09 AM


  1. <?php
  2. /*
  3.  * Garfield comic by Jim Davis
  4.  * RSS wrapper by fishy ( http://yhsif.com )
  5.  * RSS wrapper source (what you are reading now) released under GPL v3
  6.  */
  7.  
  8. function shift_days($date, $days) {
  9.         return mktime(0, 0, 0, date("m", $date), date("d", $date) + $days, date("Y", $date));
  10. }
  11.  
  12. function date2img($date) {
  13.         return 'http://images.ucomics.com/comics/ga/' . date("Y", $date) . '/ga' . date("ymd", $date) . '.gif';
  14. }
  15.  
  16. function date2url($date) {
  17.         return 'http://www.gocomics.com/garfield/' . date("Y", $date) . '/' . date("m", $date) . '/' . date("d", $date);
  18. }
  19.  
  20. function find_next_available_date($date, $cachedate) {
  21.         $day = $date;
  22.         $fp = fopen("/dev/null", "w");
  23.         $i = 0;
  24.         while(1) {
  25.                 if($day <= $cachedate)
  26.                         break;
  27.                 $i++;
  28.                 if($i > 3) {
  29.                         $i--;
  30.                         break;
  31.                 }
  32.                 $ch = curl_init(date2img($day));
  33.                 curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
  34.                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  35.                 curl_setopt($ch, CURLOPT_FILE, $fp);
  36.                 curl_exec($ch);
  37.                 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  38.                 curl_close($ch);
  39.                 if($code == 200)
  40.                         break;
  41.                 $day = shift_days($day, -1);
  42.         }
  43.         fclose($fp);
  44.         print("<!-- fetched $i url(s) -->\n");
  45.         return $day;
  46. }
  47.  
  48. header("Content-Type: application/xml");
  49. header('Content-Disposition: inline; filename="rss2.xml"');
  50. print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  51.  
  52. $cache_file = ".rss-cache";
  53. $count = intval($_REQUEST['n']);
  54. if($count == 0)
  55.         $count = 10;
  56. $lastcheck = 0;
  57. $cacheday = 0;
  58. if(file_exists($cache_file)) {
  59.         $handle = fopen($cache_file, "r");
  60.         list($lastcheck, $cacheday) = fscanf($handle, "%u\t%u\n");
  61.         fclose($handle);
  62. }
  63. $lastday = $cacheday;
  64. if($cacheday == 0 || $lastcheck == 0 || (time() - $lastcheck) > 60*30) {
  65.         $lastday = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y"));
  66.         $lastday = find_next_available_date($lastday, $cacheday);
  67.         $handle = fopen($cache_file, "w");
  68.         fwrite($handle, sprintf("%u\t%u\n", time(), $lastday));
  69.         fclose($handle);
  70. } else
  71.         print("<!-- cache hit -->\n");
  72. ?>
  73. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  74. <channel>
  75. <title>Garfield daily comic</title>
  76. <link>http://www.garfield.com/comics/comics_todays.html</link>
  77. <description>The daily comic of Garfield, from ucomics.com</description>
  78. <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  79. <atom:link href="https://selif.yhsif.com/ga-rss.php" rel="self" type="application/rss+xml" />
  80. <?php
  81. for($i = 0; $i < $count; $i++) {
  82.         $img = date2img($lastday);
  83.         $url = date2url($lastday);
  84.         $title = "Garfield comic " . date("Y-m-d", $lastday);
  85.         $date = date("r", $lastday);
  86.         $desc = "<![CDATA[<img src=\"$img\" />]]>";
  87.         print <<<EOLAST
  88. <item>
  89. <title>$title</title>
  90. <link>$url</link>
  91. <guid>$url</guid>
  92. <pubDate>$date</pubDate>
  93. <description>$desc</description>
  94. </item>
  95.  
  96. EOLAST;
  97.         $lastday = shift_days($lastday, -1);
  98. }
  99. ?>
  100. </channel>
  101. </rss>

Raw Paste

Login or Register to edit or fork this paste. It's free.