protected function createNews(PendingNews $pn)
 {
     if ($npn = Npn::findOne(["pending_news_id" => $pn->id])) {
         return $npn->news_id;
     }
     $news = new News();
     $news->title = NewsParserComponent::replace4byte($pn->title);
     $news->thumb = $pn->thumb_src;
     $news->status = "in_process";
     $news->cnt = 0;
     $news->created_at = new \yii\db\Expression('NOW()');
     $news->updated_at = new \yii\db\Expression('NOW()');
     if ($news->save()) {
         $this->linkNews($news->id, $pn->id);
         $news->status = "done";
         $news->save();
         if ($news->thumb && PageLoaderComponent::checkRemoteFile($news->thumb)) {
             $mq = new RabbitMQComponent();
             $mq->postMessage("image", "image", json_encode(["news_id" => $news->id, "src" => $news->thumb]));
             $mq->postMessage("twitter", "twitter", json_encode(["news_id" => $news->id, "src" => $news->thumb]));
         } else {
             if ($giData = PageLoaderComponent::load("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" . urlencode($news->title) . "&userip=127.0.0.1&imgsz=large")) {
                 $data = json_decode($giData);
                 if (isset($data->responseData->results[0])) {
                     $news->thumb = $data->responseData->results[0]->unescapedUrl;
                     $news->save();
                     $mq = new RabbitMQComponent();
                     $mq->postMessage("image", "image", json_encode(["news_id" => $news->id, "src" => $data->responseData->results[0]->unescapedUrl]));
                     $mq->postMessage("twitter", "twitter", json_encode(["news_id" => $news->id, "src" => $data->responseData->results[0]->unescapedUrl]));
                 }
             }
         }
     } else {
         print_r($news->getErrors());
     }
     return $news->id;
 }
示例#2
0
 /**
  * Finds the Npn model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  *
  * @param integer $news_id
  * @param integer $pending_news_id
  *
  * @return Npn the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($news_id, $pending_news_id)
 {
     if (($model = Npn::findOne(['news_id' => $news_id, 'pending_news_id' => $pending_news_id])) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }