TheHackerNewsBridge.php 3.3 KB

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