28
loading...
This website collects cookies to deliver better user experience
./vendor/bin/sail up -d
http://localhost
from your browser../vendor/bin/sail
on the root of the application folder, so that you can run Sail with ./sail
. You can create such an alias with the following commands, executed from the application root folder:ln -s ./vendor/bin/sail sail
chmod +x sail
make:command
utility. This will create a new file with boilerplate code for an Artisan command, saved at app/Console/Commands/
../sail artisan make:command ImportPosts
app/Console/Commands/ImportPosts.php
in your code editor, and you'll see content like this:<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class ImportPosts extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:name';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
return 0;
}
}
command:name
for the command signature you'd like to use. In this demo we'll use import:dev
.handle()
method is where the command action happens; this is what will be executed when you run artisan import:dev
from the command line.https://dev.to/api/articles?username=DEV_USERNAME
curl
. Require that dependency with:./sail composer require minicli/curly
use
directive at the top of the file so that you can easily reference Minicli\Curly\Client
in your code. We'll also use Laravel's Carbon/Carbon
library to parse the article's publish date, so you should include that within your use
directives as well. Lastly, include the Illuminate\Support\Facades\Storage
class with another use
directive, since we'll be using this facade class to check and save files within the local file storage system. app/Console/Commands/ImportDev.php
file should look like:<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Minicli\Curly\Client;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
handle()
method with the following code:public function handle()
{
$crawler = new Client();
$headers = [
'User-Agent: curly 0.1',
];
$articles_response = $crawler->get('https://dev.to/api/articles?username=DEV_USERNAME&per_page=10', $headers);
if ($articles_response['code'] !== 200) {
$this->error('Error while contacting the dev.to API.');
return Command::FAILURE;
}
$articles = json_decode($articles_response['body'], true);
foreach ($articles as $article) {
$article_slug = $article['slug'];
$published = new Carbon($article['published_at']);
$filename = $published->format('YmdH') . '-' . $article_slug .'.md';
if (Storage::disk('local')->exists($filename)) {
continue;
}
$endpoint = sprintf('https://dev.to/api/articles/%s', $article['id']);
$article_query = $crawler->get($endpoint, $headers);
if ($article_query['code'] == 200) {
$article_full = json_decode($article_query['body'], true);
Storage::disk('local')->put($filename, $article_full['body_markdown']);
$this->info("Article saved: " . $filename);
}
}
return Command::SUCCESS;
}
Storage
utility to save the body_markdown
of each article as a markdown file in the application's storage folder. The files are saved using a combination of the article's publish date and the article slug as filename, with the .md
extension../sail artisan import:dev
Article saved: 2021121515-setting-up-tailwindcss-30-on-a-laravel-project-1cb8.md
Article saved: 2021121013-creating-a-new-laravel-application-with-sail-and-docker-no-php-required-4c2n.md
Article saved: 2021120318-how-to-set-up-elgatos-stream-deck-on-ubuntu-linux-2110-pdh.md
Article saved: 2021112011-dynamic-twitter-header-images-with-dynacover-github-action-35nb.md
Article saved: 2021111818-how-to-build-github-actions-in-php-with-minicli-and-docker-1k6m.md
Article saved: 2021111618-automatically-update-your-contributors-file-with-this-github-action-workflow-d98.md
Article saved: 2021102717-10-sourcegraph-search-tricks-for-open-source-contributors-and-maintainers-44n9.md
Article saved: 2021092715-3d-design-creating-printable-solid-shapes-from-svg-files-with-inkscape-and-freecad-266e.md
Article saved: 2021090615-3d-design-with-freecad-part-2-working-with-sketcher-part-design-workbenches-3leo.md
Article saved: 2021090614-an-introduction-to-3d-design-with-freecad-part-1-navigation-3gjo.md
storage/app
folder and you'll see your posts as individual markdown files named after each post's publish date and slug.ls storage/app
2021090614-an-introduction-to-3d-design-with-freecad-part-1-navigation-3gjo.md
2021090615-3d-design-with-freecad-part-2-working-with-sketcher-part-design-workbenches-3leo.md
2021092715-3d-design-creating-printable-solid-shapes-from-svg-files-with-inkscape-and-freecad-266e.md
2021102717-10-sourcegraph-search-tricks-for-open-source-contributors-and-maintainers-44n9.md
2021111618-automatically-update-your-contributors-file-with-this-github-action-workflow-d98.md
2021111818-how-to-build-github-actions-in-php-with-minicli-and-docker-1k6m.md
2021112011-dynamic-twitter-header-images-with-dynacover-github-action-35nb.md
2021120318-how-to-set-up-elgatos-stream-deck-on-ubuntu-linux-2110-pdh.md
2021121013-creating-a-new-laravel-application-with-sail-and-docker-no-php-required-4c2n.md
2021121515-setting-up-tailwindcss-30-on-a-laravel-project-1cb8.md
public
minicli/curly
library. The command uses the underlying storage system from Laravel to store the posts as individual markdown files in your storage/app
folder.