]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/MOBIClass/downloaders/FanFictionNet.php
4 * Description of FanFictionNet
8 class FanFictionNet
extends MultipleFileHandler
{
9 private static $prefix = "http://www.fanfiction.net/s/";
10 private $downloadedMetadata = false;
12 private $chapterCount = -1;
14 public function __construct($url) {
15 $ending = substr($url, strlen(self
::$prefix));
16 $this->id
= intval(substr($ending, 0, strpos($ending, "/")));
18 for($i = 1; $i <= max(1, $this->chapterCount
); $i++
){
19 $this->addChapter($i);
23 private function addChapter($n){
24 $doc = new DOMDocument();
25 $file = Http
::Request(self
::$prefix.$this->id
."/".$n."/");
26 @$doc->loadHTML($file) or die($file);
28 if(!$this->downloadedMetadata
){
29 $this->loadMetadata($doc);
30 $this->downloadedMetadata
= true;
32 if($this->chapterCount
< 0){
33 $this->chapterCount
= $this->getNumberChapters($doc);
35 if($this->chapterCount
> 4){
36 die("Too many files to download, don't use php for this!");
40 $textEl = $doc->getElementById("storytext");
41 if($textEl == null) die("Error: ".$doc->saveHTML());
42 $horizontalRulebars = $doc->getElementsByTagName('hr');
47 foreach($horizontalRulebars as $hr) {
48 $hr->setAttribute("size", null);
49 $hr->setAttribute("noshade", null);
51 $text = $this->innerHtml($textEl);
54 $selects = $doc->getElementsByTagName('select');
55 foreach($selects as $select) {
56 if($select->hasAttribute("name") && $select->getAttribute("name") == "chapter"){
57 $options = $select->getElementsByTagName("option");
60 foreach($options as $option){
61 $val = $option->nodeValue
;
62 if(substr($val, 0, strlen($test)) == $test){
63 $title = substr($val, strlen($test));
70 $this->addPage($text, $title);
73 private function getNumberChapters($doc){
74 $selects = $doc->getElementsByTagName('select');
75 foreach($selects as $select) {
76 if($select->hasAttribute("name") && $select->getAttribute("name") == "chapter"){
77 $options = $select->getElementsByTagName("option");
79 $count = $options->length
;
85 private function loadMetadata($doc){
87 $links = $doc->getElementsByTagName('a');
88 foreach($links as $link) {
92 if($link->hasAttribute("href") && substr($link->getAttribute("href"), 0, 3) == "/u/"){
93 $this->setMetadata("author", $link->nodeValue
);
98 $links = $doc->getElementsByTagName('link');
99 foreach($links as $link) {
100 if($link->hasAttribute("rel") && $link->getAttribute("rel") == "canonical"){
101 $url = $link->getAttribute("href");
102 $title = str_replace("_", " ", substr($url, strrpos($url, "/")+1));
103 $this->setMetadata("title", $title);
107 //TODO: Find a more reliable way to extract the title
108 $title = $doc->getElementsByTagName("b")->item(0)->nodeValue
;
109 $this->setMetadata("title", $title);
112 private function innerHtml($node){
113 $doc = new DOMDocument();
114 foreach ($node->childNodes
as $child)
115 $doc->appendChild($doc->importNode($child, true));
117 return $doc->saveHTML();
120 public static function Matches($url){
121 //TODO: Implement with regex
122 return strpos($url, self
::$prefix) !== false;