2016-08-14 13:46:16 +03:00
< ? php
class AskfmBridge extends BridgeAbstract {
public function loadMetadatas () {
$this -> maintainer = " az5he6ch " ;
$this -> name = " Ask.fm Answers " ;
$this -> uri = " http://ask.fm/ " ;
$this -> description = " Returns answers from an Ask.fm user " ;
2016-08-22 02:25:56 +03:00
$this -> parameters [ " Ask.fm username " ] = array (
'u' => array (
2016-08-25 00:41:33 +03:00
'name' => 'Username' ,
'required' => true
2016-08-22 02:25:56 +03:00
)
);
2016-08-14 13:46:16 +03:00
}
public function collectData ( array $param ){
$html = '' ;
if ( isset ( $param [ 'u' ])) {
$this -> request = $param [ 'u' ];
2016-08-14 16:27:49 +03:00
$html = $this -> getSimpleHTMLDOM ( 'http://ask.fm/' . urlencode ( $this -> request ) . '/answers/more?page=0' ) or $this -> returnServerError ( 'Requested username can\'t be found.' );
2016-08-14 13:46:16 +03:00
}
else {
2016-08-17 15:45:08 +03:00
$this -> returnClientError ( 'You must specify a username (?u=...).' );
2016-08-14 13:46:16 +03:00
}
foreach ( $html -> find ( 'div.streamItem-answer' ) as $element ) {
2016-08-22 19:55:59 +03:00
$item = array ();
$item [ 'uri' ] = 'http://ask.fm' . $element -> find ( 'a.streamItemsAge' , 0 ) -> href ;
2016-08-14 13:46:16 +03:00
$question = trim ( $element -> find ( 'h1.streamItemContent-question' , 0 ) -> innertext );
2016-08-22 19:55:59 +03:00
$item [ 'title' ] = trim ( htmlspecialchars_decode ( $element -> find ( 'h1.streamItemContent-question' , 0 ) -> plaintext , ENT_QUOTES ));
2016-08-14 13:46:16 +03:00
$answer = trim ( $element -> find ( 'p.streamItemContent-answer' , 0 ) -> innertext );
2016-08-22 19:55:59 +03:00
#$item['update'] = $element->find('a.streamitemsage',0)->data-hint; // Doesn't work, DOM parser doesn't seem to like data-hint, dunno why
2016-08-14 13:46:16 +03:00
$visual = $element -> find ( 'div.streamItemContent-visual' , 0 ) -> innertext ; // This probably should be cleaned up, especially for YouTube embeds
//Fix tracking links, also doesn't work
foreach ( $element -> find ( 'a' ) as $link ) {
if ( strpos ( $link -> href , 'l.ask.fm' ) !== false ) {
#$link->href = str_replace('#_=_', '', get_headers($link->href, 1)['Location']); // Too slow
$link -> href = $link -> plaintext ;
}
}
$content = '<p>' . $question . '</p><p>' . $answer . '</p><p>' . $visual . '</p>' ;
// Fix relative links without breaking // scheme used by YouTube stuff
$content = preg_replace ( '#href="\/(?!\/)#' , 'href="http://ask.fm/' , $content );
2016-08-22 19:55:59 +03:00
$item [ 'content' ] = $content ;
2016-08-14 13:46:16 +03:00
$this -> items [] = $item ;
}
}
public function getName (){
return empty ( $this -> request ) ? $this -> name : $this -> request ;
}
public function getURI (){
return empty ( $this -> request ) ? $this -> uri : 'http://ask.fm/' . urlencode ( $this -> request );
}
public function getCacheDuration (){
return 300 ; // 5 minutes
}
}