TheHackerNewsBridge.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. }
  14. return $string;
  15. }
  16. function stripRecursiveHtmlSection($string, $tag_name, $tag_start){
  17. $open_tag = '<' . $tag_name;
  18. $close_tag = '</' . $tag_name . '>';
  19. $close_tag_length = strlen($close_tag);
  20. if(strpos($tag_start, $open_tag) === 0) {
  21. while(strpos($string, $tag_start) !== false) {
  22. $max_recursion = 100;
  23. $section_to_remove = null;
  24. $section_start = strpos($string, $tag_start);
  25. $search_offset = $section_start;
  26. do {
  27. $max_recursion--;
  28. $section_end = strpos($string, $close_tag, $search_offset);
  29. $search_offset = $section_end + $close_tag_length;
  30. $section_to_remove = substr(
  31. $string,
  32. $section_start,
  33. $section_end - $section_start + $close_tag_length
  34. );
  35. $open_tag_count = substr_count($section_to_remove, $open_tag);
  36. $close_tag_count = substr_count($section_to_remove, $close_tag);
  37. } while($open_tag_count > $close_tag_count && $max_recursion > 0);
  38. $string = str_replace($section_to_remove, '', $string);
  39. }
  40. }
  41. return $string;
  42. }
  43. $html = getSimpleHTMLDOM($this->getURI())
  44. or returnServerError('Could not request TheHackerNews: ' . $this->getURI());
  45. $limit = 0;
  46. foreach($html->find('article') as $element) {
  47. if($limit < 5) {
  48. $article_url = $element->find('a.entry-title', 0)->href;
  49. $article_author = trim($element->find('span.vcard', 0)->plaintext);
  50. $article_title = $element->find('a.entry-title', 0)->plaintext;
  51. $article_timestamp = strtotime($element->find('span.updated', 0)->plaintext);
  52. $article = getSimpleHTMLDOM($article_url)
  53. or returnServerError('Could not request TheHackerNews: ' . $article_url);
  54. $contents = $article->find('div.articlebodyonly', 0)->innertext;
  55. $contents = stripRecursiveHtmlSection($contents, 'div', '<div class=\'clear\'');
  56. $contents = stripWithDelimiters($contents, '<script', '</script>');
  57. $item = array();
  58. $item['uri'] = $article_url;
  59. $item['title'] = $article_title;
  60. $item['author'] = $article_author;
  61. $item['timestamp'] = $article_timestamp;
  62. $item['content'] = trim($contents);
  63. $this->items[] = $item;
  64. $limit++;
  65. }
  66. }
  67. }
  68. }