Skip to content

Commit 91dbbba

Browse files
committed
initial commit
1 parent bf1924c commit 91dbbba

File tree

12 files changed

+5717
-92
lines changed

12 files changed

+5717
-92
lines changed

Command/Help/DefaultController.php

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace librarianphp\Import;
4+
5+
use Minicli\Command\CommandController;
6+
7+
class DefaultController extends CommandController
8+
{
9+
public function handle(): void
10+
{
11+
$this->getPrinter()->info("Import posts from DEV.", true);
12+
$this->getPrinter()->info("./librarian import devto");
13+
}
14+
}

Command/Import/DevController.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace librarianphp\Import;
4+
5+
use Librarian\Exception\ApiException;
6+
use Provider\DevtoServiceProvider;
7+
use Minicli\Command\CommandController;
8+
9+
class DevController extends CommandController
10+
{
11+
public function handle(): void
12+
{
13+
/** @var DevtoServiceProvider $devto */
14+
$devto = $this->getApp()->devto;
15+
$app_debug = $this->getApp()->config->app_debug;
16+
17+
if ($devto === null) {
18+
if ($app_debug) {
19+
$this->getPrinter()->error('ERROR: dev.to service not found. Perhaps you forgot to register it?');
20+
}
21+
exit;
22+
}
23+
24+
if (!$this->getApp()->config->devto_username) {
25+
if ($app_debug) {
26+
$this->getPrinter()->error(
27+
"ERROR: dev.to username not set.\n" .
28+
"You must define a devto_username in your config file\n" .
29+
"if you want to import posts from that platform."
30+
);
31+
}
32+
exit;
33+
}
34+
35+
$this->getPrinter()->info("Starting import... this might take a few minutes.");
36+
37+
try {
38+
$devto->fetchAll();
39+
} catch (ApiException $e) {
40+
$this->getPrinter()->error($e->getMessage());
41+
}
42+
43+
$this->getPrinter()->success("Import Finished.");
44+
}
45+
}

Provider/DevtoServiceProvider.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Provider;
4+
5+
use Librarian\Content;
6+
use Librarian\Exception\ApiException;
7+
use Librarian\Exception\MissingConfigException;
8+
use Minicli\App;
9+
use Minicli\ServiceInterface;
10+
use Minicli\Curly\Client;
11+
12+
class DevtoServiceProvider implements ServiceInterface
13+
{
14+
/** @var string Dev.to username to pull articles from */
15+
protected string $username;
16+
17+
/** @var string local path to save static markdown files from dev.to content */
18+
protected string $data_path;
19+
20+
public static string $API_ARTICLES_ENDPOINT = 'https://dev.to/api/articles';
21+
22+
/**
23+
* @param App $app
24+
* @throws MissingConfigException
25+
*/
26+
public function load(App $app): void
27+
{
28+
if (!$app->config->has('devto_username')) {
29+
throw new MissingConfigException('devto_username config not found.');
30+
}
31+
32+
if (!$app->config->has('devto_datadir')) {
33+
throw new MissingConfigException('devto_datadir config not found.');
34+
}
35+
36+
if (!$app->config->has('data_path')) {
37+
throw new MissingConfigException('data_path config not found.');
38+
}
39+
40+
$this->username = $app->config->devto_username;
41+
$this->data_path = $app->config->data_path . '/' . $app->config->devto_datadir;
42+
}
43+
44+
/**
45+
* Fetches the latest dev.to articles and save them to local data dir as .md files.
46+
* Warning: Existing content will be overwritten.
47+
* @throws ApiException
48+
*/
49+
public function fetchAll(): void
50+
{
51+
$crawler = new Client();
52+
53+
$articles_response = $crawler->get(self::$API_ARTICLES_ENDPOINT . '?username=' . $this->username);
54+
55+
if ($articles_response['code'] !== 200) {
56+
throw new ApiException('Error while contacting the dev.to API.');
57+
}
58+
59+
$articles = json_decode($articles_response['body'], true);
60+
61+
foreach ($articles as $article) {
62+
$full_article_response = $crawler->get(self::$API_ARTICLES_ENDPOINT . '/' . $article['id']);
63+
64+
if ($full_article_response['code'] !== 200) {
65+
throw new ApiException('Error while contacting the dev.to API.');
66+
}
67+
68+
$full_article = json_decode($full_article_response['body'], true);
69+
70+
try {
71+
$published = new \DateTime($full_article['published_timestamp']);
72+
} catch (\Exception $e) {
73+
$published = new \DateTime();
74+
}
75+
76+
$date = $published->format('Ymd');
77+
78+
$content = new Content($full_article['body_markdown']);
79+
$content->save($this->data_path . '/' . $date . '_' . $full_article['slug'] . '.md');
80+
}
81+
}
82+
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# command-help
1+
# command-import
22

3-
Librarian's built-in help command.
3+
Librarian's built-in import command.
44

55
```shell
6-
./librarian help
6+
./librarian import dev
77
```

composer.json

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
{
2-
"name": "librarianphp/command-help",
2+
"name": "librarianphp/command-import",
33
"type": "library",
4-
"description": "Librarian's built-in help command",
4+
"description": "Librarian's built-in import command",
55
"license": "MIT",
6-
"homepage": "https://github.com/librarianphp/command-demo",
7-
"keywords": ["cli","command-line", "markdown"],
6+
"homepage": "https://github.com/librarianphp/command-import",
7+
"keywords": ["cli","command-line", "markdown", "devto"],
88
"autoload": {
99
"psr-4": {
10-
"librarianphp\\": "Command/"
10+
"librarianphp\\": "Command/",
11+
"Provider\\": "Provider/"
1112
}
1213
},
1314
"require": {
14-
"minicli/minicli": "^3.0"
15+
"minicli/minicli": "^3.2.1",
16+
"minicli/curly": "^0.2.2",
17+
"librarianphp/librarian-core": "^3.1"
18+
},
19+
"require-dev": {
20+
"pestphp/pest": "^2.5",
21+
"friendsofphp/php-cs-fixer": "^3.16",
22+
"mockery/mockery": "^1.5"
23+
},
24+
"scripts": {
25+
"test" : ["pest"],
26+
"csfix": ["php-cs-fixer fix"]
27+
},
28+
"config": {
29+
"allow-plugins": {
30+
"pestphp/pest-plugin": true
31+
}
1532
}
1633
}

0 commit comments

Comments
 (0)