Example #1
0
 /**
  * ADD News by cron
  * */
 public function CronNewsAction()
 {
     ini_set('max_execution_time', 0);
     $newsModel = new NewsModel();
     $sec_to_delete_news_from_feeds = $this->getParam("SEC_TO_DELETE_NEWS_FROM_FEEDS");
     $newsModel->deleteOldNews(date("Y-m-d H:i:s", time() - $sec_to_delete_news_from_feeds));
     $lastRSS = new lastRSS();
     $lastRSS->cache_dir = './rss_cache';
     $lastRSS->cache_time = 3600;
     // one hour
     $aNewsTreeFeeds = $newsModel->getAllNewsTreeFeeds("", true, true, true);
     foreach ($aNewsTreeFeeds as $newsTreeFeeds) {
         echo $newsTreeFeeds['url'];
         echo "<br>";
         $aFeeds = $lastRSS->Get($newsTreeFeeds['url']);
         echo "<pre>";
         //print_r($aFeeds);
         //print_r($newsTreeFeeds);
         //echo $newsTreeFeeds['last_parse_date']."<br>";
         $n = 0;
         if (is_array($aFeeds) && count($aFeeds) > 0 && is_array($aFeeds['items'])) {
             foreach ($aFeeds['items'] as $item) {
                 //print_r($item); echo "<hr>";
                 $pubDate = isset($item['pubDate']) ? $item['pubDate'] : date("Y-m-d H:i:s");
                 $title = isset($item['title']) ? $item['title'] : "";
                 $link = isset($item['link']) ? $item['link'] : "";
                 $description = isset($item['description']) ? $item['description'] : "";
                 $category = isset($item['category']) ? $item['category'] : "";
                 $enclosure = isset($item['enclosure']) ? $item['enclosure'] : "";
                 $enclosure_type = isset($item['enclosure_type']) ? $item['enclosure_type'] : "";
                 if (strtoupper($aFeeds['encoding']) != 'UTF-8') {
                     $title = iconv(strtoupper($aFeeds['encoding']), 'UTF-8', $title);
                     $description = iconv(strtoupper($aFeeds['encoding']), 'UTF-8', $description);
                     $category = iconv(strtoupper($aFeeds['encoding']), 'UTF-8', $category);
                     $enclosure = iconv(strtoupper($aFeeds['encoding']), 'UTF-8', $enclosure);
                 }
                 $short_text = $newsModel->getNWordsFromText($description, 40);
                 $pub_date = date("Y-m-d H:i:s", strtotime($pubDate));
                 if (!$newsTreeFeeds['category_tag'] || strtoupper($newsTreeFeeds['category_tag']) == strtoupper($category)) {
                     // if RSS-feeds have different categories => it should be same as in item
                     $pub_date_in_sec = strtotime($pub_date);
                     if ((!$newsTreeFeeds['last_parse_date'] || $newsTreeFeeds['last_parse_date'] < $pub_date) && time() - $sec_to_delete_news_from_feeds < $pub_date_in_sec) {
                         // not parsed yet
                         $n++;
                         $newsModel->addNews($newsTreeFeeds['id'], $title, $link, $short_text, $description, $category, $pub_date, $enclosure, $enclosure_type, 0, 0, 0, $newsTreeFeeds['text_parse_type']);
                         $newsModel->setParseDate($newsTreeFeeds['feed_id'], date("Y-m-d H:i:s"));
                     }
                 }
             }
         }
         echo "Added " . $n . " News";
         echo "</pre>";
         echo "<hr>";
     }
 }