WeLiveSecurityBridge.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. class WeLiveSecurityBridge extends BridgeAbstract {
  3. public function loadMetadatas() {
  4. $this->maintainer = 'ORelio';
  5. $this->name = 'We Live Security';
  6. $this->uri = 'http://www.welivesecurity.com/';
  7. $this->description = 'Returns the newest articles.';
  8. }
  9. public function collectData(array $param) {
  10. function ExtractFromDelimiters($string, $start, $end) {
  11. if (strpos($string, $start) !== false) {
  12. $section_retrieved = substr($string, strpos($string, $start) + strlen($start));
  13. $section_retrieved = substr($section_retrieved, 0, strpos($section_retrieved, $end));
  14. return $section_retrieved;
  15. } return false;
  16. }
  17. function StripWithDelimiters($string, $start, $end) {
  18. while (strpos($string, $start) !== false) {
  19. $section_to_remove = substr($string, strpos($string, $start));
  20. $section_to_remove = substr($section_to_remove, 0, strpos($section_to_remove, $end) + strlen($end));
  21. $string = str_replace($section_to_remove, '', $string);
  22. } return $string;
  23. }
  24. $feed = $this->getURI().'feed/';
  25. $html = $this->getSimpleHTMLDOM($feed) or $this->returnServerError('Could not request '.$this->getName().': '.$feed);
  26. $limit = 0;
  27. foreach ($html->find('item') as $element) {
  28. if ($limit < 5) {
  29. $article_image = $element->find('image', 0)->plaintext;
  30. $article_url = ExtractFromDelimiters($element->innertext, '<link>', '</link>');
  31. $article_summary = ExtractFromDelimiters($element->innertext, '<description><![CDATA[<p>', '</p>');
  32. $article_html = file_get_contents($article_url) or $this->returnServerError('Could not request '.$this->getName().': '.$article_url);
  33. if (substr($article_html, 0, 2) == "\x1f\x8b") //http://www.gzip.org/zlib/rfc-gzip.html#header-trailer -> GZip ID1
  34. $article_html = gzdecode($article_html); //Response is GZipped even if we didn't accept GZip!? Let's decompress...
  35. $article_html = str_get_html($article_html); //Now we have our HTML data. But still, that's an important HTTP violation...
  36. $article_content = $article_html->find('div.wlistingsingletext', 0)->innertext;
  37. $article_content = StripWithDelimiters($article_content, '<script', '</script>');
  38. $article_content = '<p><img src="'.$article_image.'" /></p>'
  39. .'<p><b>'.$article_summary.'</b></p>'
  40. .trim($article_content);
  41. $item = array();
  42. $item['uri'] = $article_url;
  43. $item['title'] = $element->find('title', 0)->plaintext;
  44. $item['author'] = $article_html->find('a[rel=author]', 0)->plaintext;
  45. $item['timestamp'] = strtotime($element->find('pubDate', 0)->plaintext);
  46. $item['content'] = $article_content;
  47. $this->items[] = $item;
  48. $limit++;
  49. }
  50. }
  51. }
  52. }