Exemplo n.º 1
0
 /**
  * Execute the console command.
  *
  * @return void
  * @throws \RuntimeException
  * @throws \Exception
  */
 public function fire()
 {
     $sourceId = $this->argument('source');
     $sources = new Collection();
     if ($sourceId !== null && is_numeric($sourceId)) {
         $source = Source::find($sourceId);
         if ($source && $source->getAttribute('is_enabled')) {
             $sources = new Collection([$source]);
         }
     } else {
         $sources = Source::query()->where('is_enabled', '=', 1)->get();
     }
     $sources->each(function (Source $source) {
         try {
             $channel = Reader::import($source->getAttribute('source_url'));
             $maxItems = $source->getAttribute('max_items');
             $this->getOutput()->writeln($channel->getTitle());
             $itemCount = 0;
             /** @type Rss $item */
             foreach ($channel as $item) {
                 ++$itemCount;
                 $this->getOutput()->writeln($itemCount . '. ' . $item->getTitle());
                 $attributes = ['item_id' => $item->getId(), 'source_id' => $source->getAttribute('id'), 'title' => $item->getTitle(), 'link' => $item->getLink(), 'description' => strip_tags($item->getDescription()), 'category' => implode(', ', $item->getCategories()->getValues()), 'comments' => $item->getCommentLink(), 'pub_date' => $item->getDateCreated(), 'is_published' => $source->getAttribute('publish_new_items')];
                 if ($item->getAuthors() !== null && is_array($item->getAuthors())) {
                     $attributes['author'] = implode(', ', $item->getAuthors());
                 }
                 Item::firstOrCreate($attributes);
                 if ($maxItems > 0 && $itemCount >= $maxItems) {
                     break;
                 }
             }
             $source->setAttribute('fetched_at', new Carbon());
             $source->save();
         } catch (\Exception $e) {
             $this->getOutput()->writeln('<error>' . $e->getMessage() . '</error>');
         }
     });
 }