public function run($url)
 {
     // 前回のデータを取得
     $lastFeedFilename = storage_path() . $this->convertUrlToFileName($url);
     $lastFeedTime = $this->reader->read($lastFeedFilename, '2000-01-01 00:00:00');
     // 保存していた最終アイテムの投稿時間を取得
     $lastTime = Carbon::createFromFormat('Y-m-d H:i:s', trim($lastFeedTime), 'Asia/Tokyo');
     // RSS取得
     try {
         $fastFeed = Factory::create();
         $fastFeed->addFeed('default', $url);
         $items = $fastFeed->fetch('default');
     } catch (Exception $e) {
         Log::error('RSS取得失敗。' . $e->getMessage());
         return false;
     }
     // 取得できなければ終了
     if (empty($items)) {
         return false;
     }
     $event = new FeedPosted();
     foreach ($items as $item) {
         // 以前の最新フィードより新しい物だけを処理
         $itemTime = Carbon::instance($item->getDate());
         if ($itemTime->gt($lastTime)) {
             // 取得情報をイベントへ!!
             $event->author = $item->getAuthor();
             $event->content = html_entity_decode($item->getContent());
             $event->date = Carbon::instance($item->getDate());
             $event->id = $item->getId();
             $event->image = $item->getImage();
             $event->intro = html_entity_decode($item->getIntro());
             $event->name = html_entity_decode($item->getName());
             $event->source = $item->getSource();
             $event->tags = $item->getTags();
             // イベント発行
             $this->dispatcher->fire($event);
         }
     }
     if (count($items) >= 1) {
         // 最新アイテムの投稿時間を
         // 目認しやすいように日本時間で保存する
         $latestTime = Carbon::instance(head($items)->getDate());
         $this->file->put($lastFeedFilename, $latestTime->timezone('Asia/Tokyo')->toDateTimeString());
     }
     return true;
 }
Пример #2
0
 public function testCreate()
 {
     $this->assertInstanceOf('FastFeed\\FastFeed', Factory::create());
 }
Пример #3
0
 public function testCreate()
 {
     HttpAdapterFactory::register('mock_adapter', 'Ivory\\HttpAdapter\\MockHttpAdapter');
     $this->assertInstanceOf('FastFeed\\FastFeed', Factory::create('mock_adapter'));
 }
Пример #4
0
 /**
  * Scrape the RSS feed
  * @since Version 3.9
  * @return \Railpage\News\Scraper
  */
 public function fetch()
 {
     if (!is_string($this->feed)) {
         throw new Exception("Cannot fetch news articles from RSS feed because no RSS feed was provided");
     }
     $articles = array();
     $FastFeed = FastFeedFactory::create();
     $FastFeed->addFeed('default', $this->feed);
     $FastFeed->pushProcessor(new RemoveStylesProcessor());
     #$FastFeed->pushParser(new RailpageParser);
     /**
      * Remove tags
      */
     $StripTagsProcessor = new StripTagsProcessor();
     $StripTagsProcessor->setAllowedTagsForContent("img, a, ul, li, ol, strong, i, em, table, tr, td, th, thead, tbody, tfoot");
     $StripTagsProcessor->setAllowedTagsForIntro("a, ul, li, ol, strong, i, em, table, tr, td, th, thead, tbody, tfoot");
     $FastFeed->pushProcessor($StripTagsProcessor);
     $items = $FastFeed->fetch('default');
     printArray($items);
     die;
     foreach ($items as $Item) {
         $content = $Item->getContent();
         #printArray($Item->getExtra("category"));
         $date = $Item->getDate();
         $row = array("title" => $Item->getName(), "date" => $date->setTimeZone(new DateTimeZone("Australia/Melbourne")), "source" => $Item->getSource(), "blurb" => $Item->getIntro(), "body" => $Item->getContent(), "topic" => News::guessTopic($topic));
         printArray($row);
         die;
     }
     $articles[] = $row;
     $this->articles = $articles;
     /**
      * Zend HTTP config
      */
     $config = array('adapter' => 'Zend\\Http\\Client\\Adapter\\Curl', 'curloptions' => array(CURLOPT_FOLLOWLOCATION => true));
     $client = new Client($this->feed, $config);
     /**
      * Fetch the RSS feed
      */
     $response = $client->send();
     $content = $response->getBody();
     /**
      * Load the SimpleXML object
      */
     $xml = new SimpleXMLElement($content);
     /**
      * Load the namespaces
      */
     $ns = $xml->getNamespaces(true);
     /**
      * Loop through each RSS item and build an associative array of the data we need
      */
     foreach ($xml->channel->item as $item) {
         if (isset($ns['content']) && !empty($ns['content'])) {
             $content = $item->children($ns['content']);
             $content = strval($content->encoded);
         } else {
             $content = $item->description->__toString();
             $content = strip_tags($content, "img,a");
         }
         #printArray($content->__toString());die;
         $topic = json_decode(json_encode($item->category), true);
         if (empty($topic)) {
             $topic = $this->feed;
         }
         $line = explode("\n", $content);
         $firstline = preg_replace('/([^?!.]*.).*/', '\\1', strip_tags($line[0]));
         $body = trim(str_replace($firstline, "", $content));
         $row = array("title" => strval($item->title), "date" => (new DateTime(strval($item->pubDate)))->setTimeZone(new DateTimeZone("Australia/Melbourne")), "source" => strval($item->link), "blurb" => $firstline, "body" => $body, "topic" => News::guessTopic($topic));
         /**
          * Add this article to the list of news articles found in this scrape
          */
         $articles[] = $row;
     }
     $this->articles = $articles;
     return $this;
 }