WeLiveSecurityBridge.php 3.4 KB

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