rss-bridge-flickr-explore.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /* Flickr Explorer RSS bridge.
  3. Returns a feed all new interesting images from http://www.flickr.com/explore
  4. Licence: Public domain.
  5. Returns ATOM feed by default.
  6. Other available formats: ?format=plaintext and ?format=json
  7. */
  8. ini_set('user_agent', 'Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0');
  9. date_default_timezone_set('UTC');
  10. //ini_set('display_errors','1');
  11. //error_reporting(E_ALL);
  12. function returnError($code, $message) { header("HTTP/1.1 $code"); header('content-type: text/plain'); die($message); }
  13. if (!file_exists('simple_html_dom.php')) { returnError('404 Not Found', 'ERROR: "PHP Simple HTML DOM Parser" is missing. Get it from http://simplehtmldom.sourceforge.net/ and place the script "simple_html_dom.php" in the same folder to allow me to work.'); }
  14. require_once('simple_html_dom.php');
  15. $html = file_get_html('http://www.flickr.com/explore') or returnError('404 Not Found', 'ERROR: could not request Flickr');
  16. $items = Array();
  17. foreach($html->find('span.photo_container') as $element)
  18. {
  19. $item['href'] = 'http://flickr.com'.$element->find('a',0)->href; // Page URI
  20. $item['thumbnailUri'] = $element->find('img',0)->getAttribute('data-defer-src'); // Thumbnail URI
  21. $item['title'] = $element->find('a',0)->title; // Photo title
  22. $items[] = $item;
  23. }
  24. if(empty($items)) { returnError('404 Not Found', 'ERROR: no results.'); }
  25. $format = 'atom';
  26. if (!empty($_GET['format'])) { $format = $_GET['format']; }
  27. switch($format)
  28. {
  29. case 'plaintext':
  30. case 'json':
  31. case 'atom':
  32. break;
  33. default:
  34. $format='atom';
  35. }
  36. if($format == 'plaintext') { header('content-type: text/plain;charset=utf8'); print_r($items); exit; }
  37. if($format == 'json') { header('content-type: application/json'); $items = json_encode($items); exit($items); }
  38. if($format == 'atom')
  39. {
  40. header('content-type: application/atom+xml; charset=UTF-8');
  41. echo '<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xml:lang="en-US">'."\n";
  42. echo '<title type="text">Flickr Explore</title>'."\n";
  43. echo '<id>http'.(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : '')."://{$_SERVER['HTTP_HOST']}{$_SERVER['PATH_INFO']}".'/</id>'."\n";
  44. echo '<updated>'.date(DATE_ATOM, $tweets['0']['timestamp']).'</updated>'."\n";
  45. echo '<link rel="alternate" type="text/html" href="http://www.flickr.com/explore" />'."\n";
  46. echo '<link rel="self" href="http'.(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : '')."://{$_SERVER['HTTP_HOST']}".htmlentities($_SERVER['REQUEST_URI']).'" />'."\n"."\n";
  47. foreach($items as $item) {
  48. echo '<entry><author><name>Flickr</name><uri>http://flickr.com/</uri></author>'."\n";
  49. echo '<title type="html"><![CDATA['.$item['title'].']]></title>'."\n";
  50. echo '<link rel="alternate" type="text/html" href="'.$item['href'].'" />'."\n";
  51. echo '<id>'.$item['href'].'</id>'."\n";
  52. echo '<updated></updated>'."\n"; // FIXME: date ???
  53. echo '<content type="html"><![CDATA[<a href="'.$item['href'].'"><img src="'.$item['thumbnailUri'].'" /></a>]]></content>'."\n";
  54. echo '</entry>'."\n\n";
  55. }
  56. echo '</feed>';
  57. exit;
  58. }
  59. exit();