A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.4 KiB

  1. <?php
  2. namespace SparkLib\SocialNoise;
  3. use \SparkLib\Util\DateTime;
  4. class Reddit extends \SparkLib\SocialNoise {
  5. /**
  6. * Get an object representing search results.
  7. */
  8. public static function search ($text, $qty)
  9. {
  10. $url = "http://www.reddit.com/search.json?q={$text}&sort=new";
  11. return static::getSearchFromJson($url);
  12. }
  13. /**
  14. * Return search results formatted as HTML.
  15. *
  16. * @param $text to search for
  17. * @param $qty max number of items to return
  18. */
  19. public static function searchHTML ($text, $qty)
  20. {
  21. $h = function ($str) { return htmlspecialchars($str); };
  22. $html = '<ul>';
  23. $result = static::search($text, $qty);
  24. $i = 0;
  25. foreach ($result->data->children as $post) {
  26. if ($i++ > $qty) {
  27. break;
  28. }
  29. $post_title = $h(trim($post->data->title));
  30. if ($post->data->score >= 10)
  31. $post_title = "<b>$post_title</b>";
  32. $html .= '<li><a href="http://www.reddit.com/'
  33. . $h($post->data->permalink)
  34. . '">'
  35. . $post_title . "</a>";
  36. $author = $post->data->author;
  37. $html .= ' <small><i>' . $post->data->score . ' points, submitted '
  38. . DateTime::contextualTime($post->data->created_utc)
  39. . ' by <a href="http://www.reddit.com/u/'
  40. . $h($author) . '">' . $h($post->data->author) . '</a></i></small></li>';
  41. }
  42. return $html . '</ul>';
  43. }
  44. }