Esempio n. 1
0
 public function updateEntries()
 {
     try {
         // Create a new instance of the SimplePie object
         $feed = new SimplePie();
         $feed->set_feed_url($this->feed);
         $feed->set_cache_location(__DIR__ . '/../../cache');
         // Initialize the whole SimplePie object.  Read the feed, process it, parse it, cache it, and
         // all that other good stuff.  The feed's information will not be available to SimplePie before
         // this is called.
         $success = $feed->init();
         // We'll make sure that the right content type and character encoding gets set automatically.
         // This function will grab the proper character encoding, as well as set the content type to text/html.
         $feed->handle_content_type();
     } catch (\Exception $e) {
         return false;
     }
     if ($feed->error() || !$success) {
         return false;
     }
     foreach ($feed->get_items() as $item) {
         try {
             /** @var SimplePie_Item $item $newsEntry */
             $exists = NewsEntry::count([['sourceId' => $this->_id, 'entryId' => $item->get_id()]]);
             if ($exists > 0) {
                 continue;
             }
             $newsEntry = new NewsEntry();
             $newsEntry->sourceId = $this->_id;
             $newsEntry->entryId = $item->get_id();
             $newsEntry->title = $item->get_title();
             $newsEntry->description = $item->get_description();
             $newsEntry->link = $item->get_link();
             $newsEntry->publishedAt = $item->get_date('U');
             $newsEntry->updatedAt = $item->get_updated_date('U');
             $newsEntry->author = $item->get_author() ? $item->get_author()->get_name() : '';
             $newsEntry->categories = [];
             /** @var SimplePie_Category $category */
             if ($item->get_categories()) {
                 foreach ($item->get_categories() as $category) {
                     if ($category->get_term()) {
                         $newsEntry->categories[] = $category->get_term();
                     }
                 }
             }
             $client = new \GuzzleHttp\Client();
             $response = $client->get($item->get_link());
             $newsEntry->link = $response->getEffectiveUrl();
             if ($newsEntry->save() && $newsEntry->updateStats()) {
                 $newsEntry->save();
             }
         } catch (\Exception $e) {
             continue;
         }
     }
     return true;
 }