/** * Load Items * * @param int $maxItems * @return array|static[] */ public static function loadItems($maxItems = 10) { try { $items = Item::select(['adrenth_rssfetcher_items.*', 'adrenth_rssfetcher_sources.name AS source'])->join('adrenth_rssfetcher_sources', 'adrenth_rssfetcher_items.source_id', '=', 'adrenth_rssfetcher_sources.id')->where('adrenth_rssfetcher_sources.is_enabled', '=', 1)->where('adrenth_rssfetcher_items.is_published', '=', 1)->orderBy('adrenth_rssfetcher_items.pub_date', 'desc')->limit($maxItems); } catch (\InvalidArgumentException $e) { return []; } return $items->get(); }
/** * Load Items * * @return LengthAwarePaginator|array */ protected function loadItems() { try { $items = Item::select(['adrenth_rssfetcher_items.*', 'adrenth_rssfetcher_sources.name AS source'])->join('adrenth_rssfetcher_sources', 'adrenth_rssfetcher_items.source_id', '=', 'adrenth_rssfetcher_sources.id')->where('adrenth_rssfetcher_sources.is_enabled', '=', 1)->where('adrenth_rssfetcher_items.is_published', '=', 1)->orderBy('adrenth_rssfetcher_items.pub_date', 'desc')->paginate($this->property('itemsPerPage')); } catch (\InvalidArgumentException $e) { return []; } return $items; }
/** * @return mixed */ public function index_onDelete() { if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) { foreach ($checkedIds as $sourceId) { if (!($source = Item::find($sourceId))) { continue; } $source->delete(); } } return $this->listRefresh(); }
/** * 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>'); } }); }