Skip to content

Instantly share code, notes, and snippets.

@getinstancemz
Created February 24, 2023 17:19
Show Gist options
  • Save getinstancemz/a38e28d8c69861cb8b393a728c48dcdd to your computer and use it in GitHub Desktop.
Save getinstancemz/a38e28d8c69861cb8b393a728c48dcdd to your computer and use it in GitHub Desktop.
<?php
class MediumPoster
{
private CurlService $service;
private string $workingdir;
public function __construct($intToken, $workingdir = "")
{
$this->intToken = $intToken;
$this->workingdir = $workingdir;
$this->service = new CurlService('https://api.medium.com/v1');
$this->service->setHeader("Authorization", "Bearer {$intToken}");
}
public function getMe(): \stdClass
{
return $this->service->get("/me");
}
public function uploadImage(string $imagePath): ?string
{
$endpoint = '/images';
$url = $this->service->postBin($endpoint, "image", $imagePath);
return $url->data->url ?? null;
}
public function parseArticle($content): string
{
$regexp = '!\[(.*?)\]\s*\((\S+)(?:\s+([\'"])(.*?)\3)?\s*\)';
// this callback will invoke an upload for local files
// and replace the local path with the uploaded URL
$func = function ($a) {
$alt = $a[1];
$url = $a[2];
if (preg_match("/^http[s]{0,1}:/i", $url)) {
// ignore non-local URLs
return $a[0];
}
$path = $this->workingdir . $url;
if (! file_exists($path)) {
error_log("unable to locate local file '$path'");
return $a[0];
}
$title = "";
if (! empty($a[3]) && ! empty($a[4])) {
// rebuild title
$title = " {$a[3]}{$a[4]}{$a[3]}";
}
$url = $this->uploadImage($path);
if (is_null($url)) {
return $a[0];
}
return "![{$alt}]({$url}{$title})";
};
return preg_replace_callback("/{$regexp}/", $func, $content);
}
public function addArticle(string $title, string $content): \stdClass
{
$content = $this->parseArticle($content);
$endpoint = '/users/' . $this->getMe()->data->id . '/posts';
$data = [
'title' => $title,
'contentFormat' => 'markdown',
'content' => $content,
'publishStatus' => 'draft'
];
return $this->service->post($endpoint, $data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment