Exemple #1
0
 public function actionTest()
 {
     $data = [];
     if (Yii::$app->request->post()) {
         $data = Yii::$app->request->post();
         if (isset($data['url'])) {
             $parser = new NewsParserComponent(ParserQueue::findOne(1));
             $data['parserResult'] = $parser->parse(PageLoaderComponent::load($data['url']), $data['url'], Sources::findOne($data['Source']));
         }
     }
     return $this->render('test', $data);
 }
 public static function processMessage($msg)
 {
     //            print_r($msg);
     try {
         $params = json_decode($msg->body);
         print_r($params);
         $pqItem = ParserQueue::findOne(["id" => $params->pq_id]);
         $pnItem = PendingNews::findOne(["id" => $params->pn_id]);
         if ($pqItem && $pnItem) {
             $newsParser = new NewsParserComponent($pqItem, $pnItem);
             $newsParser->run();
         }
     } catch (Exception $e) {
         echo $e->getMessage();
         if ($e->getCode() != 505) {
             $mq = new RabbitMQComponent();
             $mq->postMessage("parse", "parse_rss", $msg->body);
         }
     }
     $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
     //            die();
 }
Exemple #3
0
 public static function fillTags($content, $id_news)
 {
     if ($tagList = KeywordDetector::detect($content)) {
         foreach ($tagList as $tagName) {
             $tagName = NewsParserComponent::replace4byte($tagName);
             $tag = Tags::findOne(['name' => $tagName]);
             if (!$tag) {
                 $tag = new Tags();
                 $tag->name = $tagName;
                 $tag->cnt = 0;
                 $tag->save();
             }
             $nht = new NewsHasTags();
             $nht->news_id = $id_news;
             $nht->tag_id = $tag->id;
             if ($nht->save()) {
                 $tag->updateCounters(['cnt' => 1]);
             }
         }
     }
 }
 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;
 }