Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Medium-Publish article from markdown file #2302

Merged
merged 9 commits into from
Oct 16, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
296 changes: 296 additions & 0 deletions Medium/sample.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
{
"cells": [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MSaiKiran9 The file name has to be renamed according to the naming convention.
Have a look here.

{
"cell_type": "markdown",
"id": "b2986071-1a29-4f66-995e-72565d384ce8",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"cell_type": "markdown",
"id": "5d7b0c0b-5e6c-4be3-b901-704fc00b6c08",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# Medium - Publish article from Mardown file"
]
},
{
"cell_type": "markdown",
"id": "ba4944a7-f4e5-4e89-9e3e-b4ee2e12c15f",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #medium #publish #article #markdown #file #api"
]
},
{
"cell_type": "markdown",
"id": "c96fc2a3-0910-4b6d-a735-1e83cd5fa24e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [SaiKiran M](www.linkedin.com/in/msaikiran9)"
]
},
{
"cell_type": "markdown",
"id": "edcf9f6c-c073-46c4-92ab-cc72866daf37",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-10-09 (Created: 2023-10-09)"
]
},
{
"cell_type": "markdown",
"id": "9b1586dd-d9e8-4f75-a583-c5cd5952bb05",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook explains how to publish an article from a Markdown file via the Medium API. It is usefull for organizations that need to quickly publish content on Medium."
]
},
{
"cell_type": "markdown",
"id": "cc1d8d75-0c1f-41ad-a6b0-b0eb43fcc665",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**References:**\n",
"- [Programmatically Publish a Markdown File as a Medium Story with Python](https://betterprogramming.pub/programmatically-publish-a-markdown-file-as-a-medium-story-with-python-b2b072a5f968)\n",
"- [Medium API Documentation](https://github.com/Medium/medium-api-docs)"
]
},
{
"cell_type": "markdown",
"id": "32f771ab-b85d-44c2-8555-004b07b512ea",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "74c2e136-cd6b-4079-9338-0b609b927692",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e4bad8c4-13c7-4c1c-b97d-46fc1ced422c",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"try:\n",
" import naas\n",
" import json\n",
" import requests\n",
"except ImportError:\n",
" print(\"One or more modules not found. Installing necessary packages...\")\n",
" !pip install requests\n",
" import requests"
]
},
{
"cell_type": "markdown",
"id": "85e28cc8-0742-4aed-a9ee-c0b98cc00de4",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n",
"- `token`: Medium access token. [Instructions to get the token](https://github.com/Medium/medium-api-docs#22-authentication).\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

A step-wise guide would be better. Something similar to take inspiration from, not the exact same thing.

"- `file_name`: Name of the Markdown file to be published."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7acc98ec-b364-4164-8524-bb0b7109d6c4",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"TOKEN = naas.secret.get(name=\"key\")\n",
"file_name = \"sample.md\""
]
},
{
"cell_type": "markdown",
"id": "252ce263-9e42-4b03-aa2a-57efb0294c64",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "20f812e5-9880-4c5e-92d8-71591450bdc1",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Publish article"
]
},
{
"cell_type": "markdown",
"id": "f0e2693b-779a-4c19-9c6d-e46c756e8236",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"Long description of the function without break"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "650f0106-6407-4fa8-a4e5-019762a526b1",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"def publish_post(title, content):\n",
" headers = {\n",
" \"Authorization\": f\"Bearer {TOKEN}\",\n",
" \"Content-Type\": \"application/json\"\n",
" }\n",
" \n",
" payload = {\n",
" \"title\": title,\n",
" \"contentFormat\": \"markdown\",\n",
" \"content\": content,\n",
" \"tags\": [\"example\", \"python\"],\n",
" \"publishStatus\": \"public\",\n",
" \"canonicalUrl\": \"http://example.com/sample\"\n",
" } \n",
" \n",
" response = requests.get(\"https://api.medium.com/v1/me\", headers=headers, params={\"Authorization\": \"Bearer {}\".format(TOKEN)})\n",
" if response.status_code == 200:\n",
" author_id = response.json()['data']['id']\n",
" url = \"https://api.medium.com/v1/users/{}/posts\".format(author_id)\n",
" else:\n",
" return\n",
" \n",
" response = requests.post(url, headers=headers, json=payload)\n",
" if response.status_code == 201:\n",
" return response.json()[\"data\"][\"url\"]\n",
" else:\n",
" raise Exception(f\"Failed to publish post: {response.json()}\")\n",
"\n",
"# Read the content of the Markdown file\n",
"with open(\"sample.md\", \"r\", encoding=\"utf-8\") as markdown_file:\n",
" content = markdown_file.read()\n",
"\n",
"# Publish the post\n",
"post_url = publish_post(\"Sample Post Title\", content)\n",
"\n",
"print(\"Post published successfully!\")\n",
"print(\"Post URL:\", post_url)"
]
},
{
"cell_type": "markdown",
"id": "a4948757-3194-45e2-96f5-2e4463071eb3",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "2aff52b9-5335-4240-9306-cb1238b29506",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Display result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b0af2a9-5227-45ac-9da9-d0bffabbbe0c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"print(post_url)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading