/**
  * Get all feed entries as a parser instance
  *
  * @param \Newscoop\IngestPluginBundle\Entity\Feed $feedEntity Feed entity
  *
  * @return array Array should contain feed entries
  */
 public static function getStories(\Newscoop\IngestPluginBundle\Entity\Feed $feedEntity)
 {
     $feed = new SimplePie();
     $feed->set_feed_url($feedEntity->getUrl());
     $feedInitialized = $feed->init();
     if (!$feedInitialized) {
         throw new \Exception($feed->error());
     }
     $items = $feed->get_items();
     $entries = array();
     foreach ($items as $item) {
         $entries[] = new RFCRSSParser($item);
     }
     return array_reverse($entries);
 }
 /**
  * Get all feed entries as a parser instance
  *
  * @param \Newscoop\IngestPluginBundle\Entity\Feed $feed Feed entity
  *
  * @return array Array should contain feed entries
  */
 public static function getStories(\Newscoop\IngestPluginBundle\Entity\Feed $feed)
 {
     $finder = new Finder();
     $entries = array();
     $finder->files()->in(__DIR__ . self::FEEDS_PATH)->name('*.xml');
     foreach ($finder as $file) {
         $filePath = $file->getRealpath();
         if ($feed->getUpdated() && $feed->getUpdated()->getTimestamp() > filectime($filePath) + self::IMPORT_DELAY) {
             continue;
         }
         if (time() < filectime($filePath) + self::IMPORT_DELAY) {
             continue;
         }
         $handle = fopen($filePath, 'r');
         if (flock($handle, LOCK_EX | LOCK_NB)) {
             $entries[] = new SDAParser($filePath);
             flock($handle, LOCK_UN);
             fclose($handle);
         }
     }
     return $entries;
 }
 /**
  * Get all feed entries as a parser instance
  *
  * @param \Newscoop\IngestPluginBundle\Entity\Feed $feedEntity Feed entity
  *
  * @return array Array should contain feed entries
  */
 public static function getStories(Feed $feedEntity)
 {
     $entries = array();
     $data = array();
     $clientConfig = array('base_uri' => $feedEntity->getUrl(), 'options' => '');
     $client = new GuzzleClient($clientConfig);
     $sdk = new ContentApiSdk($client);
     $parameters = array('start_date' => date('Y-m-d', strtotime('-3 days')));
     try {
         $data = $sdk->getPackages($parameters, true);
     } catch (ContentApiException $e) {
         throw new NewscoopException($e->getMessage(), $e->getCode(), $e);
     }
     // Convert all $data into entries
     foreach ($data as $package) {
         $entryPackage = new SuperdeskContentApiParser($package);
         $images = $entryPackage->getImages();
         if (property_exists($package->associations, 'main') && empty($images)) {
             continue;
         }
         $entries[] = $entryPackage;
     }
     return $entries;
 }
 /**
  * Update a specific feed
  *
  * @param Newscoop\IngestPluginBundle\Entity\Feed $feed
  * @param boolean                                 $liftEmbargo
  */
 public function updateFeed(\Newscoop\IngestPluginBundle\Entity\Feed $feed, $liftEmbargo = true)
 {
     if (!$feed->isEnabled()) {
         throw new Exception('The feed ' . $feed->getName() . ' is not enabled and will not be updated.', 1);
     }
     $parser = $feed->getParser();
     $namespace = $parser->getNamespace();
     try {
         $unparsedEntries = $namespace::getStories($feed);
     } catch (\Exception $e) {
         $this->logger->error($feed->getName());
         $this->logger->error('Could not parse stories. (' . $e->getMessage() . ')');
     }
     $repository = $this->em->getRepository('Newscoop\\IngestPluginBundle\\Entity\\Feed\\Entry');
     // Get possible languages based on feed and selected sections(s)
     $this->allowedLanguages = array();
     $sections = $feed->getSections();
     foreach ($sections as $section) {
         $this->allowedLanguages[] = $section->getLanguage();
     }
     foreach ($unparsedEntries as $unparsedEntry) {
         try {
             if ($unparsedEntry->getNewsItemId() === '' || $unparsedEntry->getNewsItemId() === null) {
                 throw new NewscoopException('Skipped entry. Method getNewsItemId returns invalid value. (Entry title: ' . $unparsedEntry->getTitle() . ')');
             }
             $entry = $repository->findOneBy(array('feed' => $feed, 'newsItemId' => $unparsedEntry->getNewsItemId(), 'dateId' => $unparsedEntry->getDateId()));
             if ($entry === null) {
                 $entry = new \Newscoop\IngestPluginBundle\Entity\Feed\Entry();
                 $entry->setFeed($feed);
                 $entry->setNewsItemId($unparsedEntry->getNewsItemId());
                 $entry->setDateId($unparsedEntry->getDateId());
             }
             if ($unparsedEntry->getInstruction() == 'delete') {
                 if ($entry->getId() !== null) {
                     try {
                         $this->em->remove($entry);
                         if ($entry->getArticleId() !== null) {
                             $this->publisher->remove($entry);
                         }
                     } catch (Exception $e) {
                         throw new NewscoopException('Coult not remove entry. (' . $entry->getId() . ')');
                     }
                 }
             } else {
                 if ($feed->languageAutoMode()) {
                     // TODO: Check language settings from feed
                     $languageEntity = $this->getLanguage($unparsedEntry->getLanguage(), $feed->getPublication()->getDefaultLanguage());
                 } else {
                     $languageEntity = $feed->getLanguage();
                 }
                 // only add entry
                 $entry->setLanguage($languageEntity);
                 // Check section handling by parser
                 $sectionEntity = null;
                 // Default can be null, but not for autopublishin
                 if ($namespace::getHandlesSection() == AbstractParser::SECTION_HANDLING_FULL || $namespace::getHandlesSection() == AbstractParser::SECTION_HANDLING_PARTIAL) {
                     if ($unparsedEntry->getSection() instanceof \Newscoop\Entity\Section) {
                         $sectionEntity = $unparsedEntry->getSection();
                     } elseif (is_int($unparsedEntry->getSection())) {
                         // Try to find entity
                         $sectionEntity = $this->em->getRepository('Newscoop\\Entity\\Section')->findOneByNumber($unparsedEntry->getSection());
                     }
                 }
                 if ($namespace::getHandlesSection() == AbstractParser::SECTION_HANDLING_NONE || $namespace::getHandlesSection() == AbstractParser::SECTION_HANDLING_PARTIAL && $sectionEntity == null) {
                     $sections = $feed->getSections();
                     foreach ($sections as $section) {
                         if ($section->getLanguage() == $languageEntity) {
                             $sectionEntity = $section;
                             break;
                         }
                     }
                 }
                 $entry->setSection($sectionEntity);
                 $entry->setCreated($unparsedEntry->getCreated());
                 $entry->setTitle($unparsedEntry->getTitle());
                 $entry->setContent($unparsedEntry->getContent());
                 $entry->setCatchLine($unparsedEntry->getCatchLine());
                 $entry->setSummary($unparsedEntry->getSummary());
                 $entry->setUpdated($unparsedEntry->getUpdated());
                 $entry->setEmbargoed($unparsedEntry->getLiftEmbargo());
                 $entry->setProduct($unparsedEntry->getProduct());
                 $entry->setStatus($unparsedEntry->getStatus());
                 $entry->setPriority($unparsedEntry->getPriority());
                 $entry->setKeywords($unparsedEntry->getKeywords());
                 $entry->setAuthors($unparsedEntry->getAuthors());
                 $entry->setImages($unparsedEntry->getImages());
                 $entry->setLink($unparsedEntry->getLink());
                 $entry->setAttributes($unparsedEntry->setAllAttributes()->getAttributes());
                 $this->em->persist($entry);
                 try {
                     if ($entry->isPublished()) {
                         $this->publisher->update($entry);
                     } elseif ($feed->isAutoMode()) {
                         $this->publisher->publish($entry);
                     }
                 } catch (Exception $e) {
                     throw new NewscoopException('Could not publish or update entry ' . $unparsedEntry->getNewsItemId() . '.');
                 }
             }
             // Flush each time to prevent inconsistencies
             $this->em->flush();
         } catch (Exception $e) {
             $this->logger->error(__METHOD__ . ': ' . $e->getMessage());
         }
     }
     $feed->setUpdated(new \DateTime());
     $this->em->persist($feed);
     $this->em->flush();
     if ($liftEmbargo) {
         $this->liftEmbargo();
     }
 }