Ejemplo n.º 1
0
 /**
  * @param array $source
  * @return array
  */
 protected function fetchSource(array $source)
 {
     $fetcher = new Fetcher();
     $fetcher->setClient(new Client());
     $fetcher->setTimeout($this->app->config('fetcher.timeout'));
     $content = $fetcher->get($source['uri']);
     if (!empty($content)) {
         $defaultWorkerClass = $this->app->config('worker.default');
         /**
          * @var $worker \Nogo\Feedbox\Feed\Worker
          */
         $worker = new $defaultWorkerClass();
         $worker->setSanitizer($this->sanitizer);
         $worker->setContent($content);
         try {
             $items = $worker->execute();
         } catch (\Exception $e) {
             $items = null;
         }
         if ($items != null) {
             foreach ($items as $item) {
                 if (isset($item['uid'])) {
                     $dbItem = $this->itemRepository->findBy('uid', $item['uid']);
                     if (!empty($dbItem)) {
                         if ($item['content'] !== $dbItem['content'] || $item['title'] !== $dbItem['title']) {
                             $item['id'] = $dbItem['id'];
                             $item['starred'] = $dbItem['starred'];
                             $item['created_at'] = $dbItem['created_at'];
                         } else {
                             continue;
                         }
                     }
                 }
                 $item['source_id'] = $source['id'];
                 $this->itemRepository->persist($item);
             }
         }
         $source['last_update'] = date('Y-m-d H:i:s');
         if (empty($source['period'])) {
             $source['period'] = $worker->getUpdateInterval();
         }
         $source['errors'] = $worker->getErrors();
     } else {
         $source['errors'] = $fetcher->getError();
     }
     $source['unread'] = $this->itemRepository->countSourceUnread([$source['id']]);
     $this->sourceRepository->persist($source);
     // update tag unread counter
     if (!empty($source['tag_id'])) {
         $tag = $this->tagRepository->find($source['tag_id']);
         if ($tag) {
             $tag['unread'] = $this->sourceRepository->countTagUnread([$tag['id']]);
             $this->tagRepository->persist($tag);
         }
     }
     // clean up double uids in this source
     $uids = $this->itemRepository->findDoubleUid($source['id']);
     foreach ($uids as $uid) {
         $items = $this->itemRepository->findAllBy('uid', $uid);
         for ($i = 1; $i < count($items); $i++) {
             $this->itemRepository->remove($items[$i]['id']);
         }
     }
     return $source;
 }
Ejemplo n.º 2
0
// load API config
$configLoader = new ConfigLoader(ROOT_DIR . '/src/Nogo/Feedbox/Resources/config/default.yml', ROOT_DIR . '/data/config.yml');
$config = $configLoader->getConfig();
// database connection with pdo
$connector = new DatabaseConnector($config['database_adapter'], $config['database_dsn'], $config['database_username'], $config['database_password']);
$connection = $connector->getInstance();
// create repositories
$sourceRepository = new Source($connection);
$tagRepository = new Tag($connection);
$itemRepository = new Item($connection);
// fetch active sources with uri
$sources = $sourceRepository->findAllActiveWithUri();
// get the feed runner
$defaultWorkerClass = $config['worker.default'];
$sanitizer = new \Nogo\Feedbox\Helper\HtmlPurifierSanitizer();
$fetcher = new Fetcher();
$fetcher->setClient(new Client());
$fetcher->setTimeout($config['fetcher.timeout']);
$now = new \DateTime();
$user_id = null;
foreach ($sources as $source) {
    $error = false;
    if (!empty($source['uri'])) {
        $user_id = $source['user_id'];
        // periodic update
        if ($source['last_update'] != null) {
            $last_update = new \DateTime($source['last_update']);
            $interval = $last_update->diff($now, true);
            if ($interval !== false) {
                switch ($source['period']) {
                    case 'hourly':