LeMondeInformatiqueBridge.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. class LeMondeInformatiqueBridge extends BridgeAbstract {
  3. public function loadMetadatas() {
  4. $this->maintainer = "ORelio";
  5. $this->name = "Le Monde Informatique";
  6. $this->uri = "http://www.lemondeinformatique.fr/";
  7. $this->description = "Returns the newest articles.";
  8. }
  9. public function collectData(array $param) {
  10. function StripCDATA($string) {
  11. $string = str_replace('<![CDATA[', '', $string);
  12. $string = str_replace(']]>', '', $string);
  13. return $string;
  14. }
  15. function StripWithDelimiters($string, $start, $end) {
  16. while (strpos($string, $start) !== false) {
  17. $section_to_remove = substr($string, strpos($string, $start));
  18. $section_to_remove = substr($section_to_remove, 0, strpos($section_to_remove, $end) + strlen($end));
  19. $string = str_replace($section_to_remove, '', $string);
  20. } return $string;
  21. }
  22. function CleanArticle($article_html) {
  23. $article_html = StripWithDelimiters($article_html, '<script', '</script>');
  24. $article_html = StripWithDelimiters($article_html, '<h1 class="cleanprint-title"', '</h1>');
  25. return $article_html;
  26. }
  27. $feedUrl = 'http://www.lemondeinformatique.fr/rss/rss.xml';
  28. $html = $this->getSimpleHTMLDOM($feedUrl) or $this->returnServerError('Could not request LeMondeInformatique: '.$feedUrl);
  29. $limit = 0;
  30. foreach($html->find('item') as $element) {
  31. if($limit < 5) {
  32. //Retrieve article details
  33. $article_uri = $element->innertext;
  34. $article_uri = substr($article_uri, strpos($article_uri, '<link>') + 6);
  35. $article_uri = substr($article_uri, 0, strpos($article_uri, '</link>'));
  36. $article_html = $this->getSimpleHTMLDOM($article_uri) or $this->returnServerError('Could not request LeMondeInformatique: '.$article_uri);
  37. $article_content = CleanArticle($article_html->find('div#article', 0)->innertext);
  38. $article_title = $article_html->find('h1.cleanprint-title', 0)->plaintext;
  39. //Build and add final item
  40. $item = array();
  41. $item['uri'] = $article_uri;
  42. $item['title'] = $article_title;
  43. $item['author'] = StripCDATA($element->find('dc:creator', 0)->innertext);
  44. $item['timestamp'] = strtotime($element->find('dc:date', 0)->plaintext);
  45. $item['content'] = $article_content;
  46. $this->items[] = $item;
  47. $limit++;
  48. }
  49. }
  50. }
  51. public function getCacheDuration() {
  52. return 1800; // 30 minutes
  53. }
  54. }