AskfmBridge.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. class AskfmBridge extends BridgeAbstract{
  3. const MAINTAINER = "az5he6ch";
  4. const NAME = "Ask.fm Answers";
  5. const URI = "http://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(htmlspecialchars_decode($element->find('h1.streamItemContent-question',0)->plaintext, ENT_QUOTES));
  24. $answer = trim($element->find('p.streamItemContent-answer',0)->innertext);
  25. #$item['update'] = $element->find('a.streamitemsage',0)->data-hint; // Doesn't work, DOM parser doesn't seem to like data-hint, dunno why
  26. $visual = $element->find('div.streamItemContent-visual',0)->innertext; // This probably should be cleaned up, especially for YouTube embeds
  27. //Fix tracking links, also doesn't work
  28. foreach($element->find('a') as $link) {
  29. if (strpos($link->href, 'l.ask.fm') !== false) {
  30. #$link->href = str_replace('#_=_', '', get_headers($link->href, 1)['Location']); // Too slow
  31. $link->href = $link->plaintext;
  32. }
  33. }
  34. $content = '<p>' . $question . '</p><p>' . $answer . '</p><p>' . $visual . '</p>';
  35. // Fix relative links without breaking // scheme used by YouTube stuff
  36. $content = preg_replace('#href="\/(?!\/)#', 'href="'.self::URI,$content);
  37. $item['content'] = $content;
  38. $this->items[] = $item;
  39. }
  40. }
  41. public function getName(){
  42. return self::NAME.' : '.$this->getInput('u');
  43. }
  44. public function getURI(){
  45. return self::URI.urlencode($this->getInput('u')).'/answers/more?page=0';
  46. }
  47. }