|
| 1 | +<?php |
| 2 | +class FicbookBridge extends BridgeAbstract { |
| 3 | + |
| 4 | + const NAME = 'Ficbook Bridge'; |
| 5 | + const URI = 'https://ficbook.net/'; |
| 6 | + const DESCRIPTION = 'No description provided'; |
| 7 | + const MAINTAINER = 'logmanoriginal'; |
| 8 | + |
| 9 | + const PARAMETERS = array( |
| 10 | + 'Site News' => array(), |
| 11 | + 'Fiction Updates' => array( |
| 12 | + 'fiction_id' => array( |
| 13 | + 'name' => 'Fanfiction ID', |
| 14 | + 'type' => 'text', |
| 15 | + 'pattern' => '[0-9]+', |
| 16 | + 'required' => true, |
| 17 | + 'title' => 'Insert fanfiction ID', |
| 18 | + 'exampleValue' => '5783919', |
| 19 | + ), |
| 20 | + 'include_contents' => array( |
| 21 | + 'name' => 'Include contents', |
| 22 | + 'type' => 'checkbox', |
| 23 | + 'title' => 'Activate to include contents in the feed', |
| 24 | + ), |
| 25 | + ), |
| 26 | + 'Fiction Comments' => array( |
| 27 | + 'fiction_id' => array( |
| 28 | + 'name' => 'Fanfiction ID', |
| 29 | + 'type' => 'text', |
| 30 | + 'pattern' => '[0-9]+', |
| 31 | + 'required' => true, |
| 32 | + 'title' => 'Insert fanfiction ID', |
| 33 | + 'exampleValue' => '5783919', |
| 34 | + ), |
| 35 | + ), |
| 36 | + ); |
| 37 | + |
| 38 | + public function getURI() { |
| 39 | + switch($this->queriedContext) { |
| 40 | + case 'Site News': { |
| 41 | + // For some reason this is not HTTPS |
| 42 | + return 'http://ficbook.net/sitenews'; |
| 43 | + } |
| 44 | + case 'Fiction Updates': { |
| 45 | + return self::URI |
| 46 | + . 'readfic/' |
| 47 | + . urlencode($this->getInput('fiction_id')); |
| 48 | + } |
| 49 | + case 'Fiction Comments': { |
| 50 | + return self::URI |
| 51 | + . 'readfic/' |
| 52 | + . urlencode($this->getInput('fiction_id')) |
| 53 | + . '/comments#content'; |
| 54 | + } |
| 55 | + default: return parent::getURI(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public function collectData() { |
| 60 | + |
| 61 | + $header = array('Accept-Language: en-US'); |
| 62 | + |
| 63 | + $html = getSimpleHTMLDOM($this->getURI(), $header) |
| 64 | + or returnServerError('Could not request ' . $this->getURI()); |
| 65 | + |
| 66 | + $html = defaultLinkTo($html, self::URI); |
| 67 | + |
| 68 | + switch($this->queriedContext) { |
| 69 | + case 'Site News': return $this->collectSiteNews($html); |
| 70 | + case 'Fiction Updates': return $this->collectUpdatesData($html); |
| 71 | + case 'Fiction Comments': return $this->collectCommentsData($html); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + private function collectSiteNews($html) { |
| 77 | + foreach($html->find('.news_view') as $news) { |
| 78 | + $this->items[] = array( |
| 79 | + 'title' => $news->find('h1.title', 0)->plaintext, |
| 80 | + 'timestamp' => strtotime($this->fixDate($news->find('span[title]', 0)->title)), |
| 81 | + 'content' => $news->find('.news_text', 0), |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private function collectCommentsData($html) { |
| 87 | + foreach($html->find('article.post') as $article) { |
| 88 | + $this->items[] = array( |
| 89 | + 'uri' => $article->find('.comment_link_to_fic > a', 0)->href, |
| 90 | + 'title' => $article->find('.comment_author', 0)->plaintext, |
| 91 | + 'author' => $article->find('.comment_author', 0)->plaintext, |
| 92 | + 'timestamp' => strtotime($this->fixDate($article->find('time[datetime]', 0)->datetime)), |
| 93 | + 'content' => $article->find('.comment_message', 0), |
| 94 | + 'enclosures' => array($article->find('img', 0)->src), |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private function collectUpdatesData($html) { |
| 100 | + foreach($html->find('ul.table-of-contents > li') as $chapter) { |
| 101 | + $item = array( |
| 102 | + 'uri' => $chapter->find('a', 0)->href, |
| 103 | + 'title' => $chapter->find('a', 0)->plaintext, |
| 104 | + 'timestamp' => strtotime($this->fixDate($chapter->find('span[title]', 0)->title)), |
| 105 | + ); |
| 106 | + |
| 107 | + if($this->getInput('include_contents')) { |
| 108 | + $content = getSimpleHTMLDOMCached($item['uri']); |
| 109 | + $item['content'] = $content->find('#content', 0); |
| 110 | + } |
| 111 | + |
| 112 | + $this->items[] = $item; |
| 113 | + |
| 114 | + // Sort by time, descending |
| 115 | + usort($this->items, function($a, $b){ return $b['timestamp'] - $a['timestamp']; }); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private function fixDate($date) { |
| 120 | + |
| 121 | + // FIXME: This list was generated using Google tranlator. Someone who |
| 122 | + // actually knows russian should check this list! Please keep in mind |
| 123 | + // that month names must match exactly the names returned by Ficbook. |
| 124 | + $ru_month = array( |
| 125 | + 'января', |
| 126 | + 'февраля', |
| 127 | + 'марта', |
| 128 | + 'апреля', |
| 129 | + 'мая', |
| 130 | + 'июня', |
| 131 | + 'июля', |
| 132 | + 'августа', |
| 133 | + 'Сентября', |
| 134 | + 'октября', |
| 135 | + 'Ноября', |
| 136 | + 'Декабря', |
| 137 | + ); |
| 138 | + |
| 139 | + $en_month = array( |
| 140 | + 'January', |
| 141 | + 'February', |
| 142 | + 'March', |
| 143 | + 'April', |
| 144 | + 'May', |
| 145 | + 'June', |
| 146 | + 'July', |
| 147 | + 'August', |
| 148 | + 'September', |
| 149 | + 'October', |
| 150 | + 'November', |
| 151 | + 'December', |
| 152 | + ); |
| 153 | + |
| 154 | + $fixed_date = str_replace($ru_month, $en_month, $date); |
| 155 | + |
| 156 | + if($fixed_date === $date) { |
| 157 | + Debug::log('Unable to fix date: ' . $date); |
| 158 | + return null; |
| 159 | + } |
| 160 | + |
| 161 | + return $fixed_date; |
| 162 | + |
| 163 | + } |
| 164 | +} |
0 commit comments