예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     Source::create(['name' => 'NU.nl | Algemeen nieuws', 'description' => 'NU.nl | Algemeen nieuws', 'source_url' => 'http://www.nu.nl/rss/Algemeen', 'max_items' => 10, 'is_enabled' => true]);
     Source::create(['name' => 'NU.nl | Internet', 'description' => 'NU.nl | Internet', 'source_url' => 'http://www.nu.nl/rss/Internet', 'max_items' => 10, 'is_enabled' => true]);
     Source::create(['name' => 'Tweakers.net', 'description' => 'Tweakers.net is sinds 1998 de grootste website in Nederland over technologie en elektronica met nieuws, reviews en de bekroonde Pricewatch.', 'source_url' => 'http://feeds.feedburner.com/tweakers/mixed', 'max_items' => 10, 'is_enabled' => true]);
     Source::create(['name' => 'Laravel News Blog', 'description' => 'Laravel News Blog', 'source_url' => 'http://feed.laravel-news.com/', 'max_items' => 10, 'is_enabled' => true]);
 }
예제 #2
0
 /**
  * Load Sources
  *
  * @return array|static[]
  */
 public static function loadSources()
 {
     try {
         $sources = Source::where('is_enabled', '=', '1')->orderBy('name', 'asc');
     } catch (\InvalidArgumentException $e) {
         return [];
     }
     return $sources->get();
 }
예제 #3
0
 /**
  * @return mixed
  */
 public function index_onDelete()
 {
     if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {
         foreach ($checkedIds as $sourceId) {
             if (!($source = Source::find($sourceId))) {
                 continue;
             }
             $source->delete();
         }
     }
     return $this->listRefresh();
 }
예제 #4
0
 /**
  * Fetches RSS items from source
  *
  * @throws ApplicationException
  * @return void
  */
 public function onFetch()
 {
     try {
         $source = Source::findOrFail($this->params[0]);
         if ($source instanceof Source && !$source->getAttribute('is_enabled')) {
             throw new \RuntimeException('Source is not enabled, please enabled it first.');
         }
         Artisan::call('adrenth:fetch-rss', ['source' => $this->params[0]]);
         Flash::success('Successfully fetched RSS items for this source');
     } catch (\Exception $e) {
         throw new ApplicationException('Error while fetching RSS items: ' . $e->getMessage());
     }
 }
예제 #5
0
 /**
  * {@inheritdoc}
  */
 public function importData($results, $sessionKey = null)
 {
     foreach ($results as $row => $data) {
         try {
             $source = Source::make();
             $except = ['id'];
             foreach (array_except($data, $except) as $attribute => $value) {
                 $source->setAttribute($attribute, $value);
             }
             $source->forceSave();
             $this->logCreated();
         } catch (\Exception $e) {
             $this->logError($row, $e->getMessage());
         }
     }
 }
예제 #6
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>');
         }
     });
 }
예제 #7
0
use Zend\Feed\Writer\Entry;
use Zend\Feed\Writer\Feed;
Route::get('/feeds/{path}', function ($path) {
    /** @type FeedModel $model */
    $model = FeedModel::where('path', '=', $path)->first();
    if ($model === null) {
        return Response::make('Not Found', 404);
    }
    $feed = new Feed();
    $feed->setTitle($model->getAttribute('title'))->setDescription($model->getAttribute('description'))->setBaseUrl(Url::to('/'))->setGenerator('OctoberCMS/Adrenth.RssFetcher')->setId('Adrenth.RssFecther.' . $model->getAttribute('id'))->setLink(Url::to('/feeds/' . $path))->setFeedLink(Url::to('/feeds/' . $path), $model->getAttribute('type'))->setDateModified()->addAuthor(['name' => 'OctoberCMS']);
    /** @type Collection $sources */
    $sources = $model->sources;
    $ids = Arr::pluck($sources->toArray(), 'id');
    $items = [];
    Source::with(['items' => function ($builder) use(&$items, $model) {
        $items = $builder->where('is_published', '=', 1)->whereDate('pub_date', '=', date('Y-m-d'))->orderBy('pub_date', 'desc')->limit($model->getAttribute('max_items'))->get();
    }])->whereIn('id', $ids)->where('is_enabled', '=', 1)->get();
    /** @type Item $item */
    foreach ($items as $item) {
        try {
            $entry = new Entry();
            $entry->setId((string) $item->getAttribute('id'))->setTitle($item->getAttribute('title'))->setDescription($item->getAttribute('description'))->setLink($item->getAttribute('link'))->setDateModified($item->getAttribute('pub_date'));
            $comments = $item->getAttribute('comments');
            if (!empty($comments)) {
                $entry->setCommentLink($comments);
            }
            $category = $item->getAttribute('category');
            if (!empty($category)) {
                $entry->addCategory(['term' => $category]);
            }
            $feed->addEntry($entry);