LeMondeInformatiqueBridge.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. $this->update = "2016-01-28";
  9. }
  10. public function collectData(array $param) {
  11. function StripCDATA($string) {
  12. $string = str_replace('<![CDATA[', '', $string);
  13. $string = str_replace(']]>', '', $string);
  14. return $string;
  15. }
  16. function StripWithDelimiters($string, $start, $end) {
  17. while (strpos($string, $start) !== false) {
  18. $section_to_remove = substr($string, strpos($string, $start));
  19. $section_to_remove = substr($section_to_remove, 0, strpos($section_to_remove, $end) + strlen($end));
  20. $string = str_replace($section_to_remove, '', $string);
  21. } return $string;
  22. }
  23. function CleanArticle($article_html) {
  24. $article_html = StripWithDelimiters($article_html, '<script', '</script>');
  25. $article_html = StripWithDelimiters($article_html, '<h1 class="cleanprint-title"', '</h1>');
  26. return $article_html;
  27. }
  28. $feedUrl = 'http://www.lemondeinformatique.fr/rss/rss.xml';
  29. $html = $this->file_get_html($feedUrl) or $this->returnError('Could not request LeMondeInformatique: '.$feedUrl, 500);
  30. $limit = 0;
  31. foreach($html->find('item') as $element) {
  32. if($limit < 5) {
  33. //Retrieve article details
  34. $article_uri = $element->innertext;
  35. $article_uri = substr($article_uri, strpos($article_uri, '<link>') + 6);
  36. $article_uri = substr($article_uri, 0, strpos($article_uri, '</link>'));
  37. $article_html = $this->file_get_html($article_uri) or $this->returnError('Could not request LeMondeInformatique: '.$article_uri, 500);
  38. $thumbnailUri = $article_html->find('div#article', 0)->find('img#illustration', 0)->src;
  39. $article_content = CleanArticle($article_html->find('div#article', 0)->innertext);
  40. $article_title = $article_html->find('h1.cleanprint-title', 0)->plaintext;
  41. //Build and add final item
  42. $item = new \Item();
  43. $item->uri = $article_uri;
  44. $item->thumbnailUri = $thumbnailUri;
  45. $item->title = $article_title;
  46. $item->author = StripCDATA($element->find('dc:creator', 0)->innertext);
  47. $item->timestamp = strtotime($element->find('dc:date', 0)->plaintext);
  48. $item->content = $article_content;
  49. $this->items[] = $item;
  50. $limit++;
  51. }
  52. }
  53. }
  54. public function getName() {
  55. return 'Le Monde Informatique';
  56. }
  57. public function getURI() {
  58. return 'http://www.lemondeinformatique.fr/';
  59. }
  60. public function getCacheDuration() {
  61. return 1800; // 30 minutes
  62. }
  63. }