TheHackerNewsBridge.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. class TheHackerNewsBridge extends BridgeAbstract {
  3. public function loadMetadatas() {
  4. $this->maintainer = 'ORelio';
  5. $this->name = $this->getName();
  6. $this->uri = $this->getURI();
  7. $this->description = 'Cyber Security, Hacking, Technology News.';
  8. $this->update = '2016-07-22';
  9. }
  10. public function collectData(array $param) {
  11. function StripWithDelimiters($string, $start, $end) {
  12. while (strpos($string, $start) !== false) {
  13. $section_to_remove = substr($string, strpos($string, $start));
  14. $section_to_remove = substr($section_to_remove, 0, strpos($section_to_remove, $end) + strlen($end));
  15. $string = str_replace($section_to_remove, '', $string);
  16. } return $string;
  17. }
  18. function StripRecursiveHTMLSection($string, $tag_name, $tag_start) {
  19. $open_tag = '<'.$tag_name;
  20. $close_tag = '</'.$tag_name.'>';
  21. $close_tag_length = strlen($close_tag);
  22. if (strpos($tag_start, $open_tag) === 0) {
  23. while (strpos($string, $tag_start) !== false) {
  24. $max_recursion = 100;
  25. $section_to_remove = null;
  26. $section_start = strpos($string, $tag_start);
  27. $search_offset = $section_start;
  28. do {
  29. $max_recursion--;
  30. $section_end = strpos($string, $close_tag, $search_offset);
  31. $search_offset = $section_end + $close_tag_length;
  32. $section_to_remove = substr($string, $section_start, $section_end - $section_start + $close_tag_length);
  33. $open_tag_count = substr_count($section_to_remove, $open_tag);
  34. $close_tag_count = substr_count($section_to_remove, $close_tag);
  35. } while ($open_tag_count > $close_tag_count && $max_recursion > 0);
  36. $string = str_replace($section_to_remove, '', $string);
  37. }
  38. }
  39. return $string;
  40. }
  41. $html = $this->file_get_html($this->getURI()) or $this->returnError('Could not request TheHackerNews: '.$this->getURI(), 500);
  42. $limit = 0;
  43. foreach ($html->find('article') as $element) {
  44. if ($limit < 5) {
  45. $article_url = $element->find('a.entry-title', 0)->href;
  46. $article_author = trim($element->find('span.vcard', 0)->plaintext);
  47. $article_title = $element->find('a.entry-title', 0)->plaintext;
  48. $article_timestamp = strtotime($element->find('span.updated', 0)->plaintext);
  49. $article_thumbnail = $element->find('img', 0)->src;
  50. $article = $this->file_get_html($article_url) or $this->returnError('Could not request TheHackerNews: '.$article_url, 500);
  51. $contents = $article->find('div.articlebodyonly', 0)->innertext;
  52. $contents = StripRecursiveHTMLSection($contents, 'div', '<div class=\'clear\'');
  53. $contents = StripWithDelimiters($contents, '<script', '</script>');
  54. $item = new \Item();
  55. $item->uri = $article_url;
  56. $item->title = $article_title;
  57. $item->author = $article_author;
  58. $item->thumbnailUri = $article_thumbnail;
  59. $item->timestamp = $article_timestamp;
  60. $item->content = trim($contents);
  61. $this->items[] = $item;
  62. $limit++;
  63. }
  64. }
  65. }
  66. public function getName() {
  67. return 'The Hacker News Bridge';
  68. }
  69. public function getURI() {
  70. return 'https://thehackernews.com/';
  71. }
  72. public function getCacheDuration() {
  73. return 3600; //1 hour
  74. }
  75. }