Created
December 8, 2018 16:13
-
-
Save RyanThompson/0812750ecf8695f83c23ef3b1c04f5af to your computer and use it in GitHub Desktop.
Importing Block-Powered Posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Crawford\CrawfordTheme\Console; | |
use Anomaly\BlocksModule\Block\Contract\BlockRepositoryInterface; | |
use Anomaly\PostsModule\Category\Contract\CategoryRepositoryInterface; | |
use Anomaly\PostsModule\Post\Contract\PostRepositoryInterface; | |
use Anomaly\PostsModule\Type\Contract\TypeRepositoryInterface; | |
use Anomaly\Streams\Platform\Model\Posts\PostsDefaultPostsEntryModel; | |
use Anomaly\WysiwygBlockExtension\Block\BlockModel; | |
use Carbon\Carbon; | |
use Goutte\Client; | |
use Illuminate\Console\Command; | |
use Symfony\Component\DomCrawler\Crawler; | |
class ImportPosts extends Command | |
{ | |
protected $name = 'import'; | |
public function handle( | |
PostRepositoryInterface $posts, | |
TypeRepositoryInterface $types, | |
BlockRepositoryInterface $blocks, | |
CategoryRepositoryInterface $categories | |
) { | |
$posts->truncate(); | |
$type = $types->findBySlug('default'); | |
$category = $categories->findBySlug('news'); | |
for ($i = 0; $i < 19; $i++) { | |
$this->warn('Starting: ' . 'https://crawford-company.com/newsroom' . ($i ? ('/P' . ($i * 16)) : '')); | |
/* @var Crawler $crawler */ | |
$crawler = (new Client())->request( | |
'GET', | |
'https://crawford-company.com/newsroom' . ($i ? ('/P' . ($i * 16)) : '') | |
); | |
$crawler->filter('.articles-holder')->each( | |
function (Crawler $crawler) use ($blocks, $posts, $type, $category) { | |
$title = $crawler->filter('h4')->text(); | |
$date = Carbon::createFromTimestamp(strtotime($crawler->filter('time')->text())); | |
$summary = $crawler->filter('p')->text(); | |
$slug = str_slug($title); | |
$count = 1; | |
while (true) { | |
if ($posts->findBySlug($slug)) { | |
$slug = $slug . '-' . $count; | |
$count += 1; | |
continue; | |
} else { | |
break; | |
} | |
} | |
/* @var Crawler $article */ | |
$article = (new Client())->request('GET', $target = $crawler->filter('a')->attr('href')); | |
$this->warn('Importing: ' . $target); | |
$content = $article->filter('.content-wrapper'); | |
$entry = (new PostsDefaultPostsEntryModel())->create(); | |
$content = str_replace( | |
'/images/uploads/_sm/', | |
'/files/images/', | |
$content->html() | |
); | |
if ($article->filter('.alignright.hidden-xs')->count()) { | |
$content = str_replace( | |
'/images/uploads/_sm/', | |
'/files/images/', | |
$article->filter('.alignright.hidden-xs')->html() | |
) . $content; | |
} | |
$block = (new BlockModel())->create( | |
[ | |
'en' => [ | |
'content' => $content, | |
], | |
] | |
); | |
$posts->create( | |
[ | |
'en' => [ | |
'title' => $title, | |
'summary' => $summary, | |
], | |
'slug' => $slug, | |
'publish_at' => $date, | |
'enabled' => true, | |
'type' => $type, | |
'entry' => $entry, | |
'category' => $category, | |
'author' => 1, | |
] | |
); | |
$blocks->create( | |
[ | |
'area' => $entry, | |
'entry' => $block, | |
'field' => $type->getEntryStream()->getField('content'), | |
'extension' => 'anomaly.extension.wysiwyg_block', | |
'sort_order' => 1, | |
] | |
); | |
$this->info('Imported: ' . $target); | |
sleep(2); | |
} | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment