AskfmBridge.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. class AskfmBridge extends BridgeAbstract {
  3. const MAINTAINER = 'az5he6ch';
  4. const NAME = 'Ask.fm Answers';
  5. const URI = 'https://ask.fm/';
  6. const CACHE_TIMEOUT = 300; //5 min
  7. const DESCRIPTION = 'Returns answers from an Ask.fm user';
  8. const PARAMETERS = array(
  9. 'Ask.fm username' => array(
  10. 'u' => array(
  11. 'name' => 'Username',
  12. 'required' => true
  13. )
  14. )
  15. );
  16. public function collectData(){
  17. $html = getSimpleHTMLDOM($this->getURI())
  18. or returnServerError('Requested username can\'t be found.');
  19. foreach($html->find('div.streamItem-answer') as $element) {
  20. $item = array();
  21. $item['uri'] = self::URI . $element->find('a.streamItemsAge', 0)->href;
  22. $question = trim($element->find('h1.streamItemContent-question', 0)->innertext);
  23. $item['title'] = trim(
  24. htmlspecialchars_decode($element->find('h1.streamItemContent-question', 0)->plaintext,
  25. ENT_QUOTES
  26. )
  27. );
  28. $answer = trim($element->find('p.streamItemContent-answer', 0)->innertext);
  29. // Doesn't work, DOM parser doesn't seem to like data-hint, dunno why
  30. #$item['update'] = $element->find('a.streamitemsage',0)->data-hint;
  31. // This probably should be cleaned up, especially for YouTube embeds
  32. $visual = $element->find('div.streamItemContent-visual', 0)->innertext;
  33. //Fix tracking links, also doesn't work
  34. foreach($element->find('a') as $link) {
  35. if(strpos($link->href, 'l.ask.fm') !== false) {
  36. // Too slow
  37. #$link->href = str_replace('#_=_', '', get_headers($link->href, 1)['Location']);
  38. $link->href = $link->plaintext;
  39. }
  40. }
  41. $content = '<p>' . $question . '</p><p>' . $answer . '</p><p>' . $visual . '</p>';
  42. // Fix relative links without breaking // scheme used by YouTube stuff
  43. $content = preg_replace('#href="\/(?!\/)#', 'href="' . self::URI, $content);
  44. $item['content'] = $content;
  45. $this->items[] = $item;
  46. }
  47. }
  48. public function getName(){
  49. if(!is_null($this->getInput('u'))) {
  50. return self::NAME . ' : ' . $this->getInput('u');
  51. }
  52. return parent::getName();
  53. }
  54. public function getURI(){
  55. if(!is_null($this->getInput('u'))) {
  56. return self::URI . urlencode($this->getInput('u')) . '/answers/more?page=0';
  57. }
  58. return parent::getURI();
  59. }
  60. }