TheHackerNewsBridge.php 3.4 KB

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