1
0

TheHackerNewsBridge.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. $this->update = '2016-08-09';
  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 = $this->file_get_html($article_url) or $this->returnError('Could not request TheHackerNews: '.$article_url, 500);
  50. $contents = $article->find('div.articlebodyonly', 0)->innertext;
  51. $contents = StripRecursiveHTMLSection($contents, 'div', '<div class=\'clear\'');
  52. $contents = StripWithDelimiters($contents, '<script', '</script>');
  53. $item = new \Item();
  54. $item->uri = $article_url;
  55. $item->title = $article_title;
  56. $item->author = $article_author;
  57. $item->timestamp = $article_timestamp;
  58. $item->content = trim($contents);
  59. $this->items[] = $item;
  60. $limit++;
  61. }
  62. }
  63. }
  64. }