AskfmBridge.php 2.3 KB

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